Project

General

Profile

Actions

Feature #10930

closed

Allow splat operator to work for string interpolation

Added by danielpclark (Daniel P. Clark) almost 10 years ago. Updated almost 9 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:68394]

Description

Currently when you use the splat operator in a method it pulls the items out of the array for method parameters.

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.

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(',')

fiz = [1,2,3]

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

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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0