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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

MjAwOC80LzMwLCBLZW4gQmxvb20gPGtibG9vbUBnbWFpbC5jb20+Ogo+IGRlZiBpbml0aWFsaXpl
Cj4gICBAcGFyYW1zPXt9Cj4gIGVuZAo+Cj4gIGRlZiBwYXJhbQo+ICAgQHBhcmFtcwo+ICBlbmQK
CkJ1dCBJIGRvbid0IHdhbnQgdGhhdCBzaW5jZSBpdCBhbGxvd3MgYW55IHNlbGYgbW9kaWZpY2F0
aW9uIHdheQooLnN0cmlwISAgLmNob21wISAuLi4pIGFuZCBJIGp1c3Qgd2FudCB0byBhbGxvdyBb
XT0KClRoYW5rcy4KCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
K

Ken Bloom

2008/4/30 said:
def initialize
@params={}
end

def param
@params
end

But I don't want that since it allows any self modification way (.strip!
.chomp! ...) and I just want to allow []=

Thanks.

In that case, do as I wrote further on and have the param function return
a proxy that edits the params array in only the ways you want.

class ParamsProxy
def initialize hash
#thanks to duck typing hash can be an Array instead, if you'd like
@hash=hash
end
def [] key
#with .dup here, mutating operations on the returned value
#will not affect the values stored in the @hash
#note that even attr_reader isn't this careful about the
#objects that it returns, and so if you want behavior
#like attr_reader, then omit the .dup here
@hash[key].dup
end
def []= key,value
@hash[key]=value
end
end

class MyClass
def initialize
@params={}
end

def param
ParamsProxy.new @params
end
end


I presented this in rough outline before, without code, because there are
many ways one could want to dress this up -- having some kind of
validation code, or having the object.param[key]=value update something
else entirely, or shoving the whole thing behind a nice attr_accessor-
like interface. I don't have the time now to think through all of the
possibilities involved to come up with a general solution, nor do I have
a clear idea of exactly what your other constraints are. But start here,
be creative, and let us know exactly what you do come up with.

--Ken
 
I

Iñaki Baz Castillo

El Jueves, 1 de Mayo de 2008, Ken Bloom escribi=C3=B3:
2008/4/30 said:
def initialize
@params=3D{}
end

def param
@params
end

But I don't want that since it allows any self modification way (.strip!
.chomp! ...) and I just want to allow []=3D

Thanks.

In that case, do as I wrote further on and have the param function return
a proxy that edits the params array in only the ways you want.

class ParamsProxy
def initialize hash
#thanks to duck typing hash can be an Array instead, if you'd like
@hash=3Dhash
end
def [] key
#with .dup here, mutating operations on the returned value
#will not affect the values stored in the @hash
#note that even attr_reader isn't this careful about the
#objects that it returns, and so if you want behavior
#like attr_reader, then omit the .dup here
@hash[key].dup
end
def []=3D key,value
@hash[key]=3Dvalue
end
end

class MyClass
def initialize
@params=3D{}
end

def param
ParamsProxy.new @params
end
end


I presented this in rough outline before, without code, because there are
many ways one could want to dress this up -- having some kind of
validation code, or having the object.param[key]=3Dvalue update something
else entirely, or shoving the whole thing behind a nice attr_accessor-
like interface. I don't have the time now to think through all of the
possibilities involved to come up with a general solution, nor do I have
a clear idea of exactly what your other constraints are. But start here,
be creative, and let us know exactly what you do come up with.

Thanks a lot. Very useful information :)


=2D-=20
I=C3=B1aki Baz Castillo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,205
Messages
2,571,067
Members
47,673
Latest member
MahaliaPal

Latest Threads

Top