Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:50:30+00:00 2026-06-10T00:50:30+00:00

I am writing something as follows module Test def self.print(*args) p ‘something’ p args

  • 0

I am writing something as follows

module Test
   def self.print(*args)
      p 'something'
      p args
   end
end

def print(*args)
  p "print something"
  p args
end

def method_caller(method_name, *args)
  send(method_name, *args)
end

method_caller(:print, 2) # this works fine
method_caller("print", 2, 3) # this one also
method_caller("Test.print", 2) # this doesn't work

Basically, I pass method_caller the name of a method and some arguments and then I use the send method to actually invoke the method.

I usually pass in the name of the method as a symbol, but how would I deal with Test.print? I imagine at some point I might be passing in objects and having them call their own methods.

The method I would like to invoke could be literally anywhere.

UPDATE:

Trying out each suggestion, the explicit receiver looks like a good way to do it since it’s obvious what is happening, but what about christianblais’ idea. I changed it to avoid the eval call as such

def method_caller(method_name, *args)
  if method_name.is_a?(String)
    chain = method_name.split('.')
    obj, method_name = Object.const_get(chain[0...-1].join('.')), chain[-1]
    obj.send(method_name, *args)
  else
    send(method_name, args)
  end
end

Which means I can just say method_caller("Test.print", 2), but maybe there are some gotchas…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:50:32+00:00Added an answer on June 10, 2026 at 12:50 am

    Well, why don’t you give your method_caller a receiver argument?

    def method_caller(receiver, method_name, *args)
      receiver.send(method_name, *args)
    end
    
    method_caller(Test, :print, 2)
    

    The other calls need to use self as the receiver, which is implicit in your current implementation. But finally all this gives you is an incomplete ad hoc reimplementation of send, so one really wonders what you are trying to achieve with that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that writing something like ++a = a++; Is not only unreadable but
I am writing something that will allow users to search through a log of
What is a good way of writing something like this in one line. The
Heys, I am writing something in Xcode. I use Core Data for persistency and
This morning, I find myself writing something like: if (a == b == c):
I have written some relatively simple jQuery plug-ins, but I am contemplating writing something
That is to say, let's say I'm writing something that's hosted on foo.com .
When writing views and nesting views within deeper views I sometimes miss something and
I'm writing a prototype to prove the feasibility of something I'm working on. Basically,
I'm writing a Windows batch file and want to copy something to the desktop.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.