I was looking at Groovyclosures 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 : Groovy, Ruby, Closures
0 Comments:
Post a Comment
<< Home