Thursday, 23 October 2014

What is the difference between Ruby 1.8.7 and 1.9.2?

What is the difference between Ruby 1.8.7 and 1.9.2?

   a).Pseudo-Keyword Hash Syntax
       Ruby 1.9 adds a cool feature that lets you write things like:
         foo(a: 1, b: 2)
         But on Ruby 1.8, we’re stuck using the old key => value syntax:
         foo(:a => 1, :b => 2)
   b).Multisplat Arguments
            Ruby 1.9.1 offers a downright insane amount of ways to process arguments to methods.
        def add(a,b,c,d,e)
           a + b + c + d + e
         end
    
       add(*[1,2], 3, *[4,5]) #=> 15
      The closest thing we can get to this on Ruby 1.8 would be something like  this:
       add(*[[1,2], 3, [4,5]].flatten) #=> 15

c).  Block-Local Variables
On Ruby 1.9, block variables will shadow outer local variables, resulting in the following behavior:
>> a = 1
=> 1

>> (1..10).each { |a| a }
=> 1..10
>> a
=> 1
This is not the case on Ruby 1.8, where the variable will be modified even if not explicitly set:
>> a = 1
=> 1
>> (1..10).each { |a| a }
=> 1..10

>> a
=> 10
d). Block Arguments
In Ruby 1.9, blocks can accept block arguments, which is most commonly seen in define_method:
define_method(:answer) { |&b| b.call(42) }
However, this won’t work on Ruby 1.8 without some very ugly workarounds

e).New Proc Syntax

Both the stabby Proc and the .() call are new in 1.9, and aren’t parseable by the Ruby 1.8 interpreter. This means that calls like this need to go:
>> ->(a) { a*3 }.(4)
=> 12
Instead, use the trusty lambda keyword and Proc#call or Proc#[]:
>> lambda { |a| a*3 }[4]
=> 12

f).Using Enumerator

In Ruby 1.9, you can get back an Enumerator for pretty much every method that iterates over a collection:
>> [1,2,3,4].map.with_index { |e,i| e + i }
=> [1, 3, 5, 7]
In Ruby 1.8, Enumerator is part of the standard library instead of core, and isn’t quite as feature-packed. However, we can still accomplish the same goals by being a bit more verbose:
>> require "enumerator"
=> true

>> [1,2,3,4].enum_for(:each_with_index).map { |e,i| e + i }
=> [1, 3, 5, 7]
 g). String Iterators
In Ruby 1.8, Strings are Enumerable, whereas in Ruby 1.9, they are not. Ruby 1.9 provides String#lines, String#each_line, String#each_char, and String#each_byte, all of which are not present in Ruby 1.8.

 h). Character Operations
In Ruby 1.9, strings are generally character-aware, which means that you can index into them and get back a single character, regardless of encoding:
>> "Foo"[0]
=> "F"
This is not the case in Ruby 1.8.6, as you can see:
>> "Foo"[0]
=> 70

 i).Encoding Conversions
Ruby 1.9 has built-in support for transcoding between various character encodings, whereas Ruby 1.8 is more limited. However, both versions support Iconv. If you know exactly what formats you want to translate between, you can simply replace your string.encode("ISO-8859-1") calls with something like this:
Iconv.conv("ISO-8859-1", "UTF-8", string)
j).Introduction of Fibers

Fibers are light-weight threads with manual, cooperative scheduling, rather than the preemptive scheduling of Ruby 1.8's threads.A fiber gets interrupted only when it yields its execution. So a fiber is a sort of user-managed thread. Threads use pre-emptive scheduling, whereas fibers use cooperative scheduling.
  

No comments:

Post a Comment