Home
Ruby
String Array Examples
This page was last reviewed on Aug 29, 2023.
Dot Net Perls
String array. With a Ruby string array, we can store many strings together. We often prefer iterators, not loops, to access an array's individual elements.
Array
Shows a string array
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.
Convert
Create string arrays. Here 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.
Info A string array has a length. We access this property on the array itself. This property is fast.
Note We use the each iterator to loop over the strings in the array. Modern Ruby programs typically use iterators like each.
Here In the next part we use the push method to add elements in an imperative style.
Shows a string array
# 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
Read file lines, chomp. With IO.foreach we efficiently read in the lines of a file, one after another. We use chomp() to remove the trailing newline.
File
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]
Combine string arrays. Two arrays can be concatenated with the plus operator. This is an effective way to combine two string arrays.
Note This program, with a 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 result
virus disease bacteria germ
2D string array. This 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.
Note This is actually a "jagged array," an array of nested arrays. 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 strings. These methods involve string arrays. With join, we convert from a string array into a single string with a delimiter in between elements.
Detail This separates a string value based on a character. So split and join can "round-trip" data in their calls.
String split
Tip In Ruby 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.length
two,seven,nine,forty ["two", "seven", "nine", "forty"] 4
A summary. String arrays are converted into (and from) strings. File handling often involves string arrays. Ruby provides excellent syntax for these common tasks.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 29, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.