LU 36-39 Ruby Basic-Cs-Arrays
LU 36-39 Ruby Basic-Cs-Arrays
In Ruby, an array is an ordered collection of elements that can hold objects of any
data type, including other arrays.
Array Syntax
Arrays in Ruby are created by placing elements within square brackets ([]), and
elements are separated by commas.
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]
mixed = [1, "Hello", 3.14, true]
Modifying Elements You can modify an element at a specific index by assigning a new
value.
numbers = [10, 20, 30, 40]
numbers[1] = 25 # Change the second element
puts numbers # Output: [10, 25, 30, 40]
Adding Elements
Push: Add an element to the end of the array using push or <<.
numbers = [1, 2, 3]
numbers.push(4) # Adds 4 to the end
numbers << 5 # Adds 5 to the end
puts numbers # Output: [1, 2, 3, 4, 5]
Removing Elements
Pop: Remove the last element of the array.
numbers = [1, 2, 3, 4]
numbers.pop # Removes 4
puts numbers # Output: [1, 2, 3]
Array Length Use the length or size method to determine the number of elements in
an array.
numbers = [10, 20, 30, 40]
puts numbers.length # Output: 4
Checking for Elements You can check whether a certain element exists in the array
using the include? method.
numbers = [10, 20, 30, 40]
puts numbers.include?(20) # Output: true
Multi-dimensional Arrays
Ruby arrays can store other arrays, allowing the creation of multi-dimensional
arrays.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
puts matrix[1][2] # Output: 6
Examples:
1. Sort an array
numbers = [5, 3, 8, 1, 2]
sorted_numbers = numbers.sort
2. Reverse an array
letters = ['a', 'b', 'c', 'd']
reversed_letters = letters.reverse
6. include?
colors = ["red", "green", "blue"]
if colors.include?("green")
puts "Green is in the array"
else
puts "Green is not in the array"
end
String Methods:
1. length / size
Returns the number of characters in a string.
str = "Hello, Ruby!"
puts str.length # Output: 12
puts str.size # Output: 12
2. upcase / downcase
upcase: Converts all characters to uppercase.
downcase: Converts all characters to lowercase.
str = "Hello, Ruby!"
puts str.upcase # Output: "HELLO, RUBY!"
puts str.downcase # Output: "hello, ruby!"
3. capitalize
Capitalizes the first character of the string and converts the rest to lowercase.
str = "ruby programming"
puts str.capitalize # Output: "Ruby programming"
4. reverse
Reverses the characters in the string.
str = "Ruby"
puts str.reverse # Output: "ybuR"
5. include?
Checks if a given substring is present in the string. Returns true or false
str = "Hello, Ruby!"
puts str.include?("Ruby") # Output: true
puts str.include?("Java") # Output: false
6. gsub
Substitutes all occurrences of a pattern with a given replacement.
str = "I love Ruby, Ruby is awesome!"
new_str = str.gsub("Ruby", "Python")
puts new_str # Output: "I love Python, Python is awesome!"
7. split
Splits the string into an array based on a delimiter (space by default).
str = "apple,banana,cherry"
fruits = str.split(",")
puts fruits # Output: ["apple", "banana", "cherry"]
9. chomp
Removes the newline character (\n) from the end of the string.
str = "Hello, Ruby!\n"
puts str.chomp # Output: "Hello, Ruby!"
10. concat
Appends one string to another.
str1 = "Hello, "
str2 = "Ruby!"
puts str1.concat(str2) # Output: "Hello, Ruby!"
12. index
Returns the index of the first occurrence of a substring, or nil if not found.
str = "Hello, Ruby!"
puts str.index("Ruby") # Output: 7
puts str.index("Java") # Output: nil
13. replace
Replaces the contents of the string with another string.
str = "Hello, Ruby!"
str.replace("Hi, Python!")
puts str # Output: "Hi, Python!"
14. slice
Extracts a portion of the string based on the index or range.
str = "Hello, Ruby!"
puts str.slice(0, 5) # Output: "Hello"
puts str.slice(7..10) # Output: "Ruby"
16. empty?
Returns true if the string is empty, otherwise false.
str1 = ""
str2 = "Ruby"
puts str1.empty? # Output: true
puts str2.empty? # Output: false
17. sub
Replaces the first occurrence of a pattern with a given replacement.
str = "I love Ruby, Ruby is awesome!"
new_str = str.sub("Ruby", "Python")
puts new_str # Output: "I love Python, Ruby is awesome!"
18. count
Counts the number of occurrences of a specific character or set of characters.
str = "abracadabra"
puts str.count("a") # Output: 5
puts str.count("bc") # Output: 4
19. delete
Deletes all occurrences of a specific character or set of characters from the
string.
str = "abracadabra"
puts str.delete("a") # Output: "brcdbr"
20. insert
Inserts a string at the specified index.
str = "Hello Ruby!"
str.insert(5, ",")
puts str # Output: "Hello, Ruby!"