On Jul 21, 4:30=A0pm, Kendall Gifford <zettab... / gmail.com> wrote:

> For fun, here's my OO-oriented approach with inspiration (though differen=
t)
> from Robert's solution:
>
> class Hash
> =A0 INDENT =3D " " * 2
> =A0 def inspect(indent =3D "")
> =A0 =A0 result =A0 =A0=3D "{\n"
> =A0 =A0 processed =3D 0
> =A0 =A0 total =A0 =A0 =3D count()
> =A0 =A0 each do |k, v|
> =A0 =A0 =A0 total =A0+=3D 1
> =A0 =A0 =A0 v =A0 =A0 =A0 =3D v.is_a?(Hash) ? v.inspect(indent + INDENT) =
: v.inspect
> =A0 =A0 =A0 result << indent + INDENT + "#{k.inspect} =3D> #{v}"
> =A0 =A0 =A0 result << "," if processed < total
> =A0 =A0 =A0 result << "\n"
> =A0 =A0 end
> =A0 =A0 "#{result}#{indent}}"
> =A0 end
> end

Always a good idea to make it easier to see what the hell is going
on :-)

> class Tree
> =A0 attr_reader :registry
> =A0 def initialize(parent =3D :parent)
> =A0 =A0 @parent =A0 =3D parent
> =A0 =A0 @registry =3D {}
> =A0 end
> =A0 def register(node)
> =A0 =A0 parent =3D node.send(@parent)
> =A0 =A0 if parent
> =A0 =A0 =A0 @registry[parent] =A0 =A0 ||=3D {}
> =A0 =A0 =A0 @registry[parent][node] =3D @registry[node]
> =A0 =A0 =A0 register(parent)
> =A0 =A0 end
> =A0 end
> =A0 def to_hash
> =A0 =A0 @registry.select { |k, v| k.send(@parent_method).nil? }
> =A0 end
> end

Nice, that's pretty easy to understand. Thanks.

FYI, @parent_method, should be @parent.