从命令行调用 ruby 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10316495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call ruby function from command-line
提问by user1251007
How can I directly call a ruby function from the command-line?
如何从命令行直接调用 ruby 函数?
Imagine, I would have this script test.rb:
想象一下,我会有这个脚本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).
如果从命令行 ( ruby test.rb)运行此脚本,则不会发生任何事情(如预期)。
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 test.rb TestClass.test_function('someTextString')吗?我想获得以下输出:I got the following variable: someTextString.
回答by Candide
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..
首先类的名称需要以大写字母开头,并且由于您确实要使用静态方法,因此函数名称定义需要以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_functionas an instance method, you'd have:
如果您改为test_function使用实例方法,您将拥有:
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'"
回答by Rob Davis
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:
这是另一种变体,如果您发现在命令行键入 ruby 语法很尴尬,并且您真的只想将 args 传递给 ruby。这是 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 可执行并像这样运行它:
./test.rb "Some Value"
Or run it like this:
或者像这样运行它:
ruby test.rb "Some Value"
This works because ruby automatically sets the ARGVarray to the arguments passed to the script. You could use ARGV[0]or ARGV.firstto get the first argument, or you could combine the args into a single string, separated by spaces, using ARGV.join(' ').
这是有效的,因为 ruby 会自动将ARGV数组设置为传递给脚本的参数。您可以使用ARGV[0]或ARGV.first来获取第一个参数,或者您可以使用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.
如果您正在执行大量命令行操作,您最终可能会用到Shellwords,它位于标准 ruby 库中。
回答by Lunero
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'"
回答by DigitalRoss
#!/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 environmentcalled 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.)
编写 Ruby 脚本的常用方法是仅使用名为main的顶级执行环境。您可以开始定义在类之外编写的方法和代码,这些将直接执行。(顺便说一句,在类内但在任何方法之外的代码也将“自行”运行。)
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.rband it will do what you want. Or you can chmod +x test.rb; ./test.rbsince we did add a shebang.
有了这个,您只需输入$ ruby test.rb,它就会做您想做的事。或者你可以,chmod +x test.rb; ./test.rb因为我们确实添加了一个shebang。
回答by NilsHaldenwang
回答by Kaey
If you know that how to call an rb file from commandline
如果您知道如何从命令行调用 rb 文件
ruby yourfile.rb
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
回答by user1251007
Just an extension to Ingenu's answer for the case that the function does not print something out, but does return something.
只是 Ingenu 对函数不打印某些内容但确实返回某些内容的回答的扩展。
We would have the following test.rb
我们将有以下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')"

