From: Robert Klemme Date: 2013-04-30T00:29:46+09:00 Subject: Re: New Rexx like data structure --047d7b672e26e312d304db818d8c Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 29, 2013 at 5:07 PM, 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. > There is no way because a getter method cannot know whether the result is used as is or whether it is part of a chain and another setter is invoked. In other words: x = a.c # case 1 a.c.d = 9 # case 2 Here, no implementation of method c can detect whether we have case 1 or 2. This reminds me of OpenStruct... Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7b672e26e312d304db818d8c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Apr 29, 2013 at 5:07 PM, Peter Hickman &l= t;peter= hickman386@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 =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.

There is no way bec= ause a getter method cannot know whether the result is used as is or whethe= r it is part of a chain and another setter is invoked. =A0In other words:

x =3D a.c #= case 1
a.c.d =3D 9 # case 2

Here, no implementat= ion of method c can detect whether we have case 1 or 2.

This remind= s me of OpenStruct...

Kind regards
<= br>
robert

= --
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--047d7b672e26e312d304db818d8c--