I
Iain Barnett
Hi,
I've written the method below to recurse through a non-binary tree of =
arbitrary depth. I'm still new to Ruby and was wondering if there were =
some Rubyism's (or just general pointers) that might improve it?
Each object 't' holds an array 'children' of objects of the same type as =
t, which can themselves hold further arrays. Each object also has the =
attribute 'parent' holding a reference to the parent object.
def find_all( block )
selves =3D [ ]
=20
#find_all'
f =3D lambda { |s|=20
if block.call( s )
selves.push( s )=20
elsif s.children
s.children.each { |c| f.call( c ) }
else
return
end
}
=20
f.call( self )
=20
return selves
end
Then I can call it like this:
t.find_all( lambda { |x| return x if x.whatever_attribute =3D=3D =
'anything' } )
It works, but any thoughts or input are much appreciated.
Regards
Iain=
I've written the method below to recurse through a non-binary tree of =
arbitrary depth. I'm still new to Ruby and was wondering if there were =
some Rubyism's (or just general pointers) that might improve it?
Each object 't' holds an array 'children' of objects of the same type as =
t, which can themselves hold further arrays. Each object also has the =
attribute 'parent' holding a reference to the parent object.
def find_all( block )
selves =3D [ ]
=20
#find_all'
f =3D lambda { |s|=20
if block.call( s )
selves.push( s )=20
elsif s.children
s.children.each { |c| f.call( c ) }
else
return
end
}
=20
f.call( self )
=20
return selves
end
Then I can call it like this:
t.find_all( lambda { |x| return x if x.whatever_attribute =3D=3D =
'anything' } )
It works, but any thoughts or input are much appreciated.
Regards
Iain=