Last active
January 4, 2016 16:29
-
-
Save chrisseaton/8648164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(*a) | |
# Isn't the * below redundant? Doesn't it cast a to an Array if it isn't one already? | |
# But if you remove it the behaviour changes - how come? | |
yield *a | |
end | |
puts foo(1, 2) { |a| a } | |
# MRI, JRuby, prints 1; Truffle prints 1 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this will make the example a little more clear:
In the first yield we are passing a single argument which is an array. Since the block accepts a single arg you get the whole array.
In the second yield you splatted the array back as two arguments to the yield. Since the block accepts a single arg you get the first element.
block argument passing is easily the most confusing part of Ruby...