String
arrayWith a Ruby string
array, we can store many strings together. We often prefer iterators, not loops, to access an array's individual elements.
In Ruby programs, we use string
arrays in many places. We can combine and convert string
arrays. Ruby supports operators that act upon entire arrays.
string
arraysHere we use 2 syntax forms to create string
arrays. First, we use the initializer syntax—this requires only one line to create a 3-element array.
string
array has a length. We access this property on the array itself. This property is fast.# Initialize a three-element string Array. strings = ["one", "two", "THREE"] puts strings.length # Iterate over the strings with "each." strings.each do |st| puts st end # Create an array and push three strings to it. strings2 = Array[] strings2.push("one") strings2.push("two") strings2.push("THREE") # Write the length and display all the strings again. # ... Then use a shorter iterator syntax. puts strings2.length strings2.each {|st| puts st}3 one two THREE 3 one two THREE
With IO.foreach
we efficiently read in the lines of a file, one after another. We use chomp()
to remove the trailing newline.
ruby sapphire diamond emerald topaz# Create an empty array. lines = [] # Use foreach iterator to loop over lines in the file. # ... Add chomped lines. IO.foreach("/files/gems.txt") do |line| lines.push(line.chomp()) end # Display elements in string array. lines.each do |v| print "[" << v << "]" << "\n" end[ruby] [sapphire] [diamond] [emerald] [topaz]
string
arraysTwo arrays can be concatenated with the plus operator. This is an effective way to combine two string
arrays.
print()
call instead of puts, can be run in Python or in Ruby.left = ["virus", "disease"] right = ["bacteria", "germ"] # Concatenate the two string arrays together. # ... The elements are placed together in a four-element array. result = left + right puts resultvirus disease bacteria germ
string
arrayThis example creates a 2D string
array and then iterates over it with the each iterator. It displays elements. It accesses a specific element in the top row.
Length
counts the nested arrays, not all elements, so it returns 2.# The first row. values1 = [] values1.push("AA") values1.push("BB") values1.push("CC") # The second row. values2 = [] values2.push("DD") values2.push("EE") values2.push("FF") # The 2D array (a container of the other arrays). container = [] container.push(values1) container.push(values2) # Use two iterators to display all strings in 2D array. container.each do |arr| arr.each do |inner| print inner << " " end print "\n" end # The 2D array has only two rows. puts container.length # Print string cell in middle of top row. puts container[0][1]AA BB CC DD EE FF 2 BB
Split
and join stringsThese methods involve string
arrays. With join, we convert from a string
array into a single string
with a delimiter in between elements.
string
value based on a character. So split and join can "round-trip" data in their calls.join()
is an array method. It is called on an array. This is not true for Python, where join()
acts on a string
.items = ["two", "seven", "nine", "forty"] # Join the string array's elements together. # ... Display them. string_value = items.join(",") puts string_value # Split apart our newly-joined string. array_values = string_value.split(",") p array_values # We have four string elements. puts array_values.lengthtwo,seven,nine,forty ["two", "seven", "nine", "forty"] 4
String
arrays are converted into (and from) strings. File handling often involves string
arrays. Ruby provides excellent syntax for these common tasks.