From: Jason Creighton Date: 2003-06-12T10:58:32+09:00 Subject: Re: Is there any description of main changes in 1.8 over 1.6.x / 1.7.x ? On 11 Jun 2003 11:56:00 -0700 itsme213@hotmail.com (you CAN teach an old dog ...) wrote: > google post insists "the body of your message must contain some text", so > here it is Look at the documentation for "shim" on the RAA: It's a Ruby implementation of some of the new features in 1.8. Just a couple things off the top of my head. Enumerable#partition Enumerable#zip Enumerable#all? Enumerable#any? Enumerable#inject Various new methods for other stuff, I haven't looked very much at the other classes. Look at the test 1.8 version of 'ri'. You can get it at rdoc.sf.net (I think.) Click on download. 'ri' is at the bottom of the download section. (This is all IIRC, so I could be wrong.) Another cool thing: Blocks initializers. Consider this example: >> a = Array.new(5, Array.new(5,0)) >> pp a [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] (To use 'pp' you need to do a "require 'pp'". I don't think that pp is in 1.6) This is all well and good, until.... >> a[0][2] = "Hello!" => "Hello!" >> pp a [[0, 0, "Hello!", 0, 0], [0, 0, "Hello!", 0, 0], [0, 0, "Hello!", 0, 0], [0, 0, "Hello!", 0, 0], [0, 0, "Hello!", 0, 0]] So all five elements in the top level array point the same object. So, what to do? You could do some clever looping, but 1.8 makes it easy: >> a = Array.new(5) { Array.new(5,0) } >> a[0][2] = "Hello!" => "Hello!" >> pp a [[0, 0, "Hello!", 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] Note: The reason (I think. I'm not sure about this.) you can get away without having '0' in a block is because there's no method for a Fixnum (Or other numbers in Ruby, for that matter.) that would modify the receiver. Almost the same deal with hashes: h = Hash.new { |hash, key| puts "Spawning new array!"; hash[key] = Array.new; } => {} >> h["This value has not been used before"].push "Hello!" Spawning new array! => ["Hello!"] >> h => {"This value has not been used before"=>["Hello!"]} >> h = Hash.new { |hash, key| "You can't use keys without assigning them first!" } => {} >> h["This value has not been sued before"].upcase => "YOU CAN'T USE KEYS WITHOUT ASSIGNING THEM FIRST!" >> h => {} You can even use this to implement a poor man's cache: >> statcache = Hash.new { |h, k| h[k] = File.stat(k) } => {} >> statcache['/etc/passwd'] => # >> statcache => {"/etc/passwd"=>#} Cool, huh? Jason Creighton