From: Avdi Grimm Date: 2013-04-30T00:26:30+09:00 Subject: Re: New Rexx like data structure --089e0122931070f28c04db816b83 Content-Type: text/plain; charset=ISO-8859-1 You might enjoy reading the code of https://github.com/ahoward/map BTW, nice to see someone mention REXX. I taught myself to code in REXX :-) On Mon, Apr 29, 2013 at 11:07 AM, Peter Hickman < peterhickman386@googlemail.com> wrote: > 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? > > --089e0122931070f28c04db816b83 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
You might enjoy reading the code of=A0https://github.com/ahoward/map
<= /div>

BTW, nice to see someone mention REXX. I tau= ght myself to code in REXX :-)


On Mon,= Apr 29, 2013 at 11:07 AM, Peter Hickman <peterhickman386@goo= glemail.com> wrote:
This is just something that= I have been playing with for some time but I thought a structure like this= might be cool.

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 this ...<= /div>

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

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

cl= ass 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 this plan=

a.j =3D> Hash

Given that a.j has not beed defined, that is there has been no "a= .j =3D 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?


--089e0122931070f28c04db816b83--