About two months have passed since the release of the
ruby programming language interpreter
version 1.9 . The second odd number here, like many other open source projects, means an unstable, experimental version, a way to try out
“wild and weird ideas” . Below is a brief overview of these experimental changes from the point of view of a person who is familiar with previous versions. The choice for review is not limited by their importance (a very important thing is improved support for Unicode, for example, is skipped), but by the interest they aroused in the author.
Changes in syntax and semantics
Notes: the word block below refers to both Proc and lambda, since subtle differences do not play a role in this brief overview, using “ =>
” is not part of the language, but means the result that the last construct returned
New way to write blocks
fun = ->(a, b){ a + b }
instead
fun = lambda{|a,b| a + b }
The feature is marked as “VERY EXPERIMENTAL”, one of the most controversial ideas, and, indeed, when used in large volumes, it can produce hard-to-read code.
New way to call blocks
fun.(2,5)
In addition to the two old ways:
fun.call(2,5) # 1
fun[2,5] # 2
The method is shorter than 1 and more obvious than 2, and, in my opinion, it is worth including in the stable version.
Block parameters
The block parameters are now local (for me, probably the most frequent rake in Ruby). Now this (code for 1.8) does not happen again:
i = 42
10.times{|i| puts i }
i
=> 10
The value of
i
will remain 42. This change means incompatibility with 1.8, but most likely it will be adopted in a stable version.
')
New methods
with_index
Enumerator
has a very useful method
with_index
, which allows converting it to another
Enumerator
which also performs an action, but also passes an additional argument, an index. For example, the following code selects every third element from an array:
[1,3,4,6,5,6].select.with_index{|v,i| (i+1)%3 == 0 }
=> [4, 6]
The idea agrees well with the general ideology of ruby, eliminates the need for special methods like
each_with_index
. This eliminates the need for an additional loop variable and makes the code easier and safer.
Trivia for Time and Integer
Seven new ways were added to Time, which correspond very well to the ruby ​​style. Try to guess what - for those who know Ruby should not be difficult.
Time.now.sunday?
=> false
Time.now.thursday?
=> true
Similarly to the Integer added methods
odd?
and
even?
1.odd?
=> true
3.even?
=> false
Logical continuation for a language containing methods
nil?
and
zero?
.
Symbol # to_proc
Symbol by default works now converted to block. Now you can write:
["one", "two", "three"].map(&:capitalize)
=> ["One", "Two", "Three"]
This, however, could be done in older versions, but with additional library code.
I repeat that this was a review of the
experimental version of ruby, the reviews on which will largely determine version 2.0. It is planned that it will be faster, smaller, safer, easier to integrate into other applications.
Changes have been made much more, here you can find a
detailed description .