Method &block question

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

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs