From: Robert Klemme Date: 2013-04-30T03:07:05+09:00 Subject: Re: New Rexx like data structure --001a11c30b887a3eb804db83c05c Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 29, 2013 at 5:43 PM, Peter Hickman < peterhickman386@googlemail.com> wrote: > On 29 April 2013 16:33, Robert Klemme wrote: > >> We can only make it work if you accept always getting an OpenStruct in >> return >> >> irb(main):011:0> class OpenStruct >> irb(main):012:1> alias _method_missing method_missing >> irb(main):013:1> def method_missing(*a,&b) x=_method_missing(*a,&b); >> x.nil? ? send("#{a.first}=", self.class.new) : x end >> irb(main):014:1> end >> => nil >> irb(main):015:0> o=OpenStruct.new >> => # >> irb(main):016:0> o.foo.bar=123 >> => 123 >> irb(main):017:0> o >> => #> >> >> > Hmm. That doesn't seem to work for me in 1.8.7 or 1.9.2 > > ruby-1.9.2-p290 :001 > class OpenStruct > ruby-1.9.2-p290 :002?> alias _method_missing method_missing > ruby-1.9.2-p290 :003?> def method_missing(*a,&b) > x=_method_missing(*a,&b); x.nil? ? send("#{a.first}=", self.class.new) : x > end > ruby-1.9.2-p290 :004?> end > => nil > ruby-1.9.2-p290 :005 > > ruby-1.9.2-p290 :006 > o=OpenStruct.new > => # > ruby-1.9.2-p290 :007 > o.foo.bar=123 > NoMethodError: undefined method `foo' for # > from (irb):3:in `method_missing' > from (irb):7 > from /Users/peterhickman/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in > `
' > ruby-1.9.2-p290 :008 > o > => # > > Same error in both > > Just to be sure: you did require "ostruct" did you? robert@fussel:~$ irb -r ostruct irb(main):001:0> class OpenStruct irb(main):002:1> alias _method_missing method_missing irb(main):003:1> def method_missing(*a,&b) x=_method_missing(*a,&b); x.nil? ? send("#{a.first}=", self.class.new) : x end irb(main):004:1> end => nil irb(main):005:0> irb(main):006:0* o = OpenStruct.new => # irb(main):007:0> o.foo.bar = 123 => 123 irb(main):008:0> RUBY_VERSION => "1.9.3" Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --001a11c30b887a3eb804db83c05c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Apr 29, 2013 at 5:43 PM, Peter Hickman &l= t;peter= hickman386@googlemail.com> wrote:
On 29 April 2013 16:33,= Robert Klemme <shortcutter@googlemail.com> wrote:<= br>
We can only make it work if you accept al= ways getting an OpenStruct in return

irb(main):011:0> cla= ss OpenStruct
irb(main):012:1> alias _method_missing method_mi= ssing
irb(main):013:1> def method_missing(*a,&b) x=3D_method_missing(= *a,&b); x.nil? ? send("#{a.first}=3D", self.class.new) : x en= d
irb(main):014:1> end
=3D> nil
irb(mai= n):015:0> o=3DOpenStruct.new
=3D> #<OpenStruct>
irb(main):016:0> o.foo.bar=3D= 123
=3D> 123
irb(main):017:0> o
=3D>= #<OpenStruct foo=3D#<OpenStruct bar=3D123>>


Hmm= . That doesn't seem to work for me in 1.8.7 or 1.9.2

ruby-1.9.2-p290 :001 > class OpenStruct
ruby-1.9.2-p290 :002?> =A0 alias _method_missing method_missing
ruby-1.9.2-p290 :003?> =A0 def method_missing(*a,&b) x=3D_method_m= issing(*a,&b); x.nil? ? send("#{a.first}=3D", self.class.new)= : x end
ruby-1.9.2-p290 :004?> =A0 end
=A0=3D> nil=A0
ruby-1.9.2-p290 :005 >=A0
ruby-1.9.2-p290 :006 > =A0 o=3D= OpenStruct.new
=A0=3D> #<OpenStruct:0x00000100a9c3a0>=A0=
ruby-1.9.2-p290 :007 > o.foo.bar=3D123
NoMethodError: undefined method `foo' for #<OpenStruct:0x000001= 00a9c3a0>
from (i= rb):3:in `method_missing'
from (irb):7
from /Users/peterhickman/= .rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby= -1.9.2-p290 :008 > o
=A0=3D> #<OpenStruct:0x00000100a9c3= a0>=A0

Same error in both
=A0

Just to be sure: yo= u did require "ostruct" did you?
=
robert@fuss= el:~$ irb -r ostruct
irb(main):001:0> class OpenStruct
irb(main):002:1> =A0 alias _method_missing method_m= issing
irb(main):003:1> =A0 def method_m= issing(*a,&b) x=3D_method_missing(*a,&b); x.nil? ? send("#{a.f= irst}=3D", self.class.new) : x end
irb(main):004:1> end
=3D> nil
irb(main):005:0>=A0<= /div>
irb(main):006:0* o =3D OpenStruct.new
=
=3D> #<OpenStruct>
irb(main):007:0= > o.foo.bar =3D 123
=3D> 123
irb(main):008:0> RUBY_VERSION
=3D> "1.9.3"=

Cheers

ro= bert

--
remember.guy do |as, often| as.you_ca= n - without end
http://bl= og.rubybestpractices.com/

--001a11c30b887a3eb804db83c05c--