From: James Britt Date: 2004-08-14T05:42:09+09:00 Subject: Re: Question: A method for summing several variables Yet Another Version $SAFE = 3 # See # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13402 # for ArrayMD class ArrayMD < Array def [](n) self[n]=ArrayMD.new if super(n)==nil super(n) end end class FieldWrapper def initialize( ) @data = ArrayMD.new( ) end def [](box_name) x, y = get_indices( box_name.to_s ) @data[x][y][0] end def method_missing( name, *args ) raise( "Unknown method #{name}") unless name.to_s =~ /^box/ unless ( name.to_s =~ /=/ ) x, y = get_indices( name.to_s ) @data[x][y][0] else name = name.to_s.gsub( '=', '' ) x, y = get_indices( name.to_s ) @data[x][y] = args end end def get_indices( box_name ) s = box_name.gsub( "box", '') parts = s.split( '_' ) return [parts[0].to_i, parts[1].to_i] end end fw = FieldWrapper.new fw.box1_1 = 23 p fw.box1_1 fw.send( "box1_2=", 12) p fw[ "box1_2" ]