“Literal interpolation detected” by Rubocop.

RuboCop Logo

One of our recent assignments was exercise 7 from the book Learn Ruby the Hard Way

Here’s what that code looks like:

Also we’re using a program called rubocop. Here’s a short intro to Rubocop.

And when we run rubocop we get this offence:

1___RuboCop_results__22_files_inspected__1_offense_detected__ruby_The reason for this offense is interpolated word ‘snow’ inside curly brackets inside a string on line 2. This is absolutely unnecessary. We could just type:

puts "Its fleece was white as snow."

We use interpolation if we want to insert a previously defined variable.

For example we can define a variable ‘description’, and then interpolate that into our string.

description = 'snow'  
puts "Its fleece was white as #{'description'}."   

=> Its fleece was white as snow.

In this case we’re referencing a variable from outside of our string.

Here’s a code that checks for such errors:

Now we can either correct our code, or edit .rubocop.yml file to ignore this error. After all we need to copy exercise exactly.

In order to make rubocop ignore this offense we need to add following lines to .rubocop.yml file:

LiteralInInterpolation:
  Enabled: false

Let’s check our code now.

1___RuboCop_results__22_files_inspected__1_offense_detected__ruby_

Much better.

As we can see in this example, there are a lot of ways to customize rubocop.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s