When you define an instance variable in a class cube, it is a class instance variable defined on CheckOut, which is an instance Classand does not exist in the instance CheckOut. Instead, you need to define the instance variables in yours initialize, as you have already found (since it initializeexecutes in the context of the new instance CheckOut):
class CheckOut
def initialize(rules, discounts)
@total = 0
@basket = Hash.new
@rules = rules
end
...
end
Here is a brief example to illustrate this further:
class Foo
@bar = "class bar!"
@baz = "class baz!"
def initialize
@bar = "instance bar!"
end
end
Foo.instance_variable_get(:@bar)
Foo.new.instance_variable_get(:@bar)
Foo.instance_variable_get(:@baz)
Foo.new.instance_variable_get(:@baz)
, nil, . NoMethodError for nil:NilClass, NameError.