Ruby is a wonderful language with many interesting details that you might not even see before.
In this post I collected several such details in the list.
1. Heredoc + Method
If you have any text data that you want to embed in the program, you can use “heredoc”. As a result, you get a string, like this:
input = <<-IN ULL RRDDD LURDL IN
But in addition to this, you can use post-processing, for example, to divide the text by words. Ruby allows you to do this:
')
input = <<-IN.split ULL RRDDD LURDL IN
And in Ruby 2.3 a “wavy” heredoc appeared << ~. It removes all spaces used for indentation, a common problem of using heredoc for text.
2. Calling a double colon
Maybe someone will come in handy.
"abc"::size
3. Puts with multiple arguments
Pretty simple thing, but often useful.
puts 1,2,3 1 2 3
4. Infinite taking by index
Just an example:
words = ["abc", "foo"] words[0][0][0][0][0]
Of course, this works because [] is just a method, and simply returns the first character of a string, which is also a string.
5. Destructuring block arguments
Want to get rid of a pair of local variables? You will like it.
a = [[1,2],[3,4]] a.each do |(first, last)|
Equivalent to:
a = [[1,2],[3,4]] a.each do |sub_array| first, last = sub_array
But saves a line of code.
6. Special global variables
When using regexes with brackets, special constants $ 1 for the first group, $ 2 for the second, etc. will be defined. It is worth remembering that they do not behave like ordinary variables: they have a different scope (common to the method and the flow) and cannot be reassigned. A list of them can be seen
here .
$1 = 'test'
7. Join operator with string
The operator of accession (<<) does not work with a string as expected, if you pass a number as an argument:
"" << 97
It interprets the number as an ASCII code. Regular way for the same:
97.chr
8. Character literals
Not sure if this is actually useful to someone.
?a "a" ?aa
You can write in the comments what you think about it.
9. RbConfig module
RbConfig is an undocumented module containing some info about your Ruby configuration. For example, RbConfig :: CONFIG contains the interpreter compilation flags, version, operating system.
RbConfig.constants
10. Spaces, spaces everywhere!
You can put as many spaces as you like between the called method and the receiver.
a = [1,2,3] a [0] a .size a . empty?
Yeah, this is valid Ruby syntax.
11. Infinite inheritance of constants
String::String::Fixnum::Float
Inquiring minds have guessed right away: all the top-level constants (defined not inside any class) are contained in the class Object, and can be called by any class inherited from Object. To better understand, look at Objectb.constants in irb.
12. Sequential join operator
The << operator can be chained:
a = [] a << 1 << 2 << 3
13. BEGIN and END
Two keywords that are used quite rarely. I think that they came from the world of Perl / Unix, where the usual action is to write short scripts to handle the output of other programs. How it works:
puts 123 BEGIN { puts "Program starting..." }
This code will print “Program starting ...” BEFORE “123”. My reader suggests that this is useful when you need to change the path to RUBYLIB for 'require', because it will be guaranteed to be executed before all "require". It can also be useful for setting $ VERBOSE and other environmental constants.
14. ChoZaHren
I do not even know how and why it appeared in the language, and I advise you to handle it more carefully. Few people know about this feature, and it is very difficult to understand. But forewarned is forearmed:
if (condition1)..(condition2)
The idea is that if the first condition is true, then the invisible lever switches, and from that moment the condition will be fulfilled until the second condition also becomes true. Example:
(1..20).each do |i| puts i if (i == 3)..(i == 15) end
It prints all numbers from 3 to 15, but if 15 is skipped in a cycle, it will continue to print.
15. Keyword redo
Another rarely used keyword that allows you to repeat an iteration in a loop.
10.times do |n| puts n redo end
Just do not forget to put it under the condition, otherwise you get an infinite loop. So with this feature you have to be careful.