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.
On Mon, Apr 29, 2013 at 5:07 PM, Peter H. < [email protected]> wrote:
behaves like this …
if key =~ /(.*)=$/
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.
On Mon, Apr 29, 2013 at 5:43 PM, Peter H. < [email protected]> wrote:
=> nil
ruby-1.9.2-p290 :001 > class OpenStruct
NoMethodError: undefined method foo' for #<OpenStruct:0x00000100a9c3a0> from (irb):3:inmethod_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
=> #OpenStruct:0x00000100a9c3a0
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”
The problem here is that even if it was to work it is just changing the
problem by returning an OpenStruct rather than a Hash. But what I really
wanted was a nil so nothing really changes.
The problem here is that even if it was to work it is just changing the
problem by returning an OpenStruct rather than a Hash. But what I really
wanted was a nil so nothing really changes.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.