Have you used all of the Comment feature in Ruby?

Tram Ho

We are all too familiar with using comments in ruby ​​code and its purpose is often to make notes or explain more about the purpose of the code and the comment will not be interpreted. So comments can do anything other than just simple comments. Let’s find out more.

Multi-line comments

Often times when we need to comment on multiple lines, we simply add a # in front of each line. Eg:

The above writing is absolutely correct, but we can rewrite the above comment in a better way like:

The code above is easy to read when looking at it especially when we have more lines. There is also another way that you can write many comments at the end of the file as follows:

Another option to be able to comment on multiple lines is to use the docstring.

Indentation warning comment

You can enable the warning indent feature by adding the # warn_indent: true comment at the top of the ruby ​​file you need to enable. When we add the above comment, if there is an error about the indent, there will be a warning message displayed at the console.

We have the file warn_ident.rb :

Before turning on warn_indent :

After enabling warn_indent :

Frozen String Literal Comment

In Ruby, by default all strings are mutable and every time a new string is created the same creates an object. And that is also the main reason leading to a lot of memory in the ruby ​​language.

And to fix this problem simply add the following comment at the beginning of the file

When adding the above comment, string objects with the same value are initialized only once.

And the above comment will help reduce 20% of memory usage.

When not using frozen_string_literal

When using frozen_string_literal

As in the above example, when we are not using frozen_string_literal , even though 2 strings have the same value, there are 2 objects created. In the following example, we use frozen_string_literal: true , only one object will be created. So when working with strings , people should also consider adding frozen_string_literal: true comments at the beginning of the file to reduce memory footprint.

File Encoding Comment

In Ruby 2.0 the default encoding for the string will be UTF-8. In lower version like 1.9.x, the default encoding will be US-ASCII. Sometimes we want to use an encoding other than the default encoding

Or can be written in the following format:

Take a look at the code below:

When there are many comments about the change default encoding in the same file, the compiler will only receive the first comment and the remaining comments will be discarded.

The above code will shoot error because “É” is not a character in the ASCII table, the error will be as follows:

Let’s try to convert the encoding to UTF-8:

Results after running the file

You can see a list of encoding supports by ruby here

Above are some other features of comments in Ruby, hope people can understand more about the comment. Thank you for reading (bow) (bow)

Happy Coding!


Share the news now

Source : Viblo