# How to call a method within another method

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046>\
**Category:** Ruby\
**Created:** [October 5, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046 "2010-10-05T23:58:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prichard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@prichard](https://www.ruby-forum.com/u/prichard)\
**Post date:** [October 5, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046/1 "2010-10-05T23:58:03Z")

</div>

Hi, I have a method…

def make\_pay\_loan

end

I want this method to ‘activate’ (for want of a better word) another  
method called pay\_loan.

Any tips on how this could be done are welcome, thanks

---

<div class="post-metadata">

**Author:** ![prichard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@prichard](https://www.ruby-forum.com/u/prichard)\
**Post date:** [October 6, 2010, 12:22am UTC](https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046/2 "2010-10-06T00:22:27Z")

</div>

On 10/5/2010 5:00 PM, Paul R. wrote:

> Any tips on how this could be done are welcome, thanks  
> I think you want to dynamically define one method from within another.  
> Try this:

def make\_pay\_loan  
def pay\_loan  
puts ‘paid’  
end  
end

# Calling pay\_loan here will cause an error since the method is

# not yet defined.

#pay\_loan

# This will define the pay\_loan method.

make\_pay\_loan

# This outputs “paid”. 🙂

pay\_loan

-Jeremy

---

<div class="post-metadata">

**Author:** ![prichard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@prichard](https://www.ruby-forum.com/u/prichard)\
**Post date:** [October 6, 2010, 4:53am UTC](https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046/3 "2010-10-06T04:53:58Z")

</div>

On Oct 5, 2010, at 15:22 , Jeremy B. wrote:

> > Any tips on how this could be done are welcome, thanks
> 
> I think you want to dynamically define one method from within another.  
> Try this:
> 
> def make\_pay\_loan  
> def pay\_loan  
> puts ‘paid’  
> end  
> end

But there is nothing dynamic about that solution. All you’re doing is  
defining a def expression inside of another which will get defined the  
first time (and every time for that matter) make\_pay\_loan gets called.  
pay\_loan is entirely static and has no ability to be dynamic in any way.

You’ll want to use define\_method or eval in order to actually be  
dynamic.

% echo "def make\_pay\_loan

> def pay\_loan  
> puts ‘paid’  
> end  
> end  
> " | parse\_tree\_show  
> s(:defn, :make\_pay\_loan,  
> s(:args),  
> s(:scope,  
> s(:block,  
> s(:defn, :pay\_loan,  
> s(:args),  
> s(:scope,  
> s(:block, s(:call, nil, :puts, s(:arglist, s(:str, “paid”)))))))))

Depending on how the OP means “activate”, it could be as simple as  
calling public on the method to make it visible, or as complex as  
defining a dynamic method with define\_method and using the closure to  
capture the dynamic values passed in to make\_pay\_loan.

---

<div class="post-metadata">

**Author:** ![prichard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@prichard](https://www.ruby-forum.com/u/prichard)\
**Post date:** [October 6, 2010, 6:12am UTC](https://www.ruby-forum.com/t/how-to-call-a-method-within-another-method/196046/4 "2010-10-06T06:12:59Z")

</div>

On 10/05/2010 09:53 PM, Ryan D. wrote:

> > > I want this method to ‘activate’ (for want of a better word) another  
> > > end  
> > > end
> 
> But there is nothing dynamic about that solution. All you’re doing is defining a def expression inside of another which will get defined the first time (and every time for that matter) make\_pay\_loan gets called. pay\_loan is entirely static and has no ability to be dynamic in any way.

All I’m doing is attempting to answer the question as asked without  
adding too much at once for a new user for whom this might appear quite  
dynamic. The terminology will sort itself out in time. 🙂

> You’ll want to use define\_method or eval in order to actually be dynamic.
> 
> Depending on how the OP means “activate”, it could be as simple as calling public on the method to make it visible, or as complex as defining a dynamic method with define\_method and using the closure to capture the dynamic values passed in to make\_pay\_loan.

You’re correct that define\_method and eval are able to create truly  
dynamic definitions and that “activate” can have other interpretations;  
however, I don’t believe that being overly pedantic is particularly  
helpful in this case. If you have a better answer to the OP’s question,  
please provide it, even if it’s just asking for clarification. 😉

-Jeremy
