New Amazing features in Ruby 1.9.3

2 min read

Ruby is one of the dynamic object oriented programming language. Ruby 1.9 has been released with lot of amazing features. Ruby 1.9.3 is the latest stable version of ruby, with major increases in speed and reliability. Some of the features in Ruby 1.9.3 are

    * “load.c” patch to speed up requiring/loading files
    * Improved GC performance with a lazy garbage collector
    * Random.rand accepts inputs in form of ranges
    * IO/Console, new library in stdlib
    * New encodings supported
    * New String methods
    * Extended formats for Timezone
    * Scope of Constants in Struct
    * Improved matrix library
    * New methods private_constants and public_constants in Module

Let’s have a quick walkthrough of some Ruby 1.9.3 features below
* Extended formats for Timezone

    Time#strftime now supports some extended formats for timezone like :z and ::z. An example of strftime method
    [source language=”ruby”]
    Time.now.strftime(“%:z %::z”) # => “+01:00 +01:00:00”
    [/source]
    where :z includes the minutes and ::z gives the full HH:MM:SS

* New Methods for String Class

    In addition to some changes in String class, we have some new methods like prepend and byteslice.
    String#prepend prepends a new string to an existing string. A simple example is
    [source language=”ruby”]
    str = ” Programmer”
    str.prepend(“Ruby “)
    puts str # => “Ruby Programmer”
    [/source]
    String#byteslice returns a substring from the given string depending upon the arguments passed. Some formats of byteslice are
    [source language=”ruby”]
    “ruby”.byteslice(1) # => “u”
    “ruby”.byteslice(1, 2) # => “ub”
    “ruby”.byteslice(1..3) # => “uby”
    [/source]

* Changes in Random#rand

    Ruby 1.9.2 introduced Random class in which we can create our own random number object. In 1.9.2, you can use rand as instance method like
    [source language=”ruby”]
    1.9.2p290 :003 > r = Random.new
    1.9.2p290 :004 > r.rand(2..6)
    [/source]
    But now in Ruby 1.9.3, you can directly accept ranges on its own rand class method like
    [source language=”ruby”]
    1.9.3p0 :029 > Random.rand(1..3)
    => 3
    [/source]
    In addition to this, Kernel.rand also supports ranges like
    [source language=”ruby”]
    1.9.3p0 :030 > rand(2..8)
    => 6
    [/source]

* New encodings supported

    Ruby 1.9.3 supports some new encodings. In previous version, we used to have UTF-16 and UTF-32 for big-endian(BE) and little -endian specific forms but now we have 4 new encodings like
    [source language=”ruby”]
    1.9.3p0 :001 > Encoding.find(“UTF-32”)
    => #
    1.9.3p0 :002 > Encoding.find(“UTF-16”)
    => #
    1.9.3p0 :003 > Encoding.find(“CP950”)
    => #
    1.9.3p0 :004 > Encoding.find(“CP951”)
    => #
    [/source]

* Constant Scope in Struct Bug Fix

    A weird bug in Ruby 1.9.2 Struct. Passing a block to 1.9.2 Struct.new does not create a new scope for constants. For example
    [source language=”ruby”]
    VALUE = false
    Test = Struct.new(:name, :age) do
    def self.check?
    VALUE
    end
    end
    Test::VALUE = true
    Test.check?
    [/source]
    The output of above code differs in ruby 1.9.2 and ruby 1.9.3. The output will be as shown below
    [source language=”ruby”]
    # ruby 1.9.2-p180
    Test::VALUE = true # Actually does VALUE=true
    Test.check? # Reads new value of VALUE
    => true
    [/source]
    [source language=”ruby”]
    # ruby 1.9.3-p0
    Test::VALUE = true # Creates new constant Test::VALUE
    Test.check? # Reads unchanged value of VALUE
    => false
    [/source]
    In Ruby 1.9.2, the interpreter assumes you mean to write VALUE = true and changes the global constant. This behavior is a bug.
    Ruby 1.9.3 corrects this behavior. It interprets Test::VALUE = true to mean “Create a brand new constant named Test::VALUE and assign it to true”. And so the global VALUE remains unchanged.

* Lazy Sweeping Garbage Collector

    Ruby 1.9.3 provides Lazy sweeping GC which is 8% faster on average. The Simple (non-lazy) Mark Sweep GC executes mark and sweep atomically. Also, it sweeps the whole heap and links all live objects to the freelist whereas in Lazy sweeping, each invocation of the object allocation sweeps the heap until it finds an appropriate free object.

These are some of the ruby 1.9.3 features I learnt.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *