[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

.times loop returns the number?

Roger Pack

1/12/2008 12:14:00 AM

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-....

9 Answers

John Joyce

1/12/2008 12:58:00 AM

0


On Jan 11, 2008, at 6:13 PM, Roger Pack wrote:

> 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-....
>
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.

Rick DeNatale

1/12/2008 1:09:00 AM

0

On 1/11/08, John Joyce <dangerwillrobinsondanger@gmail.com> 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"
> end
>
> the 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
------------------------------------------------------------------------


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Gary Wright

1/12/2008 5:58:00 AM

0


On Jan 11, 2008, at 7:13 PM, Roger Pack wrote:

> 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?

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

Roger Pack

1/12/2008 3:22:00 PM

0

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-....

ara.t.howard

1/12/2008 9:03:00 PM

0


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?

i often define this

class Numeric
def of &block
Array.new(to_i).map &block
end
end

list = 42.of{ those }

a @ http://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



Roger Pack

1/12/2008 9:08:00 PM

0

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
> end
>
> list = 42.of{ those }
>
> a @ http://codeforp...

--
Posted via http://www.ruby-....

ara.t.howard

1/12/2008 9:28:00 PM

0


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
>> end
>>
>> list = 42.of{ those }

i've RCR'd it a few times, but i think the migrations have lost it
the mess... ;-(

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Sebastian Hungerecker

1/12/2008 9:29:00 PM

0

ara.t.howard wrote:
> i often define this
>
> class Numeric
> =A0 =A0def of &block
> =A0 =A0 =A0Array.new(to_i).map &block
> =A0 =A0end
> end

Why not
Array.new(to_i, &block)
?

=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

ara.t.howard

1/13/2008 5:25:00 AM

0


On Jan 12, 2008, at 2:28 PM, Sebastian Hungerecker wrote:

> Why not
> Array.new(to_i, &block)

just because i've been doing it since 1.6.8 ;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama