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

时间:2022-06-01 18:57:53

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中将数组元素转换成字符串而不需要括号和引号。我有一个数组(DB解压),我想用它来创建一个定期报告。

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).

第一季度我们卖出了两个苹果。

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

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

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

第一季度我们卖出了[2][苹果][s]。

After reading in the documentation Ruby 1.9.3 doc#Array#slice I 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.在' +':不能将数组转换成字符串(类型错误)

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?

然而,如何用一种更优雅的“ruby”方式来实现这一点呢?

4 个解决方案

#1


2  

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_s for you.

它更习惯,更高效,需要更少的输入,并自动为你调用to_s。

#2


22  

You can use the .join method.

您可以使用.join方法。

For example:

例如:

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

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

=> Apple, Pear, Banana

#3


1  

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.

如果你需要对不止一个结果执行此操作,最好的方法是转换数组并使用每个语句。

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.

#4


0  

You can think of this as arrayToString()

可以将其看作arrayToString()

array = array * " "

数组=数组* " "

E.g.,

例如,

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

myArray =(“。”、“_1_ ? !真的吗? !”,“是的!”)

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

= > "。”、“_1_ ? !真的吗? !”,“是的!”

myArray = myArray * " "

myArray = myArray "

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

= > "。_1_ ? !真的吗? !是的。”

#1


2  

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_s for you.

它更习惯,更高效,需要更少的输入,并自动为你调用to_s。

#2


22  

You can use the .join method.

您可以使用.join方法。

For example:

例如:

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

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

=> Apple, Pear, Banana

#3


1  

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.

如果你需要对不止一个结果执行此操作,最好的方法是转换数组并使用每个语句。

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.

#4


0  

You can think of this as arrayToString()

可以将其看作arrayToString()

array = array * " "

数组=数组* " "

E.g.,

例如,

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

myArray =(“。”、“_1_ ? !真的吗? !”,“是的!”)

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

= > "。”、“_1_ ? !真的吗? !”,“是的!”

myArray = myArray * " "

myArray = myArray "

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

= > "。_1_ ? !真的吗? !是的。”