DEV Community

olistik
olistik

Posted on

6

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

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay