Ruby 将字符串数组转换为字符串

Ruby convert string array to string

我有一个 ruby 字符串数组值,我想将其作为字符串值获取。我正在使用 ruby 和厨师食谱。 运行 在 windows 平台中。代码-

version_string = Mixlib::ShellOut.new('some.exe -version').run_command     
Log.info(version.stdout.to_s)    
extract_var = version_string.stdout.to_s.lines.grep(/ver/)
Log.info('version:'+ extract_var.to_s)

输出来了-

version           530    
[2016-06-08T07:03:49+00:00] INFO:  version ["version                530\r\n"]

我只想提取530个字符串。

如你所愿 val = 720 而不是 val = "720" 你可以写

val = strvar.first.to_i
  #=> 720

自从Rot以来好久不见:)

您可以使用一些 Chef 辅助方法和正则表达式来简化此过程。

output = shell_out!('saphostexec.exe -version', cwd: 'C:\Program Files\hostctrl\exe').stdout
if output =~ /kernel release\s+(\d+)/
  kernel_version = 
else
  raise "unable to parse kernel version"
end
Chef::Log.info(kernel_version)

您可以 return 从 current_kernel 字符串中找到作为整数的第一个数字系列 String#[regexp] :

current_kernel[/\d+/].to_i
  #=> 720