[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: RCR 307: allow attributes to take arguments for assignment

Eric Mahurin

5/15/2005 7:48:00 PM


--- ES <ruby-ml@magical-cat.org> wrote:
>
> Le 15/5/2005, "Eric Mahurin" <eric_mahurin@yahoo.com> a
> ?crit:
> >I just put in a RCR that allows for a little more syntax
> sugar.
> > The proposal is just to make:
> >
> > object.attribute(arguments) = expression
> >
> >to be equivalent to:
> >
> > object.send(:attribute=,arguments,expression)
> >
> >This would just be a nice-to-have RCR in my opinion as you
> can
> >use standard methods instead. But, it does seem like
> Rubyish
> >syntax. Here is the link if you are interested:
> >
> >http://www.rcrchive.net/rc...
>
> Call me stupid, but I am afraid I do not understand what this
> code would *do*. Could you clarify, maybe post an example (if
> possible, with Ruby code)?
>
> E

I put 3 examples (just added one more) in the RCR. Here is one
of them elaborated a little more. This extends the "values_at"
"attribute" in Array and Hash that retrieves values from a list
of indices/keys to also set those values:

class Array # or Hash
def values_at=(*args)
rhs = args.pop
args.zip(rhs).each { |assign|
self[assign[0]] = assign[1]
}
self
end
end

# current usage
array_or_hash.send(:values_at=,*indices,values_array)

# proposed usage
array_or_hash.values_at(*indices) = values_array

a=(0..15).to_a
a.send(:values_at=,3,9,11..15,["three","nine","11..15"])
a # [0, 1, 2, "three", 4, 5, 6, 7, 8, "nine", 10, "11..15"]




__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/...