Question

I would like to know if it's possible to use the puts statement in the following way:

array = ["a", "new", "array"]
puts "array length is: " + array.length

If I use the + operator then I get an error. I'm trying to get this output:

array length is 3

I know how to do this in Java but what is the Ruby equivalent?

Thanks

Was it helpful?

Solution

Use either

puts "array length is:  #{array.length}"

or

puts "array length is: " + array.length.to_s

OTHER TIPS

puts "array length is: #{array.length}"

You can use this to put one variable inside a string in ruby:

#{array.length}

So it would become

puts "array length is: #{array.length}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top