So Wrong It's Almost Right

by Gregory Brown


class OpenNode < OpenStruct
include Enumerable
def initialize(my_name, parent_name, children_name, name, options={})
@my_children_name = children_name
@my_parent_name = parent_name
@my_name = my_name
super(options)
self.name = name
self.send(@my_children_name) || self.send(:"#{@my_children_name}=",{})
end
def each(&p)
self.send(@my_children_name).values.each &p
end
def add_child(klass,name,options={})
options[@my_name] = self
self << klass.new(name, options)
end
def <<(child)
child.send(:"#{@my_name}=", self)
self.send(@my_children_name)[child.name] = child.dup
end
def [](child_name)
self.send(@my_children_name)[child_name]
end
end


Update: Thanks to steer on #ruby-lang for re-writing my messy each method

3 Comments

Anonymous coward
2006-01-02 06:57:05
I can't figure out what OpenNode does.

2006-01-29 19:22:13
This is an example of someone trying to be a little too clever. There is *never* a good reason to write code in such an obfuscated manner. This guy should stick with perl.
Gregory Brown
2006-01-29 19:43:51
I guess I should go back to Perl then. ;)