Sig
Sig

Reputation: 5952

splatting an array inside a string

Starting from and array of numbers such as

a = [1,2,3]

I need to get the following string

"values in (1,2,3)"

If I try a simple interpolation

"values in (#{a})"  => "values in ([1,2,3])"

I could use gsub to remove [ and ] but I'd rather find a cleaner way. Any suggestions?

Thanks

Upvotes: 1

Views: 140

Answers (3)

hirolau
hirolau

Reputation: 13911

An alternative:

a, bracs = [1,2,3], %w{[] ()}

p "values in #{a}".tr(*bracs) #=> "values in (1, 2, 3)"

Upvotes: 0

sawa
sawa

Reputation: 168139

Try this:

"values in (#{a.join(",")})"

Upvotes: 3

Arup Rakshit
Arup Rakshit

Reputation: 118271

Here is one more :

a = [1,2,3]
p "values in (#{a* ","})" # => "values in (1,2,3)"

Upvotes: 1

Related Questions