[Ruby] Setter with arguments?

For general discussion related FlowStone
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

[Ruby] Setter with arguments?

Post 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?
"There lies the dog buried" (German saying translated literally)
Post Reply