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']
=> #<File::Stat dev=0x301, ino=16077, mode=0100644, nlink=1, uid=0, gid=0, 
rdev=0x0, size=860, blksize=4096, blocks=8, atime=Sun May 04 15:14:44 MDT
2003, mtime=Fri Mar 14 10:35:05 MST 2003, ctime=Fri Mar 14 10:35:05 MST 2003>
>> statcache
=> {"/etc/passwd"=>#<File::Stat dev=0x301, ino=16077, mode=0100644, nlink=1,
uid=0, gid=0, rdev=0x0, size=860, blksize=4096, blocks=8, atime=Sun May 04
15:14:44 MDT 2003, mtime=Fri Mar 14 10:35:05 MST 2003, ctime=Fri Mar 14
10:35:05 MST 2003>}

Cool, huh?

Jason Creighton