How to call a method within another method

P

Paul Roche

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
 
J

Jeremy Bopp

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

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
 
R

Ryan Davis

=20
I think you want to dynamically define one method from within another.
Try this:
=20
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.=
 
J

Jeremy Bopp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top