Emotional review of Ruby 1.9

    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” . The following is a brief overview of these experimental changes from the point of view of a person well acquainted with previous versions. The choice for the review is limited not by their importance (a very important thing is the improved support for Unicode, for example, is missing), but by the interest that they aroused in the author.

    Changes in syntax and semantics


    Notes: the 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 returned by the last construct

    A new way to write blocks


    fun = ->(a, b){ a + b }

    instead, Opportunity is marked as “VERY EXPERIMENTAL”, one of the most controversial ideas, and, indeed, when used in large volumes, it can give difficult to read code.

    fun = lambda{|a,b| a + b }



    A new way to call blocks


    fun.(2,5)

    in addition to the two old methods: The method is shorter than 1 and more obvious than 2, and, in my opinion, it is worth including in a stable version.
    fun.call(2,5) # 1
    fun[2,5] # 2



    Block Parameters


    The parameters of the blocks are now local (for me, probably, the most frequent rake in rubles). Now this (code for 1.8) will not be repeated: The value will remain 42. This change means incompatibility with 1.8, but most likely will be accepted in a stable version.
    i = 42
    10.times{|i| puts i }
    i
    => 10

    i

    New methods


    with_index


    A Enumeratorvery useful method has appeared with_index, which allows you to convert it to another one Enumeratorthat also performs an action, but also passes an additional argument - an index. For example, the following code selects every third element from an array: The idea agrees well with the general ruby ​​ideology, eliminates the need for special methods like . This eliminates the need for an additional loop variable and makes the code simpler and safer.
    [1,3,4,6,5,6].select.with_index{|v,i| (i+1)%3 == 0 }
    => [4, 6]

    each_with_index

    Trivia for Time and Integer


    Time added seven new methods that fit very well with the ruby ​​style. Try to guess which ones - for those who know, ruby ​​should not be difficult. Similarly, methods and logical extensions for a language containing methods and were added to Integer .
    Time.now.sunday?
    => false
    Time.now.thursday?
    => true

    odd?even?
    1.odd?
    => true
    3.even?
    => false

    nil?zero?

    Symbol # to_proc


    Symbol works by default now converts to block. Now you can write: 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, reviews on which will largely determine version 2.0. It is planned that it will be faster, smaller, safer, easier to embed in other applications. Changes have been made much more, here you can find a detailed description .
    ["one", "two", "three"].map(&:capitalize)
    => ["One", "Two", "Three"]






    Also popular now: