# Passing a method call into a method

**URL:** <https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540>\
**Category:** Ruby\
**Created:** [September 14, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540 "2008-09-14T15:26:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [September 14, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540/1 "2008-09-14T15:26:01Z")

</div>

Hi,

I am trying to figure out how to pass a method call into a method.

I wrote a generic method that call be called on an array of numbers, and  
returns an array of the index values in the receiver that meet a  
specified condition:

class Array  
def hash\_of\_indexes  
if self.empty?  
‘The array is empty.’  
else  
h = {}  
self.each\_with\_index { |e, i| h.include?(e.to\_f) ? h[e.to\_f] \<\< i  
: h[e.to\_f] = [i] }  
h  
end  
end

def indexes\_of  
self.empty? ? ‘The array is empty.’ : hash\_of\_indexes[self.min]  
end  
end

puts [1, -100, -200.0, 1000000000000.12, 3, 1, -100, -100,  
-200].indexes\_of.inspect # --\> returns [2, 8]

What I’d like to do is to be able to specify what the condition is when  
calling the array, but can’t get that to work right. I tried using  
yield inside of the square brackets in place of self.min above, and then  
calling the method with a block with self.min inside the block, but this  
results in an error (undefined method `min’ for main:Object  
(NoMethodError)).

I have a feeling that I might need to use a lambda or proc to get this  
to work correctly, but am not sure about the syntax for either of those.

Any help would be appreciated.

Thanks,

Glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [September 14, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540/2 "2008-09-14T19:06:27Z")

</div>

> ```
> h
> 
> ```
> 
> inside of the square brackets in place of self.min above, and then calling  
> the method with a block with self.min inside the block, but this results in  
> an error (undefined method `min’ for main:Object (NoMethodError)).
> 
> I have a feeling that I might need to use a lambda or proc to get this to  
> work correctly, but am not sure about the syntax for either of those.

It’s a little unclear from this which part of the procedure you want to  
parameterize, but my guess is that you’d do this:

class Array  
def indexes\_of(value = nil)  
value = yield(self) if block\_given?  
self.empty? ? ‘The array is empty.’ : hash\_of\_indexes[value.to\_f]  
end  
end

Then you’d call it on an array with a value or a block:

some\_array.indexes\_of(7)  
some\_array.indexes\_of { |ary| ary.min }

My other guess is that you want something like Array#select, but which  
returns indexes:

class Array  
def indexes\_of  
indexes = []  
each\_with\_index { |e, i| indexes \<\< i if yield(e) }  
indexes  
end  
end

[3,9,2,7,4,7].indexes\_of { |x| x \> 5 }  
#=\> [1, 3, 5]

To explain, you have a block – { |x| x \> 5 } – that returns true or  
false.  
Inside your method, you call the block with the current element using  
yield(e) to determine whether to add the current index to the list.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [September 14, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540/3 "2008-09-14T22:41:07Z")

</div>

On Sep 14, 8:18 am, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if self.empty?
> 'The array is empty.'
> else
> h = {}
> self.each_with_index { |e, i| h.include?(e.to_f) ? h[e.to_f] << i : h[e.to_f] = [i] }
> h
> end
> 
> ```
> 
> end

class Array  
def hash\_of\_indexes  
h = {}  
each\_with\_index{ |e,i|  
e = e.to\_f  
h[e] = Array( h[e] ) \<\< i }  
h  
end  
end

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [September 15, 2008, 2:11am UTC](https://www.ruby-forum.com/t/passing-a-method-call-into-a-method/148540/4 "2008-09-15T02:11:24Z")

</div>

Glenn wrote:

> class Array  
> def hash\_of\_indexes  
> if self.empty?  
> ‘The array is empty.’  
> else  
> h = {}  
> self.each\_with\_index { |e, i| h.include?(e.to\_f) ? h[e.to\_f] \<\< i  
> : h[e.to\_f] = [i] }  
> h  
> end  
> end
> 
> def indexes\_of  
> self.empty? ? ‘The array is empty.’ : hash\_of\_indexes[self.min]  
> end  
> end

Use send, as in:

class Array  
def indexes\_of(function)  
self.empty? ? ‘The array is empty.’ :  
hash\_of\_indexes[self.send(function)]  
end  
end

puts [1, -100, -200.0, 1000000000000.12, 3, 1, -100, -100,  
-200].indexes\_of(:min)
