Hello,
I am facing a undefined local variable or method error when initializing the following in ruby:
class Model
attr_accessor :var1, :var2, :state
def initialize (x, y, key)
@var1 = x
@var2 = y
@state = every_state[:key] #this line produces the error
@every_state = {
:A => SateA.new,
:B => StateB.new,
:C => StateC.new,
:D => StateD.new
}
end
def select_state(key)
every_state[:key]
end
end
When I am using the class like
model = Model.new(1,2,:A)
The error occurs: *undefined local variable or method `every_state'*
As I am new to ruby coming from a java background, I wanted to pass a key to the initialize method (as noted here), to select a specific initial state from the hash.
Could it be that I am using the hash in a wrong way, or should I take it out of the initialize method completely and use another method to set it? My other thought is that I am using the symbol for key incorrectly.
Also, is there a direct implication of operating on non-instance variables within the initialize method? For example I was wondering what is the purpose of declaring the hash as an instance variable within initialize...
Any ideas are very welcome.