So I create a class in ruby:
class User
def initialize
end
end
Now say I want to create a attribute that is a hash with a getter/setter, I'm confused as to the options I have doing this.
If I do this:
class User
attr_accessor :some_hash
end
But I don't want this hash to be nil ever, always an empty hash.
I'm confused when I should do:
def some_hash
self.some_hash ||= {}
end
and do:
def some_hash
@some_hash ||= {}
end
What is the difference?
If I don't use the attr_accessor, I have to create both the reader and writer (or getter/setter):
def some_hash=()
end
def some_hash
end
I'm hoping someone can clear up the options I would have for creating a some_hash attribute which is a hash, and that never returns a nil if it is empty.
i.e. using attr_accessor, manually creating the methods, and finally when to use @some_hash and self.some_hash