Summary
As has become a holiday tradition in the Ruby community, Yukihiro Matsumoto released the latest version of the Ruby language and interpreter.
Advertisement
The recently released Ruby 1.9 interpreter contains many new features, and some changes that may break older code. Dave Thomas summarizes some of the arguments for, and against, switching the latest Ruby version, and also writes about what he considers to be the most significant new features, such as the new YARV virtual machine, integrated Gems and Rake, and built-in encodings and transcodings for String.
A more exhausting summary of new Ruby features has been compiled by Mauricio Fernandez, who used the change logs to extract new features, and will soon supply an automated way to try out those features. Based on Fernandez's list, some of the more interesting bits in Ruby 1.9 include:
A literal, JSON-like hash syntax for objects:
{a: "foo"}
In Ruby 1.9, block arguments are always local, meaning that an argument passed to a block will shadow that argument's value outside the block (changes made to the argument value inside the block will be invisible outside the block).
In 1.9, blocks can take block arguments:
define_method(:foo){|&b| b.call(bar)}
For splat arguments, 1.9 uses:
to_splat is used instead of #to_a.
Ruby 1.9 allows mandatory arguments to follow optional arguments in method signatures:
def m(a, b=nil, *c, d)
[a,b,c,d]
end
Ruby 1.9 supplies a BasicObject class for situations when you need a blank-state object with just a few essential methods
The Kernel and Module classes have been given some additional methods, such as Kernel#define_singleton_method and Kernel#instance_variable_defined?
A more significant change is that in Ruby 1.9, class variables are not inherited in subclasses.
Among the convenience features in 1.9 is that Enumerator is now integrated in the core and need not be required. Additional methods are also provided, such as Enumerable#cycle, Enumerable#each_with_index, Enumerable#group_by, or Enumerable#count. At the same time, String is no longer enumerable in Ruby 1.9
Regular expressions also received a major upgrade in Ruby 1.9, partly as a result of the newly integrated Oniguruma regexp engine.
Another interesting feature allows scripts to be daemonized (at least on Unix) via Process.daemon() that detaches the process from the controlling terminal and runs the process as a daemon.
Ruby 1.9 also adds fibers for light-weight concurrency. David Flanagan provides an example:
f = g = nil
f = Fiber::Core.new { |x|
puts "F1: #{x}"
x = g.transfer(x+1)
puts "F2: #{x}"
x = g.transfer(x+1)
puts "F3: #{x}"
}
g = Fiber::Core.new { |x|
puts "G1: #{x}"
x = f.transfer(x+1)
puts "G2: #{x}"
x = f.transfer(x+1)
}
f.transfer(100)
* the "string".each_char as it had an uglier alternative with regex in the past; * the new block features which help with making them more useful; * Hash parameters a la JavaScript ones, instead of always using "=>"; * the potential optimizations which should help with some scalability issues by helping with responsiveness; * native threading should help with some Windows issues I guess;
I am anxious to learn more about it and use it with "gems" libraries though. Matz was not kidding when he said 1.9 would be a release with new features, huh? :-)