ruby - 如何从命令行将数组作为参数传递给 Ruby 函数?

标签 ruby

我想从命令行以数组作为参数调用 Ruby 函数。

脚本名为test.rb

在下面的代码中,Environments are like test,dev,uat.', am passing as ['test','dev','uat']

我试过如下:

ruby -r "./test.rb" -e "start_services '['dev','test','uat']','developer','welcome123'"
def start_services(environments,node_user_name,node_password)
  environments.each do |env|
    puts env
  end
  puts node_user_name
  puts node_password
end

输出:

-e:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input start_services '['dev','test','uat']','developer',' ^

最佳答案

您显然想将一个数组作为第一个参数传递给 start_services 方法,因此您应该这样做:

$ ruby -r "./test.rb" -e "start_services ['dev','test','uat'],'developer','welcome123'"
# output:
dev
test
uat
developer
welcome123

到目前为止,您一直在尝试传递格式错误的 '[\'dev\',\'test\',\'uat\']' 字符串,因为您没有转义 ' 字符。

关于ruby - 如何从命令行将数组作为参数传递给 Ruby 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770103/

相关文章:

ruby-on-rails - 如何在 Ruby 中正确使用 guard 子句

ruby-on-rails - rails 3 : Get Random Record

ruby - 使用 Rails 4 和 Devise 3.1.0 登录子域

ruby - Amazon 基于前缀的 S3 策略不起作用(AWS、IAM、STS、Ruby)

ruby - 在 Ruby 中,为什么 "omega-3 (dHA)".gsub(/\b([a-z])/, '\0' .upcase) 不起作用?

ruby-on-rails - 如何在 en.yml 上格式化带有 “th” 后缀的日期?

ruby-on-rails - rails 加密/解密

ruby-on-rails - 回到 Rails

c - 为什么我的 C 扩展没有创建唯一实例?

python - Ruby 的 bundler/Perl 的纸箱的 Python 等价物是什么?