Re: How to create "def method(item)= (value)" ?



On Wed, 30 Apr 2008 07:22:30 -0500, Iñaki Baz Castillo wrote:

Hi, is it not possible to create a method to use in this way?

my_object.param("tag") = "new_value"

How to define that method? I've tryed something as:

def param(name)= (value)
@params[name] = value
end

but obviously I get error:

SyntaxError: compile error
syntax error, unexpected '='

Is there some way? Thanks a lot.

def initialize
@params={}
end

def param
@params
end

my_object.param["tag"]="new value"

If you need some sort of validation, you can have param return some other
class that defines [] and []= and does the necessary validation, and you
can even set up an easy method for defining the param attribute to do all
of that if need be.

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
.



Relevant Pages