Undefined local variable for hash in method ruby

后端 未结 3 1634
别那么骄傲
别那么骄傲 2020-12-12 05:21

for some reason I\'m getting

NameError: undefined local variable or method `states\' for main:Object

though states is clearly defined. What

3条回答
  •  执笔经年
    2020-12-12 06:17

    The method can't access the outside variables. You could use instance variables instead. From the documentation:

    Instance variables are shared across all methods for the same object.

    Example:

    @states = {
      CA: 'California',
      FL: 'Florida',
      MI: 'Michigan',
      NY: 'New York',
      OR: 'Oregon',
    }
    
    @states[:CO] = 'Colorado'
    @states[:HI] = 'Hawaii'
    
    @cities = {
      CA: ['Alameda', 'Apple Valley', 'Exeter'],
      FL: ['Exeter', 'Amelia Island', 'Bunnell'],
      MI: ['Ann Arbor', 'East China', 'Elberta'],
      NY: ['Angelica', 'Apalachin', 'Canadice'],
      OR: ['Amity', 'Boring', 'Camas Valley'],
      CO: ['Blanca', 'Crestone', 'Dillon', 'Fairplay'],
      HI: ['Kailua', 'Hoopili', 'Honolulu'],
    }
    
    def describe_state state
      description = "#{state.to_s} is for #{@states[state]}."
      description << " It has #{@cities[state].length} major cities: "
      description << @cities[state].join(', ')
    end
    
    puts describe_state :CA
    #=> CA is for California. It has 3 major cities: Alameda, Apple Valley, Exeter
    

    (I've fixed a minor bug in describe_state)

提交回复
热议问题