Ruby - String in comma

Introduction

Ruby uses commas to separate strings and other data types.

In some circumstances, these commas appear to have the effect of concatenating strings.

Demo

s = "This " , "is" , " not a string!", 10 
print("print (s):" , s, "\n")

Result

In Ruby, a list separated by commas creates an array.

Demo

x = "This " , "is" , " not a string!", 36 
print("print (x):" , x, "\n") 
puts("puts(x):", x) 
puts("puts x.class is: " << (x.class).to_s ) 

print("print(x):" , x, "\n") 
puts("puts(x):", x) 
puts("puts x.class is: " << (x.class).to_s )

Result

Here, the first print statement looks as though it is displaying a single string.

This is because each successive item in the array, x, is printed on the same line as the preceding item.

puts method prints each item in turn and appends a carriage return after it.

Related Topic