Shodhan Sheth
22.m@Pune
single n lookin
shodhan[dot]sheth[at]gmail[dot]com

linkZ n feedZ

Site Feed(Atom)
Geeky Feed(RSS/Atom Combo)
BlogLines Subscription
del.icio.us feed(RSS)

Powered by
Blogger

Creative Commons License
This work is licensed under a Creative Commons License.

Sunday, November 20, 2005

GroovY vs RubY {Closures}

I was looking at Groovy closures and i was just wondering how easy it would be to implement the same example in ruby. and this is what i came up with
class Array
def apply
each_index { |i| self.[]=(i, yield(self.[](i))) }
end
end
a=[1,2,3]
a.apply { |i| i*i }
p a.to_s


Unfortunately, it turned out to be a lot more skarier than i thought it would be.
make no mistake though, klosures ARE implemented equally, if not more, gracefully in ruby. the problem in this partikular kase is the fakt that [] and []= are methods in ruby. also note, i kant use each instead of each_index, as each does not synk the values of each individual array element bak to the original kopies. i think there should be an each! method. so now, since []= is a method i kannot kall [i]= direktly. i have to use the indirekt route of treating it as a setMethod(index, setValue)
Tags : , ,

0 Comments:

Post a Comment

<< Home

Get awesome blog templates like this one from BlogSkins.com