Posted by Surendran Sukumaran on May 16th, 2011

There are lot more things improved in Ruby 1.9.2, I am going to discuss about 3 new things I liked which is introduced in Ruby 1.9.x.

Block Variable Scope in Ruby 1.9.x

Block arguments don’t clash with local variables, arguments are always local.
This is a nice fix in Ruby 1.9 .

Ruby 1.8

 check_block = 2
check_block
 => 2 
1.upto(5) {|check_block| check_block} 
check_block
 => 5 

Checkout the value of local variable changed to 5, which is clash with the block variable.

Ruby 1.9

check_block = 2
check_block
 => 2 
 

1.upto(5) {|check_block| check_block}
check_block
=> 2

Here the value of check_block is still 2.

New lambda syntax in Ruby 1.9

lambda =->(a, b){a + b}
lambda.(1,3)
=> 4
 

We can even call the lambda function like

 
lambda[2,4]
=> 6

It is also possible to give default values for arguments.

lambda =->(a, b = 10){a + b}
lambda.(1)
=> 11
 

note : Ruby 1.9.2 still supports the older lambda syntax.

New hash Literal

Alternate syntax for Symbol => Object hash notation:

Ruby 1.8

{:symbol => 10}
=> {:symbol => 10}
 

Ruby 1.9

{symbol: 10}
=> {:symbol => 10}
 

I will get back to you soon with some additional new things that was in Ruby 1.9.2 .

pixelstats trackingpixel Tags: ,
Unique Views: 3732 Total views: 4375
Follow responses at RSS 2.0. Leave a response | Trackback.

3 Responses to “Three New things in Ruby 1.9.x”

  1. Joshua Geake says:

    Nice post! I had a similar issue myself with wanting to automate/schedule, create, upload and ping a sitemap.xml so blogged about it here – http://www.geakeit.co.uk/2011/05/12/generating-a-scheduled-google-sitemap-xml-for-your-ecommerce-or-cms-website/. Hopefully someone can find this useful!

  2. Pitambar says:

    nice blog suren

  3. Uma Mahesh Varma says:

    Nice post and thank’s for the useful information.

    Thank You,
    Uma Mahesh.

    Leave a Reply