Heading in right direction with a "reach" definition that exte

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1091 Views - Last Post: 29 March 2011 - 05:53 PM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Heading in right direction with a "reach" definition that exte

Posted 22 March 2011 - 11:23 AM

This project involves adding a method reach to all objects. The reach method is like the standard each method, except that it applies to the leaves of container objects. A leaf is just a method that does not define the each method.

Place your code in a file that can be required as shown above. It should re-open class Object and define the reach method. 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.

I am just getting started on this project and just wanted a heads up that I am heading in the right direction...
class String
  remove_method(:each)
end

class Object
  def reach
    if(x.respond_to?(:each))
      yield(self):
    else
      self.each
      
  end
end


#test(remove before submitting)
[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}

gets #waits for user to hit enter to exit the program



Is This A Good Question/Topic? 0
  • +

Replies To: Heading in right direction with a "reach" definition that exte

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 907
  • View blog
  • Posts: 3,173
  • Joined: 12-May 09

Re: Heading in right direction with a "reach" definition that exte

Posted 22 March 2011 - 11:32 AM

Kind of in the right direction. In your reach method, you aren't checking x for each, that was just an example. You want to ask self if you have reach, using
self.each


Second, you're supposed to yield to self if you don't have each. Your if/else statements are swapped.
Was This Post Helpful? 0
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 22 March 2011 - 12:05 PM

so my else part will say

else
   yield(self)



and my if will be checking to see if reach is present??
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 907
  • View blog
  • Posts: 3,173
  • Joined: 12-May 09

Re: Heading in right direction with a "reach" definition that exte

Posted 22 March 2011 - 12:15 PM

Yes, basically swap the statements inside the if and else.
Was This Post Helpful? 0
  • +
  • -

#5 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 22 March 2011 - 12:15 PM

class String
  remove_method(:each)
end

class Object
  def reach
    #walking through the array
    self.each{|x}
    
    #checking if responds to each and if so prints it out else
    # returns the objects yielded
    if(x.respond_to?(:each))
     self.each {|x| print x, "\n"}
    else
       yield(self)
      
  end
end


#test(remove before submitting)
[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}

gets #waits for user to hit enter to exit the program


Was This Post Helpful? 0
  • +
  • -

#6 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 27 March 2011 - 01:21 PM

ok, I am struggling big time on this project... not sure where to go.. but here is my idea
Basically saying that if the array responds to each print itself out else yield on each separate item... not sure where to go from here???

class String
  remove_method(:each)
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))
      print (self.each{|x|}) 
    else
      yield(self)
      
  end
end


#test(remove before submitting)
[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}

gets #waits for user to hit enter to exit the program


Was This Post Helpful? 0
  • +
  • -

#7 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 28 March 2011 - 08:08 AM

Ok, I have done some more work on my project... it looks like it is working but i am getting an error and getting kicked out of the program.. not sure where the error is:
class String
  remove_method(:each)
end

class Object
  def reach &x
    #checking if responds to each and if so prints it out else
    # returns the objects yielded
    each do |e|
     x.call(e)
      if(e.respond_to?(:each))
        e.reach(&x)
      else
        yield(self)
        self.each{|x| print x, "\n"}
      end
    end
  end
end


#test(remove before submitting)
[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}
[4, [13, 88], [19, "fred", "snark"], "alice"].each { |x| print x, "\n"}
[4, [13, 88], [19, "fred", "snark"], "alice"].reach { |x| print x, "\n"}
gets #waits for user to hit enter to exit the program


This post has been edited by SnoBunny85: 28 March 2011 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 06:41 AM

Since i have to test the each method and the reach method do i need to have two definitions in my class object... one for each and one for reach??

def each

end

def reach

end



???
Was This Post Helpful? 0
  • +
  • -

#9 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 12:54 PM

View PostSnoBunny85, on 27 March 2011 - 01:21 PM, said:

ok, I am struggling big time on this project... not sure where to go.. but here is my idea
Basically saying that if the array responds to each print itself out else yield on each separate item... not sure where to go from here???

class String
  remove_method(:each)
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))
      print (self.each{|x|}) 
    else
      yield(self)
      
  end
end


#test(remove before submitting)
[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}

gets #waits for user to hit enter to exit the program



You're on the right track here, you want to focus in on what happens if the object does respond_to?(:each]. Read back over what the assignment said:

Quote

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.


This is what you need to implement. So, where you have print (self.each{|x|}) you want to instead call the each method on self, and for each item call reach, and when you call reach pass a block that yields the argument.
Was This Post Helpful? 0
  • +
  • -

#10 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 03:43 PM

Ok i have this code now...
class String
  remove_method(:each)
end

class Object
  def reach
    if(x.respond_to?(:each))
      self.each{ .. i think i need something in here just don't know what... }
      self.reach
    else
      yield(self)
    end
  end
end



Was This Post Helpful? 0
  • +
  • -

#11 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 04:49 PM

Right, so inside that area you need to call reach on each element.

Quote

call reach on each component

Was This Post Helpful? 0
  • +
  • -

#12 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 05:27 PM

by doing self.reach ???

class String
  remove_method(:each)
end

class Object
  def reach
    if(x.respond_to?(:each))
      self.each{self.reach}
      self.reach
    else
      yield(self)
    end
  end
end

also, if i do that inside the brackets do i still need to do it again under self.each??? 



Was This Post Helpful? 0
  • +
  • -

#13 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 05:33 PM

On each element. Like when you printed out each element{ |x| print x, "\n" }, instead call x.reach.
Was This Post Helpful? 0
  • +
  • -

#14 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 05:40 PM

class String
  remove_method(:each)
end

class Object
  def reach
    if(x.respond_to?(:each))
      self.each{x.reach {|x| print x, "\n"}}
    else
      yield(self)
    end
  end
end



like that??? or do i need to put the second self.reach back in... I am also getting an error that it says I have undefined variable or method 'x' in my reach... do i need to declare my x somewhere else.. my examples are as follows:

[4, 13, 18, "fred", "alice"].each { |x| print x, "\n"}
[4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}
[4, [13, 88], [19, "fred", "snark"], "alice"].each { |x| print x, "\n"}
[4, [13, 88], [19, "fred", "snark"], "alice"].reach { |x| print x, "\n"}


Was This Post Helpful? 0
  • +
  • -

#15 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Heading in right direction with a "reach" definition that exte

Posted 29 March 2011 - 05:46 PM

You're getting there. The error is probably referring to x.respond_to?(:each). Right there you want to know if self has an each (because that's what you're going to be calling in the next line).

Just noticed also in the each you just call x without getting it first { |x| x.reach }.

After that, all you should need is to yield inside the innermost reach.

Quote

sending it a code block that simply calls yield on its argument.

This post has been edited by 5thWall: 29 March 2011 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2