Page 1 of 1

[Ruby] Setter with arguments?

Posted: Tue Mar 15, 2016 11:51 pm
by tulamide
The title says it all. I have nested hashes in a class and access them via methods, for example:

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) = someobject


Since this doesn't work:

Code: Select all

def something(layer1, layer2)=(myobject)
    myHash[layer0][layer1][layer2] = myobject
end


Are there any ideas, how I could achieve it nevertheless?