Step 20: Arrays & Strings


Arrays and strings have a lot of similarities. Arrays are ordered collections of any object, and strings are ordered collections of characters. Many of the methods available to arrays are also available to strings. You can find a full list of string methods at, you guessed it, http://ruby-doc.org/core-2.3.1/String.html.

sentence = "good luck on your interview"
puts(sentence.length)
puts(sentence[0])


sentence[0] = "G"
puts(sentence)
sentence[sentence.length] = "!"
puts(sentence)

It’s also common for us to need to convert strings into arrays, and vice versa. Ruby provides easy-to-use methods for us to do this. Check out the documentation for Array#join and String#split.

question = "Are you getting the hang of this?"
arr = question.split
p(arr)
p(arr.join("-"))