On Tue, Apr 5, 2011 at 5:38 AM, Leo M. [email protected] wrote:
array = []
e = i.each {|i| puts (“/R/blackout/black”+i.to_s+“\n”)}
Here, you are sending the path to standard output (that’s what puts
does,
that’s what it means to print data). But you are more interested in
using
the result that you are calculating rather than printing it. So you want
something that will collect those values into an array:
e = i.collect { |i| “/R/blackout/black”+i.to_s+“\n” }
(collect is another name for map)
alternatively, you could iterate over each element and add it to the
array
i.each { |i| array << “/R/blackout/black”+i.to_s+“\n” }
Some quick advice, you probably don’t want a newline in your filename,
so
“/R/blackout/black”+i.to_s+“\n”
should probably be
“/R/blackout/black”+i.to_s
And it is also more common to use interpolation, because it looks nicer
(IMO), is more efficient, and requires fewer characters. It’s also
easier
for my brain to comprehend at a glance:
“/R/blackout/black#{i}”
array.push e
puts array.size # => 1
Something that might be helpful is the p method.
p prints out an inspected version of your object
i = 1…87
I’ll omit the output of the puts statements
e = i.each { |i| puts “/R/blackout/black#{i}” }
p e
>> 1…87
Here, we can see that #each returns the object it was iterating over,
the
range from 1 to 87, not the new array, and certainly not what we sent to
stdout.