ruby 1.9 如何将数组转换为没有括号的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19534973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:15:05  来源:igfitidea点击:

ruby 1.9 how to convert array to string without brackets

rubyarraysstringversionstring-interpolation

提问by Chris

My question is about how to convert array elements to string in ruby 1.9 without getting the brackets and quotation marks. I've got an array (DB extract), from which I want to use to create a periodic report.

我的问题是关于如何在 ruby​​ 1.9 中将数组元素转换为字符串而不需要括号和引号。我有一个数组(数据库提取),我想用它来创建定期报告。

myArray = ["Apple", "Pear", "Banana", "2", "15", "12"]

In ruby 1.8 I had the following line

在 ruby​​ 1.8 中,我有以下一行

reportStr = "In the first quarter we sold " + myArray[3].to_s + " " + myArray[0].to_s + "(s)."
puts reportStr

Which produced the (wanted) output

哪个产生了(想要的)输出

In the first quarter we sold 2 Apple(s).

在第一季度,我们售出了 2 个苹果。

The same two lines in ruby 1.9 produce (not wanted)

ruby 1.9 中相同的两行产生(不需要)

In the first quarter we sold ["2"] ["Apple"] (s).

在第一季度,我们售出了 ["2"] ["Apple"] (s)。

After reading in the documentation Ruby 1.9.3 doc#Array#sliceI thought I could produce code like

在阅读文档 Ruby 1.9.3 doc#Array#slice 后,我想我可以生成类似的代码

reportStr = "In the first quarter we sold " + myArray[3] + " " + myArray[0] + "(s)."
puts reportStr

which returns a runtime error

返回运行时错误

/home/test/example.rb:450:in `+': can't convert Array into String (TypeError)

/home/test/example.rb:450:in `+': 无法将数组转换为字符串(类型错误)

My current solution is to remove brackets and quotation marks with a temporary string, like

我目前的解决方案是使用临时字符串删除括号和引号,例如

tempStr0 = myArray[0].to_s
myLength = tempStr0.length
tempStr0 = tempStr0[2..myLength-3]
tempStr3 = myArray[3].to_s
myLength = tempStr3.length
tempStr3 = tempStr3[2..myLength-3]
reportStr = "In the first quarter we sold " + tempStr3 + " " + tempStr0 + "(s)."
puts reportStr

which in general works.

这通常有效。

However, what would be a more elegant "ruby" way how to do that?

但是,什么是更优雅的“红宝石”方式呢?

采纳答案by Agis

Use interpolation instead of concatenation:

使用插值代替串联:

reportStr = "In the first quarter we sold #{myArray[3]} #{myArray[0]}(s)."

It's more idiomatic, more efficient, requires less typing and automatically calls to_sfor you.

它更惯用、更高效、需要更少的输入并自动呼叫to_s您。

回答by Santosh Sindham

You can use the .joinmethod.

您可以使用该.join方法。

For example:

例如:

my_array = ["Apple", "Pear", "Banana"]

my_array.join(', ') # returns string separating array elements with arg to `join`

=> Apple, Pear, Banana

回答by hirolau

And if you need to do this for more than one fruit the best way is to transform the array and the use the each statement.

如果您需要为多个水果执行此操作,最好的方法是转换数组并使用 each 语句。

myArray = ["Apple", "Pear", "Banana", "2", "1", "12"]
num_of_products = 3

tranformed = myArray.each_slice(num_of_products).to_a.transpose
p tranformed #=> [["Apple", "2"], ["Pear", "1"], ["Banana", "12"]]

tranformed.each do |fruit, amount|
  puts "In the first quarter we sold #{amount} #{fruit}#{amount=='1' ? '':'s'}."
end 

#=>
#In the first quarter we sold 2 Apples.
#In the first quarter we sold 1 Pear.
#In the first quarter we sold 12 Bananas.

回答by kayleeFrye_onDeck

You can think of this as arrayToString()

你可以把这想成 arrayToString()

array = array * " "

array = array * " "

E.g.,

例如,

myArray = ["One.","_1_?! Really?!","Yes!"]

myArray = ["One.","_1_?! Really?!","Yes!"]

=>"One.","_1_?! Really?!","Yes!"

=>"One.","_1_?! Really?!","Yes!"

myArray = myArray * " "

myArray = myArray * " "

=>"One. _1_?! Really?! Yes."

=>"One. _1_?! Really?! Yes."