From: Paul Brannan Date: 2001-11-01T03:56:23+09:00 Subject: [ruby-talk:24010] Re: RubyGems Discussion On Thu, 1 Nov 2001, Jim Freeze wrote: > On Wed, 31 Oct 2001, Paul Brannan wrote: > > > Another possibility (and I do this with config files) is to put a set of > > constants in the Ruby file. You can even temporarily overload [], so you > > end up with something that looks a LOT like an INI file: Reading this a second time, it sounds like I'm saying that my config files have sections. Actually, they are sectionless, and the section idea was just something I was thinking about out-load; see below. > > [section] > > CONSTANT1 = value1 > > CONSTANT2 = value2 > > > How do you overload [] to get the above affect? I do not think it is possible to do this in Ruby 1.6.x. But I think it should be possible in 1.7.1 using Array#allocate. I get a segmentation fault when I try this, though: class Array def self.allocate end end [1, 2, 3] With Ruby 1.6.x, I can prefix the [] with an "X" to get a similar effect. Here's something I did to get nifty-looking tables in Ruby: class Foo Rows = Array.new def self.[](name, *args) Rows.push([name, args]) end X = self _ = ' ' Header = [ _, :x, :y, :z, :a, :b ] X[ :foo, 1, _, 3, _, 5 ] X[ :foo, _, 2, _, 4, _ ] end puts Foo::Header.join("\t") puts Foo::Rows.map { |row| row.join("\t") } Paul