From: Robert Klemme Date: 2004-05-21T06:28:47+09:00 Subject: Re: Overriding "", [], and {} "why the lucky stiff" schrieb im Newsbeitrag news:40AD161F.6070107@whytheluckystiff.net... > Bill Atkins wrote: > > >Is there any way to cause "", [], and {} to have different behaviors. > >I'd like to be able to have {} create OrderedHash's instead of Hash's, > >but I don't know how to change this behavior. If I do Hash = > >OrderedHash, then Hash.new will create an OrderedHash, but {} still > >gives me an old-fashioned hash. > > > > > This isn't what you're looking for, but I think it's the best answer. > Both Array and Hash have a builtin [] class method, which can be used to > define new Arrays or Hashes. > > >> a = Array['goats', 'coats', 'asymptotes'] > => ["goats", "coats", "asymptotes"] > >> h = Hash['g' => 'goats', 'c' => 'coats'] > => {"c"=>"coats", "g"=>"goats"} > > You can use the same class method to get away with really clean > definitions of new Array/Hash derivatives. For example, YAML has an > Omap (ordered hash) class: > > >> o = YAML::Omap['c', 'coats', 'g', 'goats'] > => [["c", "coats"], ["g", "goats"]] > >> o.class > => YAML::Omap > > _why or how about oh = OrderedHash.new.update( { "foo" => "bar", "bar2 => "foo", } ) robert