read-ruby icon indicating copy to clipboard operation
read-ruby copied to clipboard

Clarification for Message Arguments + Splat (III.A.1 - splatting)

Open michaeledgar opened this issue 14 years ago • 0 comments

The section on applying the splat operator in an argument list misses one important point. It says:

This technique works by sending :to_a to the prefixed object, so any object that responds to this message with an Array can be used in the same fashion.

However, it is important to note that splatting any object will work: if the prefixed object doesn't respond to #to_a, it is wrapped into a one-element array and then splatted, making the splat a no-op. The code for the splatarray bytecode in 1.9 is as follows:

VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a");
if (NIL_P(tmp)) {
tmp = rb_ary_new3(1, ary);
}
obj = tmp;

michaeledgar avatar Feb 06 '11 21:02 michaeledgar