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 7835765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:47:52+00:00 2026-06-02T13:47:52+00:00

How can I directly call a Ruby function from the command line? Imagine, I

  • 0

How can I directly call a Ruby function from the command line?

Imagine, I would have this script test.rb:

class TestClass
    def self.test_function(some_var)
        puts "I got the following variable: #{some_var}"
    end
end

If this script is run from the command line (ruby test.rb), nothing happens (as intended).

Is there something like ruby test.rb TestClass.test_function('someTextString')?

I want to get the following output: I got the following variable: someTextString.

  • 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-02T13:47:55+00:00Added an answer on June 2, 2026 at 1:47 pm

    First the name of the class needs to start with a capital letter, and since you really want to use a static method, the function name definition needs to start with self..

    class TestClass
        def self.test_function(someVar)
            puts "I got the following variable: " + someVar
        end
    end
    

    Then to invoke that from the command line you can do:

    ruby -r "./test.rb" -e "TestClass.test_function 'hi'"
    

    If you instead had test_function as an instance method, you’d have:

    class TestClass
        def test_function(someVar)
            puts "I got the following variable: " + someVar
        end
    end
    

    then you’d invoke it with:

    ruby -r "./test.rb" -e "TestClass.new.test_function 'hi'"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you directly call a native function exported from a DLL? Can someone
I call ant command in my ruby script (on Windows XP) using %x[]. I
Can I define a static method which I can call directly on the class
I can call the webservie directly to the browser with the following URL and
I have created a page on my site where users can post directly to
I have page, i can contact directly without any problems. http://www.mysite.com/app_dev.php/comments/22 the page shows
JS library like JQuery can be linked directly from other site (e.g. google). Usually
In this Ruby code: Module M Class C < Struct.new(:param) def work M::helper(param) end
In Javascript, we can call methods on string literals directly without enclosing it within
I am calling a Page Method from JQuery as described here: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ This is

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.