tyliong
2008-Oct-28 15:17 UTC
Passing a function with arguments to another function with arguments that is a loop
Hi, I am a noobie. I just want to know what is the standard or most effective way of doing this I have two functions def display_price(grocery,batch) grocery_price = grocery.send(batch).price if grocery_price number_to_currency(grocery_price) else "NA" end end def loop(func,matrix) matrix.each { |matrix| display_price(@grocery,matrix) } end the "func argument" in the loop function is suppose to be display_price but how do i put it in if the "batch" is the thing that I want to be looped through? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pardee, Roy
2008-Oct-29 11:43 UTC
Re: Passing a function with arguments to another function with arguments that is a loop
You can create a pointer to your display_price method with a Proc object, e.g.: this_func = Proc.new {|groc, bat| display_price(groc, bat)} loop(this_func, batch) Your loop method invokes the actual function with the .call method, e.g.: def loop(func, matrix) # Note that .each will almost certainly not yield the matrix object itself. matrix.each { |matrix_item| func.call(@grocery, matrix) } end No clue if that will work for your actual application--trying to figure out what you might be doing is making my head hurt. ;-) This feels like it''s well outside of noob-land. If you back up & tell us more about your underlying goals, we may be able to give advice for staying w/more straightforward processing. HTH, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of tyliong Sent: Tuesday, October 28, 2008 8:17 AM To: Ruby on Rails: Talk Subject: [Rails] Passing a function with arguments to another function with arguments that is a loop Hi, I am a noobie. I just want to know what is the standard or most effective way of doing this I have two functions def display_price(grocery,batch) grocery_price = grocery.send(batch).price if grocery_price number_to_currency(grocery_price) else "NA" end end def loop(func,matrix) matrix.each { |matrix| display_price(@grocery,matrix) } end the "func argument" in the loop function is suppose to be display_price but how do i put it in if the "batch" is the thing that I want to be looped through? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Oct-31 10:33 UTC
Re: Passing a function with arguments to another function with arguments that is a loop
In the end I did it this way. def loop_td(func,matrix,grocery,name) holder = ["<td>" + name + "</td>"] for x in matrix holder << ("<td>" + send(func,grocery,x) + "</td>") end return holder end display_price is the func I want to insert def display_price(grocery,batch) grocery_price = grocery.send(batch).price if grocery_price number_to_currency(grocery_price) else "NA" end end The underlying goal was to make a function where I could automatically generate a table row that used the same function in each <td> for with the same array. -yl(thanks for answering btw) -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Pardee, Roy Sent: Wednesday, October 29, 2008 7:43 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Passing a function with arguments to another function with arguments that is a loop You can create a pointer to your display_price method with a Proc object, e.g.: this_func = Proc.new {|groc, bat| display_price(groc, bat)} loop(this_func, batch) Your loop method invokes the actual function with the .call method, e.g.: def loop(func, matrix) # Note that .each will almost certainly not yield the matrix object itself. matrix.each { |matrix_item| func.call(@grocery, matrix) } end No clue if that will work for your actual application--trying to figure out what you might be doing is making my head hurt. ;-) This feels like it''s well outside of noob-land. If you back up & tell us more about your underlying goals, we may be able to give advice for staying w/more straightforward processing. HTH, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of tyliong Sent: Tuesday, October 28, 2008 8:17 AM To: Ruby on Rails: Talk Subject: [Rails] Passing a function with arguments to another function with arguments that is a loop Hi, I am a noobie. I just want to know what is the standard or most effective way of doing this I have two functions def display_price(grocery,batch) grocery_price = grocery.send(batch).price if grocery_price number_to_currency(grocery_price) else "NA" end end def loop(func,matrix) matrix.each { |matrix| display_price(@grocery,matrix) } end the "func argument" in the loop function is suppose to be display_price but how do i put it in if the "batch" is the thing that I want to be looped through? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---