11

Today I was surprised to find ruby automatically find the values of an array given as a block parameter.

For example:

foo = "foo"
bar = "bar"
p foo.chars.zip(bar.chars).map { |pair| pair }.first #=> ["f", "b"]
p foo.chars.zip(bar.chars).map { |a, b| "#{a},#{b}" }.first #=> "f,b"
p foo.chars.zip(bar.chars).map { |a, b,c| "#{a},#{b},#{c}" }.first #=> "f,b,"

I would have expected the last two examples to give some sort of error.

  1. Is this an example of a more general concept in ruby?
  2. I don't think my wording at the start of my question is correct, what do I call what is happening here?
1
  • 1
    It's similar to (maybe the same as?) what happens with parallel assignment of variables from an array. See stackoverflow.com/questions/14913765/… Commented Jan 26, 2017 at 2:55

3 Answers 3

12

Ruby block are quirky like that.

The rule is like this, if a block takes more than one argument and it is yielded a single object that responds to to_ary then that object is expanded. This makes yielding an array versus yielding a tuple seem to behave the same way for blocks that take two or more arguments.

yield [a,b] versus yield a,b do differ though when the block takes one argument only or when the block takes a variable number of arguments.

Let me demonstrate both of that

def yield_tuple
  yield 1, 2, 3
end

yield_tuple { |*a| p a }
yield_tuple { |a| p [a] }
yield_tuple { |a, b| p [a, b] }
yield_tuple { |a, b, c| p [a, b, c] }
yield_tuple { |a, b, c, d| p [a, b, c, d] } 

prints

[1, 2, 3]
[1] 
[1, 2]
[1, 2, 3]
[1, 2, 3, nil]

Whereas

def yield_array
  yield [1,2,3]
end

yield_array { |*a| p a }
yield_array { |a| p [a] }
yield_array { |a, b| p [a, b] }
yield_array { |a, b, c| p [a, b, c] }
yield_array { |a, b, c, d| p [a, b, c, d] }

prints

[[1, 2, 3]]
[[1, 2, 3]] 
[1, 2] # array expansion makes it look like a tuple
[1, 2, 3] # array expansion makes it look like a tuple
[1, 2, 3, nil] # array expansion makes it look like a tuple

And finally to show that everything in Ruby uses duck-typing

class A
  def to_ary
    [1,2,3]
  end
end

def yield_arrayish
  yield A.new
end

yield_arrayish { |*a| p a }
yield_arrayish { |a| p [a] }
yield_arrayish { |a, b| p [a, b] }
yield_arrayish { |a, b, c| p [a, b, c] }
yield_arrayish { |a, b, c, d| p [a, b, c, d] }

prints

[#<A:0x007fc3c2969190>]
[#<A:0x007fc3c2969050>]
[1, 2] # array expansion makes it look like a tuple
[1, 2, 3] # array expansion makes it look like a tuple
[1, 2, 3, nil] # array expansion makes it look like a tuple

PS, the same array expansion behavior applies for proc closures which behave like blocks, whereas lambda closures behave like methods.

Sign up to request clarification or add additional context in comments.

Comments

9

Ruby's block mechanics have a quirk to them, that is if you're iterating over something that contains arrays you can expand them out into different variables:

[ %w[ a b ], %w[ c d ] ].each do |a, b|
  puts 'a=%s b=%s' % [ a, b ]
end

This pattern is very useful when using Hash#each and you want to break out the key and value parts of the pair: each { |k,v| ... } is very common in Ruby code.

If your block takes more than one argument and the element being iterated is an array then it switches how the arguments are interpreted. You can always force-expand:

[ %w[ a b ], %w[ c d ] ].each do |(a, b)|
  puts 'a=%s b=%s' % [ a, b ]
end

That's useful for cases where things are more complex:

[ %w[ a b ], %w[ c d ] ].each_with_index do |(a, b), i|
  puts 'a=%s b=%s @ %d' % [ a, b, i ]
end

Since in this case it's iterating over an array and another element that's tacked on, so each item is actually a tuple of the form %w[ a b ], 0 internally, which will be converted to an array if your block only accepts one argument.

This is much the same principle you can use when defining variables:

a, b = %w[ a b ]
a
# => 'a'
b
# => 'b'

That actually assigns independent values to a and b. Contrast with:

a, b = [ %w[ a b ] ]
a
# => [ 'a', 'b' ]
b
# => nil

11 Comments

So is the correct term for what's going on here is: "Ruby is expanding each array into the variables a, b, or c inside the block passed to the map instance method"?
@mbigras most languages refer to that as 'destructuring', ES6 now has it, LISP (cs.cmu.edu/Groups/AI/html/cltl/clm/node252.html), clojure (blog.jayfields.com/2010/07/clojure-destructuring.html) and many many more.
There is a difference between yield [a, b] and yield a, b though in how they behave when the block expects one argument only.
The elements are not [%w[a b], 0] internally, see [ %w[ a b ], %w[ c d ] ].each { |a| p a } which prints just the elements without indices. Internally each_with_index yields a tuple to the block not an array. So it uses the equivalent of yield each, index and not yield [each, index]
Yeah, I see what you're saying there. I like your answer, but you should really move the output closer to the examples, it be considerably easier to follow. I'm also trying to describe the behaviour here in a more abstract way and the subtelty between tuple and array is hard to express. I've updated the phrasing again.
|
4

I would have expected the last two examples to give some sort of error.

It does in fact work that way if you pass a proc from a method. Yielding to such a proc is much stricter – it checks its arity and doesn't attempt to convert an array argument to an argument list:

def m(a, b)
  "#{a}-#{b}"
end

['a', 'b', 'c'].zip([0, 1, 2]).map(&method(:m))
#=> wrong number of arguments (given 1, expected 2) (ArgumentError)

This is because zip creates an array (of arrays) and map just yields each element, i.e.

yield ['a', 0]
yield ['b', 1]
yield ['c', 2]

each_with_index on the other hand works:

['a', 'b', 'c'].each_with_index.map(&method(:m))
#=> ["a-0", "b-1", "c-2"]

because it yields two separate values, the element and its index, i.e.

yield 'a', 0
yield 'b', 1
yield 'c', 2

1 Comment

Good point. You could also use a lambda to demonstrate that. [1,2,3].each &lambda { |a,b| } fails but [1,2,3].each &proc { |a,b| } works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.