Arrays in Ruby can be represented as a string. This is possible with the array.to_s()
function in Ruby, which creates a string representation of an array.
array.to_s
This function takes no parameters.
The value returned is a string representation of the array.
In the code below, several arrays are created and the string representation, using the to_s
function, is printed to the console.
# creating arrayslanguagesArray = ["Java", "C++", "Python", "Javascript", "Ruby on Rails!" ]numbersArray = [1, 2, 3, 4, 5]alphabetsArray = ["a", "b", "c", "d", "e"]booleanArray = [true, false]animalsArray = ["dog", "cat", "rat", "cow", "bat"]# print string representationsputs languagesArray.to_sputs numbersArray.to_sputs alphabetsArray.to_sputs booleanArray.to_sputs animalsArray.to_s