سؤال

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