# Passing a function with arguments to another function with arguments that is a loop

**URL:** <https://www.ruby-forum.com/t/passing-a-function-with-arguments-to-another-function-with-arguments-that-is-a-loop/151973>\
**Category:** Rails\
**Created:** [October 28, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/passing-a-function-with-arguments-to-another-function-with-arguments-that-is-a-loop/151973 "2008-10-28T16:18:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [October 28, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/passing-a-function-with-arguments-to-another-function-with-arguments-that-is-a-loop/151973/1 "2008-10-28T16:18:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [October 29, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/passing-a-function-with-arguments-to-another-function-with-arguments-that-is-a-loop/151973/2 "2008-10-29T12:44:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [October 31, 2008, 11:24am UTC](https://www.ruby-forum.com/t/passing-a-function-with-arguments-to-another-function-with-arguments-that-is-a-loop/151973/3 "2008-10-31T11:24:52Z")

</div>

In the end I did it this way.

def loop\_td(func,matrix,grocery,name)  
holder = ["

" + name + “”]  
for x in matrix  
holder \<\< ("" + send(func,grocery,x) + “”)  
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

 for with  
the  
same array.

-yl(thanks for answering btw)
