N
Nikolai Weibull
How about adding a fifth way of constructing new arrays?:
Array.new{|array| block }
Here, array is the newly created array, and the block will initialize it
as it sees fit, e.g,
ary = Array.new{ |ary| 5.times{ |i| ary << i } },
which is a stupid way of doing it (Array.new(5){ |i| i } would do), but
it shows the idea. I often find myself writing methods that fit the
following template:
def a
ary = []
... construct ary by adding elements to it ...
ary
end
Lately I've been using the Kernel.returning method from Rails to write
it as
def a
returning ary = [] do
... construct ary by adding elements to it ...
end
end
which works quite well, but I think that I'd rather be writing it as
def a
Array.new do |ary|
... construct ary by adding elements to it ...
end
end
Array.new(size){ |index| block } would have to be changed so that size
is optional, and if not given, then this new way of creating an array
would be used. What do you people think? Is this worth yet another
RCR?,
nikolai
Array.new{|array| block }
Here, array is the newly created array, and the block will initialize it
as it sees fit, e.g,
ary = Array.new{ |ary| 5.times{ |i| ary << i } },
which is a stupid way of doing it (Array.new(5){ |i| i } would do), but
it shows the idea. I often find myself writing methods that fit the
following template:
def a
ary = []
... construct ary by adding elements to it ...
ary
end
Lately I've been using the Kernel.returning method from Rails to write
it as
def a
returning ary = [] do
... construct ary by adding elements to it ...
end
end
which works quite well, but I think that I'd rather be writing it as
def a
Array.new do |ary|
... construct ary by adding elements to it ...
end
end
Array.new(size){ |index| block } would have to be changed so that size
is optional, and if not given, then this new way of creating an array
would be used. What do you people think? Is this worth yet another
RCR?,
nikolai