Ruby 1.8 series has been used since 2003 and many great products were born on it.
Ruby 1.9.1 was released in January, 2009. This is the first stable release of the Ruby 1.9 series. It is modern, faster, with clearer syntax, multilingualized, a much improved version of Ruby.
As far as i know Ruby 1.9.1-p243 has been released in July,2009. This is a patch level release in the 1.9.1 series. It includes bug fixes. Get updated with Ruby Archives and News.
Some times before i started looking for What’s new and changed in Ruby 1.9.1 compared to Ruby 1.8.7. Here i am listing some changes, i found interesting.
The First one is Obvious !!!
1. You can check Ruby Version and Release Date within irb :
in Ruby 1.8.7
irb(main):017:0> RUBY_VERSION
=> “1.8.7″
irb(main):018:0> RUBY_RELEASE_DATE
=> “2008-08-11″
in Ruby 1.9.1
b(main):014:0> RUBY_VERSION
=> “1.9.1″
irb(main):015:0> RUBY_RELEASE_DATE
=> “2009-07-16″
2. Index operator for string now returns string instead of ASCII value.
in Ruby 1.8.7
irb(main):007:0> “ruby”[1]
=> 117
in Ruby 1.9.1
irb(main):005:0> “ruby”[1]
=> “u”
3. ? operator works differently
in Ruby 1.8.7
irb(main):008:0> ?a
=> 97 #Returns the ASCII value
in Ruby 1.9.1
irb(main):006:0> ?a
=> “a” #Returns a one character String
i.e. You can always do like
in Ruby 1.8.7
irb(main):009:0> ?a.class
=> Fixnum
in Ruby 1.9.1
irb(main):007:0> ?a.class
=> String
4. Array.to_s now includes punctuations.
in Ruby 1.8.7
irb(main):010:0> [1,2,3,4,"abhishek"].to_s
=> “1234abhishek”
in Ruby 1.9.1
irb(main):008:0> [1,2,3,4,"abhishek"].to_s
=> “[1, 2, 3, 4, \"abhishek\"]“
in hashes also:
in Ruby 1.8.7
irb(main):050:0> {:a=>1, :b=>2}.to_s
=> “a1b2″
in Ruby 1.9.1
irb(main):040:0> {:a=>1, :b=>2}.to_s
=> “{:a=>1, :b=>2}”
5. Block variables must be local variable, so tricks like [1,2,3].each{|@i|} no longer work.
in Ruby 1.8.7
irb(main):011:0> i=0
=> 0
irb(main):012:0> [1,2,3].each{|i|}
=> [1, 2, 3]
irb(main):013:0> i
=> 3
in Ruby 1.9.1
irb(main):009:0> i=0
=> 0
irb(main):010:0> [1,2,3].each{|i|}
=> [1, 2, 3]
irb(main):011:0> i
=> 0
lets see one more:
in Ruby 1.8.7
irb(main):014:0> @a=[1,2,3]
=> [1, 2, 3]
irb(main):015:0> [1,2,3].each{|@a|}
=> [1, 2, 3]
irb(main):016:0> @a
=> 3
in Ruby 1.9.1
irb(main):012:0> @a=[1,2,3]
=> [1, 2, 3]
irb(main):013:0> [1,2,3].each{|@a|}
SyntaxError: (irb):13: formal argument cannot be an instance variable
[1,2,3].each{|@a|}
^
from /usr/local/bin/irb:12:in `<main>’
So now you can’t use a outside variable within a block.
6. Ruby 1.9 now uses native threads, instead of green threads.
7. Module#instance_methods, #private_instance_methods, #public_instance_methods
They now return an array of symbols (instead of strings):
in Ruby 1.8.7
irb(main):051:0> class X; def foo; end end
=> nil
irb(main):052:0> X.instance_methods(false)
=> ["foo"]
in Ruby 1.9.1
irb(main):041:0> class X; def foo; end end
=> nil
irb(main):042:0> X.instance_methods(false)
=> [:foo]
8. You can’t do string.each anymore…
in Ruby 1.8.7
irb(main):039:0> “abhishek”.each do puts “a” end
a
=> “abhishek”
in Ruby 1.9.1
irb(main):031:0> “abhishek”.each do puts “a” end
NoMethodError: undefined method `each’ for “abhishek”:String
from (irb):31
from /usr/local/bin/irb:12:in `<main>’
9. Hashes are now ordered (better to say Hash keys are now ordered..)
in Ruby 1.8.7
irb(main):043:0> h={:a=>1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :d=>4, :c=>3}
irb(main):044:0> h[:e]=5
=> 5
irb(main):045:0> h
=> {:a=>1, :b=>2, :e=>5, :d=>4, :c=>3}
irb(main):046:0> h.keys
=> [:a, :b, :e, :d, :c]
irb(main):047:0> h.values
=> [1, 2, 5, 4, 3]
irb(main):048:0> h.to_a
=> [[:a, 1], [:b, 2], [:e, 5], [:d, 4], [:c, 3]]
in Ruby 1.9.1
irb(main):032:0> h={:a=>1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
irb(main):033:0> h[:e]=5
=> 5
irb(main):034:0> h
=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}
irb(main):035:0> h.keys
=> [:a, :b, :c, :d, :e]
irb(main):036:0> h.values
=> [1, 2, 3, 4, 5]
irb(main):037:0> h.to_a
=> [[:a, 1], [:b, 2], [:c, 3], [:d, 4], [:e, 5]]
10. range.cover?(value) : compares value to the begin and end values of the range, returning true if it is comprised between them.
in Ruby 1.8.7
irb(main):056:0> (“a”..”z”).cover?(“c”)
NoMethodError: undefined method `cover?’ for “a”..”z”:Range
from (irb):56
from :0
in Ruby 1.9.1
irb(main):045:0> (“a”..”z”).cover?(“c”)
=> true
irb(main):046:0> (“a”..”z”).cover?(5)
=> false
11. String.clear
in Ruby 1.8.7
irb(main):057:0> a=”abhishek”
=> “abhishek”
irb(main):058:0> a.clear
NoMethodError: undefined method `clear’ for “abhishek”:String
from (irb):58
from :0
in Ruby 1.9.1
irb(main):047:0> a=”abhishek”
=> “abhishek”
irb(main):048:0> a.clear
=> “”
12. pack and unpack :
in Ruby 1.8.7
irb(main):062:0> s = (0..4).to_a.pack(“V*”)
=> “0000000001000000020000000300000004000000″
irb(main):063:0> a = []
=> []
irb(main):064:0> s.unpack(“V*”){|x| a << x}
=> [0, 1, 2, 3, 4]
irb(main):065:0> a
=> []
in Ruby 1.9.1
irb(main):052:0> s = (0..4).to_a.pack(“V*”)
=> “\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00″
irb(main):053:0> a = []
=> []
irb(main):054:0> s.unpack(“V*”){|x| a << x}
=> nil
irb(main):055:0> a
=> [0, 1, 2, 3, 4]
Get useful information on Ruby 1.9.1
Detailed reading about changes in Ruby 1.9.1
If you want to share this post, use following :












Jan 03, 2011 @ 23:49:04