Issue #10930 has been updated by Daniel P. Clark.


The behavior of the splat operator as used in methods is like removing [ ] from and array to use as parameters.

~~~ruby
eval("xaz = [1,2,3]; def biz(a,b,c) puts a, b, c; end; biz(*xaz)")
# 1
# 2
# 3
# => nil
~~~

So intuitively it would make sense that it would remove them for interpolation from a string.

~~~ruby
zax = [1,2,3]
# => [1, 2, 3] 

eval("def zib(a,b,c) puts a, b, c; end; zib(#{*zax})")
# SyntaxError: (irb):23: syntax error, unexpected tSTRING_DEND, expecting '='
# eval("def zib(a,b,c) puts a, b, c; end; zib(#{*zax})")
#                                                    ^
# (irb):23: unterminated string meets end of file
~~~

It seems odd to only work this way in the method argument call when it can be used the same way.

~~~ruby
eval("def zab(a,b,c) puts a, b, c; end; zab(*#{zax})")
# 1
# 2
# 3
# => nil
~~~



----------------------------------------
Feature #10930: Allow splat operator to work for string interpolation
https://bugs.ruby-lang.org/issues/10930#change-51753

* Author: Daniel P. Clark
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
Currently when you use the splat operator in a method it pulls the items out of the array for method parameters.

~~~ruby
def foo(a,b,c)
  puts "#{a}, #{b}, #{c}"
end

bar = [1,2,3]
foo(*bar)
# => "1, 2, 3"
~~~

So I would expect to be able to do "#{*bar}" and get either "1, 2, 3" or "1,2,3".  But when attempting this I get.

~~~ruby
baz = [1,2,3]

"#{*baz}"

# SyntaxError: (irb):53: syntax error, unexpected tSTRING_DEND, expecting '='
# "#{*baz}"
#         ^
# (irb):53: unterminated string meets end of file

"#{*[1,2,3]}"

# SyntaxError: (irb):54: syntax error, unexpected tSTRING_DEND, expecting :: or '[' or '.'
# "#{*[1,2,3]}"
#             ^
# (irb):54: unterminated string meets end of file
~~~

This doesn't work on any of the Ruby versions available 1.8 through 2.2.1.  They each produce the same error.

I propose allowing the splat operator within string interpolation to work the same as [1,2,3].join(',')

~~~ruby
fiz = [1,2,3]

"#{*fiz}"
# => "1,2,3"

"#{*[1,2,3]}"
# => "1,2,3"
~~~



-- 
https://bugs.ruby-lang.org/