I have following code. [code] class Person def eat puts "I am eating" end def sleep puts "I am going to bed" end def laugh puts "Wahahaha" end def talk puts "...." end end little_boy = Person.new puts ''Input an action'' request = gets.chomp if request == ''eat'' little_boy.eat elsif request == ''sleep'' little_boy.sleep elsif request == ''laugh'' little_boy.laugh elsif request == ''talk'' little_boy.talk else puts ''Input error'' end [/code] Now I would like to use method: respond_to? and send to simplify it.How would it be? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
class Person ... def run action eval action.to_s # calls a sttring as if it were a method end end little_boy = Person.new puts ''Input an action'' request = gets.chomp if little_boy.respond_to?( request ) little_boy.run request else puts "invalid action #{request}" end hope it helps j ps: All this functionality works for any ruby object, not only rails models. On Jul 7, 9:34 am, Ruby Rails <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have following code. > [code] > class Person > def eat > puts "I am eating" > end > def sleep > puts "I am going to bed" > end > def laugh > puts "Wahahaha" > end > def talk > puts "...." > end > end > little_boy = Person.new > puts ''Input an action'' > request = gets.chomp > if request == ''eat'' > little_boy.eat > elsif request == ''sleep'' > little_boy.sleep > elsif request == ''laugh'' > little_boy.laugh > elsif request == ''talk'' > little_boy.talk > else > puts ''Input error'' > end > [/code] > > Now I would like to use method: respond_to? and send to simplify it.How > would it be? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-07 12:09 UTC
Re: How to simplify the following code with respond_to?
On 7 Jul 2008, at 13:04, Wolas! wrote:> > class Person > > ... > > def run action > eval action.to_s # calls a sttring as if it were a method > end > endIf you''re just going to call a method you''re better off just calling send Fred.> > > little_boy = Person.new > > puts ''Input an action'' > request = gets.chomp > > if little_boy.respond_to?( request ) > little_boy.run request > else > puts "invalid action #{request}" > end > > hope it helps > > j > > ps: All this functionality works for any ruby object, not only rails > models. > > On Jul 7, 9:34 am, Ruby Rails <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> I have following code. >> [code] >> class Person >> def eat >> puts "I am eating" >> end >> def sleep >> puts "I am going to bed" >> end >> def laugh >> puts "Wahahaha" >> end >> def talk >> puts "...." >> end >> end >> little_boy = Person.new >> puts ''Input an action'' >> request = gets.chomp >> if request == ''eat'' >> little_boy.eat >> elsif request == ''sleep'' >> little_boy.sleep >> elsif request == ''laugh'' >> little_boy.laugh >> elsif request == ''talk'' >> little_boy.talk >> else >> puts ''Input error'' >> end >> [/code] >> >> Now I would like to use method: respond_to? and send to simplify >> it.How >> would it be? >> -- >> Posted viahttp://www.ruby-forum.com/. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---