Forum: Ruby method &block question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
=?ISO-8859-1?Q?Simon_Kr=F6ger?= (Guest)
on 2006-01-04 22:30
(Received via mailing list)
Hi,

could someone please put a finger on the point i'm missing?

source:
------------------------------------
def test *a
   p a
end

[[1,2], [3,4]].each &method(:test)
puts "-----"
[[1,2], [3,4]].each &method(:p)
puts "-----"
[[1,2], [3,4]].each {|e| p e}

------------------------------------

output:
------------------------------------
[1, 2]
[3, 4]
-----
1
2
3
4
-----
[1, 2]
[3, 4]
------------------------------------

ruby 1.8.2 (2004-12-25) [i386-mswin32]

Why is the second version interating over each single element?

cheers

Simon
=?ISO-8859-1?Q?Simon_Kr=F6ger?= (Guest)
on 2006-01-04 22:39
(Received via mailing list)
Simon Kröger wrote:


> [...]
> Why is the second version interating over each single element?

It does not, the array is simply splashed when passed to 'p', so p
sees two parameters and print them...

Sorry for the noise.

cheers

Simon
This topic is locked and can not be replied to.