문제

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

도움이 되었습니까?

해결책

Use either

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

or

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

다른 팁

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}"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top