Array#firstи Array#last: эти методы возвращают первый и последний элементы массива соответственно.
arr = [1, 2, 3, 4, 5]
first_element = arr.first
last_element = arr.last
puts first_element # Output: 1
puts last_element # Output: 5
String#start_with?и String#end_with?: эти методы проверяют, начинается или заканчивается строка определенной подстрокой, и возвращают логическое значение.
str = "Hello, World!"
starts_with_hello = str.start_with?("Hello")
ends_with_world = str.end_with?("World!")
puts starts_with_hello # Output: true
puts ends_with_world # Output: false
Range#beginи Range#end: эти методы возвращают первое и последнее значения диапазона.
range = (1..5)
range_begin = range.begin
range_end = range.end
puts range_begin # Output: 1
puts range_end # Output: 5
Hash#keysи Hash#values: эти методы возвращают массив, содержащий все ключи или значения хеша соответственно.
hash = { name: "John", age: 30, city: "New York" }
keys = hash.keys
values = hash.values
puts keys # Output: [:name, :age, :city]
puts values # Output: ["John", 30, "New York"]
Enumerable#eachи Enumerable#each_with_index: эти методы позволяют перебирать элементы в коллекции.
arr = [1, 2, 3, 4, 5]
arr.each do |element|
puts element
end
# Output:
# 1
# 2
# 3
# 4
# 5
arr.each_with_index do |element, index|
puts "Index: #{index}, Element: #{element}"
end
# Output:
# Index: 0, Element: 1
# Index: 1, Element: 2
# Index: 2, Element: 3
# Index: 3, Element: 4
# Index: 4, Element: 5