Today I learned a detail about the way Ruby handles strings that made me waste 15 minutes.
def with_tags(*tags)
tags.map {|tag| "[#{tag}]"}.join
end
with_tags('Hello' 'World')
Expecting the string [Hello][World]
but obtaining [HelloWorld]
instead.
Why?
Well, because I forgot to put a comma between the arguments (admit if you fell in this trap like me 😅) and it turns out that, in Ruby, two adjacent strings, like 'Hello' 'World'
, get merged into one.
In order to make the code work as expected I simply had to add a comma:
with_tags('Hello', 'World')
So, the trade has been: 1 comma for 15 minutes.
And yes, I even searched for issues with splat arguments in Ruby 2.1 (the version of the project I'm currently on). 😳
Top comments (0)