[Ruby] Setter with arguments?
Posted: Tue Mar 15, 2016 11:51 pm
The title says it all. I have nested hashes in a class and access them via methods, for example:
It is called straight-forward:
However, when I try to create a setter with these arguments, it end in an error. Ruby seems to only work with no-argument-methods as setters. I try to get something, that allows me to do that:
Since this doesn't work:
Are there any ideas, how I could achieve it nevertheless?
Code: Select all
... (class defintition and intitializing left out)
def something(layer1, layer2)
return myHash[layer0][layer1][layer2]
end
It is called straight-forward:
Code: Select all
myClass.something(:first, :second)However, when I try to create a setter with these arguments, it end in an error. Ruby seems to only work with no-argument-methods as setters. I try to get something, that allows me to do that:
Code: Select all
myClass.something(:first, :second) = someobjectSince this doesn't work:
Code: Select all
def something(layer1, layer2)=(myobject)
myHash[layer0][layer1][layer2] = myobject
endAre there any ideas, how I could achieve it nevertheless?