C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Length: A string array has a length. We access this property on the array itself. This property is fast.
Each: We use the each iterator to loop over the strings in the array. Modern Ruby programs typically use iterators like each.
Push: In the next part we use the push method to add elements in an imperative style.
Ruby program that creates string arrays
# 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}
Output
3
one
two
THREE
3
one
two
THREE
Contents, gems.txt: Ruby
ruby
sapphire
diamond
emerald
topaz
Ruby program that reads file into string array
# 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
Output
[ruby]
[sapphire]
[diamond]
[emerald]
[topaz]
Note: This program, with a print() call instead of puts, can be run in Python or in Ruby.
Ruby program that combines two string arrays
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
Output
virus
disease
bacteria
germ
Note: This is actually a "jagged array," an array of nested arrays. Length counts the nested arrays, not all elements, so it returns 2.
Ruby program that uses 2D string array
# 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]
Output
AA BB CC
DD EE FF
2
BB
Split: This separates a string value based on a character. So split and join can "round-trip" data in their calls.
SplitTip: 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.
Ruby program that uses join, split
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
Output
two,seven,nine,forty
["two", "seven", "nine", "forty"]
4