DEV Community

olistik
olistik

Posted on

Little details are important

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
Enter fullscreen mode Exit fullscreen mode
with_tags('Hello' 'World')
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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). 😳

Latest comments (0)