1
class MyClass def fun a = 2 b = 5 yield(a,b) end def self.fun2 puts self end end m = 1 n = 2 mine = MyClass.new mine.fun {|m| puts "m = #{m} n = #{n}"} 

Here I deliberately do not match the number of parameters for yield, and the output is

test.rb:16: warning: multiple values for a block parameter (2 for 1) from test.rb:5 m = 25 n = 2 

Where does this 25 come from?

2
  • What version of ruby are you using? Commented Mar 27, 2013 at 2:31
  • sorry it's actually 1.8.7... I have 1.9.2 on my linux and I'm currently on my mac... Commented Mar 27, 2013 at 2:44

2 Answers 2

2

I suspect you to be in 1.8.x.

In which case, m will be [a, b]. Which is equivalent to [2, 5].

[2, 5].to_s # => "25" "m = #{[2, 5]} n = #{2}" # => "m = 25 n = 2" 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice guess on the version number.
0

The 25 comes from a and b passed to m as an array.

When you puts it, that's the output you get in Ruby 1.8.

You could also try doing puts "m = #{m.inspect}...." or puts "m = #{m.class.to_s}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.