From: Peter Hickman Date: 2013-04-30T00:07:59+09:00 Subject: New Rexx like data structure --047d7b343f06ed951c04db813f0b Content-Type: text/plain; charset=ISO-8859-1 This is just something that I have been playing with for some time but I thought a structure like this might be cool. a = Hash.new a.b = 12 This behaves the same as a['b'] = 12, and this ... a.c.d = 42 behaves like this ... a['c']['d'] = 42 The syntax looks cleaner to me if a little Rexxish (or Lua like). Here's how I implemented it class Hash def method_missing(m, *args, &block) key = m.to_s if key =~ /(.*)=$/ self.store($1, args.first) elsif self[key] == nil self.store(key, Hash.new) else self[key] end end end There is just one little wrinkle in this plan a.j => Hash Given that a.j has not beed defined, that is there has been no "a.j = 19" for example, I would prefer that a.j return nil but can't for the life of me think how to get this to work. Any suggestions? --047d7b343f06ed951c04db813f0b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
This is just something that I have been playing with for s= ome time but I thought a structure like this might be cool.

<= div style>
a =3D Hash.new
a.b =3D 12

This behaves the same as a['b'] =3D 12, and this ...

=
a.c.d =3D 42

behaves like thi= s ...

a['c']['d'] =3D = 42

The syntax looks cleaner to me if a little = Rexxish (or Lua like). Here's how I implemented it

=
class Hash
=A0 def method_missing(m, *args,= &block)
=A0 =A0 key =3D m.to_s

=A0 =A0 if key =3D~ /(= .*)=3D$/
=A0 =A0 =A0 self.store($1, args.first)
=A0 =A0= elsif self[key] =3D=3D nil
=A0 =A0 =A0 self.store(key, Hash.new)=
=A0 =A0 else
=A0 =A0 =A0 self[key]
=A0 =A0 end
=A0 end =A0
end

There is just one little wrinkle in thi= s plan

a.j =3D> Hash

Given that a.j has not beed defined, that is there has been no &= quot;a.j =3D 19" for example, I would prefer that a.j return nil but c= an't for the life of me think how to get this to work.

Any suggestions?

--047d7b343f06ed951c04db813f0b--