C
Cheryl M.
The reach method is like the standard each method, except that it
applies to the leaves of container objects.
It takes : [4, [13, 88], [19, "fred", "snark"], "alice"].each { |x|
print x, "\n"}
and makes it look like:
4
13
88
19
fred
snark
alice
i have this so far::
class String
remove_methodeach)
end
class Object
def reach
#checking if responds to each and if so prints it out else
# returns the objects yielded
if(x.respond_to?each))
self.each{|x|}
else
yield(self)
end
end
I am not sure what to do in the if part of this problem...
The body of reach checks whether the self has the each method. If not,
simply yield(self); otherwise run the each method on yourself and call
reach on each component, sending it a code block that simply calls yield
on its argument. To find out if an object x has a particular method m,
you may use x.respond_to?(m), where m is a string giving the name.
applies to the leaves of container objects.
It takes : [4, [13, 88], [19, "fred", "snark"], "alice"].each { |x|
print x, "\n"}
and makes it look like:
4
13
88
19
fred
snark
alice
i have this so far::
class String
remove_methodeach)
end
class Object
def reach
#checking if responds to each and if so prints it out else
# returns the objects yielded
if(x.respond_to?each))
self.each{|x|}
else
yield(self)
end
end
I am not sure what to do in the if part of this problem...
The body of reach checks whether the self has the each method. If not,
simply yield(self); otherwise run the each method on yourself and call
reach on each component, sending it a code block that simply calls yield
on its argument. To find out if an object x has a particular method m,
you may use x.respond_to?(m), where m is a string giving the name.