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?
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?
On Jan 11, 2008, at 6:13 PM, Roger P. 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/.
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 1/11/08, John J. [email protected] wrote:
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:
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
On Jan 11, 2008, at 7:13 PM, Roger P. 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?
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’
=> true12.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 W.
On Jan 11, 2008, at 5:13 PM, Roger P. wrote:
I would have expected this to return a collected array of the return
value of each block. Odd?
i often define this
class Numeric
def of &block
Array.new(to_i).map &block
end
end
list = 42.of{ those }
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 W.
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 }
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)
?
On Jan 12, 2008, at 2:07 PM, Roger P. 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 }
i’ve RCR’d it a few times, but i think the migrations have lost it
the mess… ;-(
On Jan 12, 2008, at 2:28 PM, Sebastian H. wrote:
Why not
Array.new(to_i, &block)
just because i’ve been doing it since 1.6.8
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs