12.times do |month_ahead|
end
returns 12
I would have expected this to return a collected array of the return
value of each block. Odd?
···
--
Posted via http://www.ruby-forum.com/.
12.times do |month_ahead|
end
returns 12
I would have expected this to return a collected array of the return
value of each block. Odd?
--
Posted via http://www.ruby-forum.com/.
Not odd at all.
x.times do |x|
code_here_using_x
end
In this iterating construct the current value passed in | | is the current value of x.
other than that,
Ruby blocks always return the last value returned in the block.
if you simply do this:
x.times do |x|
code_here_using_x
puts "cat"
end
the block will return "cat"
The iterating constructs in Ruby are not always used to return a value as in C-like languages.
They simply perform the code in the block a number of times.
This includes powerful and flexible concepts such iterating over collections (arrays, hashes, etc...)
a = Array.new
a.each do |element|
some_function( element )
end
The main idea on iterators in ruby is the iterating!
But do keep in mind the simple convenience that Ruby code blocks implicitly return the last value in the block.
a.each do |element|
some_function( element )
46
end
returns 46.
Sometimes that is useful and convenient.
On Jan 11, 2008, at 6:13 PM, Roger Pack wrote:
12.times do |month_ahead|
endreturns 12
I would have expected this to return a collected array of the return
value of each block. Odd?
-- Posted via http://www.ruby-forum.com/\.
12.times do |month_ahead|
endreturns 12
I would have expected this to return a collected array of the return
value of each block. Odd?
To get a collected array:
>> (0...12).collect { |x| x * 2 }
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Or via enumerators in Ruby 1.8:
>> require 'enumerator'
=> true
>> 12.enum_for(:times).collect { |x| x * 2 }
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Or via Ruby 1.9's new enumerator behavior:
>> 12.times.collect { |x| x * 2 }
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Gary Wright
On Jan 11, 2008, at 7:13 PM, Roger Pack wrote:
i often define this
class Numeric
def of &block
Array.new(to_i).map &block
end
end
list = 42.of{ those }
On Jan 11, 2008, at 5:13 PM, Roger Pack wrote:
I would have expected this to return a collected array of the return
value of each block. Odd?
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama
Ruby blocks always return the last value returned in the block.
true but..
if you simply do this:
x.times do |x|
code_here_using_x
puts "cat"
endthe block will return "cat"
But the whole expression won't.
irb(main):001:0> irb(main):001:0> 5.times {|i| "a"}
=> 5
irb(main):002:0> 5.times {|i| puts "cat"}
cat
cat
cat
cat
cat
=> 5
And puts "cat" doesn't return "cat"
irb(main):004:0> puts "cat"
cat
=> nil
On the other hand Integer#times is documented to return the integer:
qri Integer#times
---------------------------------------------------------- Integer#times
int.times {|i| block } => int
On 1/11/08, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
------------------------------------------------------------------------
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Thank you!
Or via Ruby 1.9's new enumerator behavior:
>> 12.times.collect { |x| x * 2 }
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]Gary Wright
--
Posted via http://www.ruby-forum.com/\.
That should be in the standard lib for coolness and ruby-ish-ness
class Numeric
def of &block
Array.new(to_i).map &block
end
endlist = 42.of{ those }
--
Posted via http://www.ruby-forum.com/\.
ara.t.howard wrote:
i often define this
class Numeric
def of &block
Array.new(to_i).map &block
end
end
Why not
Array.new(to_i, &block)
?
--
Jabber: sepp2k@jabber.org
ICQ: 205544826
i've RCR'd it a few times, but i think the migrations have lost it the mess... ;-(
On Jan 12, 2008, at 2:07 PM, Roger Pack wrote:
That should be in the standard lib for coolness and ruby-ish-ness
class Numeric
def of &block
Array.new(to_i).map &block
end
endlist = 42.of{ those }
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama
just because i've been doing it since 1.6.8
On Jan 12, 2008, at 2:28 PM, Sebastian Hungerecker wrote:
Why not
Array.new(to_i, &block)
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama