Ruby toddler here (3 days playing with Ruby, probably my terminology is yet imprecise).
For study reason I would like to modify the following code to accept ticket.request("triple",10)
(or equivalent syntax) in the second last line.
Is it possible (without heavily changing ticket.request
)?
ticket = Object.new
def ticket.price
return 3.5
end
def ticket.triple(c)
return 3*c
end
# direct call
puts ticket.triple(10) # ===> 30
# pointer to method
pt = ticket.method(:triple)
puts pt.call(10) # ===> 30
def ticket.request(request) # <=== should it be modified?
if self.respond_to?(request)
self.__send__(request)
end # nil otherwise
end
puts [
ticket.price, # direct ===> 3.5
ticket.request("price"), # safe ===> 3.5
ticket.request("name") # it does not exist ===> nil
# ticket.request("triple", 10) <========================= syntax ????
]