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:
1 2 3 4 5 6 7 8 | <span class="token comment">#!/usr/bin/env ruby</span> <span class="token comment"># This is</span> <span class="token comment"># multi-line</span> <span class="token comment"># comments</span> puts <span class="token string">"Running!"</span> |
The above writing is absolutely correct, but we can rewrite the above comment in a better way like:
1 2 3 4 5 6 7 8 9 10 | <span class="token comment">#!/usr/bin/env ruby</span> <span class="token comment">=begin This is multi-line comments =end</span> puts <span class="token string">"Running!"</span> |
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:
1 2 3 4 5 6 7 | <span class="token comment">#!/usr/bin/env ruby</span> puts <span class="token string">"Hello"</span> __END__ <span class="token constant">Nh</span> ững đoạn code nằm phía sau __END__ đều được xem như là comment và không được biên dịch <span class="token punctuation">.</span> |
Another option to be able to comment on multiple lines is to use the docstring.
1 2 3 4 5 6 7 8 | <span class="token comment">#!/usr/bin/env ruby</span> <span class="token operator"><</span> <span class="token operator"><</span> <span class="token operator">-</span> <span class="token constant">DOC</span> Đoạn này dùng để test chức năng comment trên nhiều line sử dụng docstring <span class="token constant">DOC</span> puts <span class="token string">"docstring"</span> |
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 :
1 2 3 4 5 | <span class="token comment">#!/usr/bin/env ruby</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">hello</span></span> puts <span class="token string">"hello world"</span> <span class="token keyword">end</span> |
Before turning on warn_indent :
1 2 | NO OUTPUT |
After enabling warn_indent :
1 2 | warn-ident.rb:5: warning: mismatched indentations at 'end' with 'def' at 3 |
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
1 2 | # frozen_string_literal: true |
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
1 2 3 4 5 | <span class="token comment">#!/usr/bin/env ruby</span> puts <span class="token string">"hello"</span> <span class="token punctuation">.</span> object_id <span class="token comment"># 60</span> puts <span class="token string">"hello"</span> <span class="token punctuation">.</span> object_id <span class="token comment"># 80</span> |
When using frozen_string_literal
1 2 3 4 5 6 7 | <span class="token comment">#!/usr/bin/env ruby</span> <span class="token comment"># frozen_string_literal: true</span> puts <span class="token string">"hello"</span> <span class="token punctuation">.</span> object_id <span class="token comment"># 60</span> puts <span class="token string">"hello"</span> <span class="token punctuation">.</span> object_id <span class="token comment"># 60</span> |
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
1 2 | <span class="token comment"># encoding: ISO-8859-1</span> |
Or can be written in the following format:
1 2 3 4 5 6 | <span class="token comment"># -*- coding: UTF-8 -*-</span> <span class="token keyword">or</span> <span class="token comment"># coding: UTF-8</span> |
Take a look at the code below:
1 2 3 4 | <span class="token comment"># -*- coding: ASCII -*-</span> puts <span class="token string">"Édsf"</span> |
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:
1 2 3 4 | ➜ Desktop ruby encoding.rb encoding.rb:3: invalid multibyte char (US-ASCII) encoding.rb:3: invalid multibyte char (US-ASCII) |
Let’s try to convert the encoding to UTF-8:
1 2 3 4 | <span class="token comment"># -*- coding: UTF-8 -*-</span> puts <span class="token string">"Édsf"</span> |
Results after running the file
1 2 3 | ➜ Desktop ruby encoding.rb Édsf |
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!
Refer: