Robert K. wrote:
If I understand the rest of your post correctly it is not exactly the
same structure but rather similarly structured data in two different
data structures (custom classes and XML DOM).
True, but it is a 3rd party object library that I don’t have access to
other than through OLE calls or file storage.
The fix that I propose is to not have two data structures storing the
same data. If you have classes already for storing all this, I’d
probably write bit of code that builds the structure using an XML push
or pull parser.
I wish that were the case, but 3rd party non-GPL’d code.
Usually nil is returned for absent keys so you can do
I was attempting to avoid the case when it isn’t…
def method_missing(sym,*args,&blk)
self[sym] || self[sym.to_s] || super
end
… this will trigger the creation and return of a memoized object,
which is precisely a side-effect I want to avoid.
For example:
test=Hash.new {|k,v| k[v]=v.to_s*3} => {}
test[:one] => “oneoneone”
test[:two] => “twotwotwo”
test.three
NoMethodError: undefined method `three’ for {:one=>“oneoneone”,
:two=>“twotwotwo”}:Hash
from (irb):15
cool and desired
class Hash
def method_missing(sym,*args,&blk)
return self[sym] if self.key?(sym)
return self[sym.to_s] if self.key?(sym.to_s)
super
end
end
=> nil
test.one => “oneoneone”
test.two => “twotwotwo”
test.three
NoMethodError: undefined method three' for {:one=>"oneoneone", :two=>"twotwotwo"}:Hash from (irb):20:in
method_missing’
from (irb):25
#Same error message also cool and desired
test[:three] => “threethreethree”
test.three => “threethreethree”
#Redefine with your more compact code, which isn’t performing the key?
check
class Hash
def method_missing(sym,*args,&blk)
self[sym] || self[sym.to_s] || super
end
end
=> nil
test.four => “fourfourfour”
#Not my expected result. I would expect an error from that method call,
but thanks for the effort.
Mac
http://pqmf.com