passing function into another function

J

Joe Van Dyk

Hi,

I couldn't work out the syntax for the following:

class Reports
def self.This; end
def self.That; end
end

def do_something_with_a_report report
# calls Reports::This or Reports::That, dtermined by what report is
# something like report.call ?
end

I want to do something like:

do_something_with_a_report Reports::This

But that calls Reports::This and sends that value to the
do_something_with_a_report function.

In other words, I want to do something like passing a function pointer
to a function in C.
 
A

ara.t.howard

Hi,

I couldn't work out the syntax for the following:

class Reports
def self.This; end
def self.That; end
end

def do_something_with_a_report report
# calls Reports::This or Reports::That, dtermined by what report is
# something like report.call ?
end

I want to do something like:

do_something_with_a_report Reports::This

But that calls Reports::This and sends that value to the
do_something_with_a_report function.

In other words, I want to do something like passing a function pointer
to a function in C.

harp:~ > cat a.rb
class Reports
def self.This; p 'This'; end
def self.That; p 'That'; end
This = method 'This'
That = method 'That'
end

def do_something_with_a_report report
report.call
end

do_something_with_a_report Reports::This
do_something_with_a_report Reports::That


harp:~ > ruby a.rb
"This"
"That"

you could also just

do_something_with_a_report Reports::method('This')
do_something_with_a_report Reports::method('That')


-a
 
J

Joe Van Dyk

harp:~ > cat a.rb
class Reports
def self.This; p 'This'; end
def self.That; p 'That'; end
This = method 'This'
That = method 'That'
end

def do_something_with_a_report report
report.call
end

do_something_with_a_report Reports::This
do_something_with_a_report Reports::That


harp:~ > ruby a.rb
"This"
"That"

you could also just

do_something_with_a_report Reports::method('This')
do_something_with_a_report Reports::method('That')

Ah, Object#method. Thank you both!

Joe
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top