-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.rb
More file actions
61 lines (51 loc) · 4.22 KB
/
Copy patharray.rb
File metadata and controls
61 lines (51 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Array
# An array in Ruby is an ordered, indexed collection of objects. Arrays are mutable,
# meaning you can change their content after creation. You can create an array by enclosing elements in square brackets (`[]`) or by using the `Array.new` constructor.
# first index is 0, and they can hold objects of any type, including other arrays. This flexibility allows you to create complex data structures. Arrays are dynamic in size,
# Creating an array
my_array = [1, 2, 3, "four", "five"]
puts my_array.inspect # Output: [1, 2, 3, "four", "five"] # inspect is used to get a string representation of the array
# Accessing elements
puts my_array[0] # Output: 1
puts my_array[3] # Output: "four"
# Modifying elements
my_array[1] = "two"
puts my_array.inspect # Output: [1, "two", 3, "four", "five"]
# Adding elements
# You can add elements to an array using the `push` method or the shovel operator (`<<`). Both of these methods will append the new element to the end of the array.
# You can also add elements at the first of array using the `unshift` method, which adds elements to the beginning of the array. Additionally, you can assign a value to an index that is beyond the current size of the array, and Ruby will automatically expand the array to accommodate the new element, filling any intermediate indices with `nil`.
my_array.push("six") # Using the push method to add an element
my_array << "six" # Using the shovel operator to add an element
my_array.unshift("zero") # Using the unshift method to add an element to the beginning
puts my_array.inspect # Output: ["zero", 1, "two", 3, "four", "five", "six", "six"]
# Removing elements
# You can remove elements from an array using the `pop` method, which removes the last element, or the `shift` method, which removes the first element. Additionally, you can use the `delete` method to remove specific elements by value.
my_array.pop # Removes the last element ("six")
my_array.shift # Removes the first element (1)
puts my_array.inspect # Output: ["two", 3, "four", "five"]
# Array methods
# Ruby provides a wide range of methods for working with arrays. Some common methods include:
# - `length` or `size`: Returns the number of elements in the array.
# - `include?`: Checks if a specific element is present in the array.
# - `sort`: Sorts the elements of the array (works for comparable elements).
# - `reverse`: Reverses the order of the elements in the array.
# - `uniq`: Removes duplicate elements from the array.
puts my_array.length # Output: 4
puts my_array.include?("four") # Output: true
puts my_array.reverse.inspect # Output: ["five", "four", 3, "two"]
puts my_array.uniq.inspect # Output: ["two", 3, "four", "five"] (No duplicates to remove in this case)
# Arrays can also be nested, meaning you can have arrays within arrays. This allows for the creation of multi-dimensional arrays, which can be useful for representing complex data structures like matrices or tables.
nested_array = [[1, 2], [3, 4], [5, 6]]
puts nested_array.inspect # Output: [[1, 2], [3, 4], [5, 6]]
puts nested_array[0][1] # Output: 2 (Accessing the second element of the first sub-array)
# dig method is used to access nested elements in an array or hash. It allows you to specify a path of keys or indices to retrieve a value from a nested structure. If any part of the path is not found, it returns nil instead of raising an error.
p nested_array.dig(1, 0) # Output: 3 (Accessing the first element of the second sub-array)
p nested_array.dig(2, 1) # Output: 6 (Accessing the second element of the third sub-array)
p nested_array.dig(3, 0) # Output: nil (Index 3 does not exist)
# delete_at method is used to remove an element from an array at a specific index. It takes the index as an argument and removes the element at that position, returning the removed element. If the index is out of bounds, it returns nil.
removed_element = my_array.delete_at(1) # Removes the element at index 1
puts removed_element # Output: two (The element that was removed)
puts my_array.inspect # Output: [1, 3, "four", "five", "six"] (The array after the element has been removed)
# delete all array elements
my_array.clear # This will remove all elements from the array, leaving it empty.
puts my_array.inspect # Output: [] (The array is now empty)