Ruby splat and << operators -
i want this:
a << *b
but happens in irb:
1.9.3p327 :020 > => [1, 2, 3, 4] 1.9.3p327 :021 > b => [5, 6, 7] 1.9.3p327 :022 > << *b syntaxerror: (irb):22: syntax error, unexpected tstar << *b ^
am missing something?
look reason here :
= [1, 2, 3, 4] b = [5, 6, 7] p a.<<(*b) # ~> -:3:in `<<': wrong number of arguments (3 1) (argumenterror) # ~> -:3:in `<main>'
<<
method expects 1 argument.so below splat(*
) operator,which create 5,6,7
,which <<
method not expect,rather expects 1 object. design of ruby don't allowing *
before b
.
= [1, 2, 3, 4] b = [5, 6, 7] p << * # ~> -:3: syntax error, unexpected * = [1, 2, 3, 4] b = [5, 6, 7] p << *b # ~> -:3: syntax error, unexpected * # ~> p << *b # ~> ^
that why 2 legitimate errors :
wrong number of arguments (3 1) (argumenterror)
syntax error, unexpected *
probably can use -
= [1, 2, 3, 4] b = [5, 6, 7] p a.push(*b) # >> [1, 2, 3, 4, 5, 6, 7]
Comments
Post a Comment