15

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.

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

In ruby 1.8 I had the following line

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)

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

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

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)

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?

Community
  • 1
  • 1
Chris
  • 165
  • 1
  • 1
  • 6
  • Wed Oct 23 18:16:46#8>ruby -v ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.5.0] Wed Oct 23 18:16:51#9>irb irb(main):001:0> myArray = ["Apple", "Pear", "Banana", "2", "15", "12"] => ["Apple", "Pear", "Banana", "2", "15", "12"] irb(main):002:0> reportStr = "In the first quarter we sold " + myArray[3].to_s + " " + myArray[0].to_s + "(s)." => "In the first quarter we sold 2 Apple(s)." irb(main):003:0> puts reportStr In the first quarter we sold 2 Apple(s). I think your issue isn't related to the version of Ruby – Leif Oct 23 '13 at 07:26
  • Sorry Leif, but your comment does not help me. – Chris Oct 23 '13 at 07:48
  • I didn't post an answer because I can't reproduce your issue under 1.9 - hence a comment. I didn't think you'd appreciate an answer of "works for me!". – Leif Oct 23 '13 at 09:39

4 Answers4

42

You can use the .join method.

For example:

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

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

=> Apple, Pear, Banana
user664833
  • 18,397
  • 19
  • 91
  • 140
Santosh Sindham
  • 879
  • 8
  • 18
3

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.

Agis
  • 32,639
  • 3
  • 73
  • 81
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.
hirolau
  • 13,451
  • 8
  • 35
  • 47
1

You can think of this as arrayToString()

array = array * " "

E.g.,

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

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

myArray = myArray * " "

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

kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80