Call ruby function from command-line

RubyCommand Line

Ruby Problem Overview


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.

Ruby Solutions


Solution 1 - Ruby

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'"

Solution 2 - Ruby

Here's another variation, if you find that typing ruby syntax at the command line is awkward and you really just want to pass args to ruby. Here's test.rb:

#!/usr/bin/env ruby

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

TestClass.test_function(ARGV[0])

Make test.rb executable and run it like this:

./test.rb "Some Value"

Or run it like this:

ruby test.rb "Some Value"

This works because ruby automatically sets the ARGV array to the arguments passed to the script. You could use ARGV[0] or ARGV.first to get the first argument, or you could combine the args into a single string, separated by spaces, using ARGV.join(' ').

If you're doing lots of command-line stuff, you may eventually have a use for Shellwords, which is in the standard ruby lib.

Solution 3 - Ruby

If you have multiple arguments to call in a example like this:

class TestClass
  def self.test_function(some_var1, some_var2)
    puts "I got the following variables: #{some_var1}, #{some_var2}"
  end
end

run it like this (the arguments need to be comma separated in this case)

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

Solution 4 - Ruby

#!/usr/bin/env ruby

class A
  def run
    p :Hello_world
  end
  self
end.new.run

The usual way to script Ruby is to just use the top level execution environment called main. You can just start defining methods and code you write outside of a class, and these will be executed directly. (BTW, code inside a class but outside any method will run "by itself" also.)

Anyway, I'm with you ... I like writing all code in a named class and instantiating that class, so you can combine the techniques .. have the class return its own object .. and then use just a little of that top level code to allocate, initialize, and then dot into the class.

With this, you can just type $ ruby test.rb and it will do what you want. Or you can chmod +x test.rb; ./test.rb since we did add a shebang.

Solution 5 - Ruby

If you are working on a command line interface, then I would suggest to have a look at [thor] 1.

Thor directly maps your commands to methods within the defined class, see the thor wiki for an example.

Solution 6 - Ruby

Just an extension to Ingenu's answer for the case that the function does not print something out, but does return something.

We would have the following test.rb

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

Then to invoke that from the command line and get the return value:

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

Solution 7 - Ruby

If you know that how to call an rb file from commandline

ruby yourfile.rb

This can do the whole trick for you.

What you have done is, just defined your methods in the class. Now you can call it below the definition. If you still want to read, wide open your eyes

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1251007View Question on Stackoverflow
Solution 1 - RubyCandideView Answer on Stackoverflow
Solution 2 - RubyRob DavisView Answer on Stackoverflow
Solution 3 - RubyLuneroView Answer on Stackoverflow
Solution 4 - RubyDigitalRossView Answer on Stackoverflow
Solution 5 - RubyNilsHaldenwangView Answer on Stackoverflow
Solution 6 - Rubyuser1251007View Answer on Stackoverflow
Solution 7 - RubyKaeyView Answer on Stackoverflow