Array with Ruby — Part 1
Let’s talk about Arrays. Array is a data structure consisting of a collection of elements. We used it in computer science. I’m not going to talk about how we create arrays or how we get data out of arrays. **whisper: I’ll tell you about that too**
Here we are mainly going to focus on what are the cool methods available in Ruby for Array.
Creating an Array:
I’m not going to dive deep into this part, but I’m going to introduce basic methods, Ruby Array can contain different types of objects, like String, Integer, Float, even Hash, anything you want.
You can create an array using [] constructor.
my_arr = ["Hello", 3, 7.9]
You can create an empty array or array with given numbers of elements.
my_arr = Array.new() #=> []my_arr = Array.new(3) #=> [nil, nil, nil]
If you want element to be whatever the value you want, you can create it like this.
my_arr = Array.new(3, true) #=> [true, true, true]
my_arr = Array.new(3, 10) #=> [10, 10, 10]
Accessing Elements:
Let’s get data out from our Array. In Ruby you can just call the element index and get the value.
my_arr = [1, 2, 3, 4, 5, 6, 7, 8 ,9]
If you want to access first element:
my_arr[0] #=> 1
If you want to access last element:
There are two ways to get it.
- If you know the length of array you can directly call the index
my_arr[8] #=> 9
- If you don’t know the length
my_arr[-1] #=> 9
Note: Array index start from left as 0 and from right start from -1
If you want to access second one from last,
my_arr[-2] #=> 8
What happens if you call an index that bound array length like my_arr[100]. It’ll return ‘nil’
my_arr[100] #=> nil
There are so many other ways to get elements from arrays, but we are not here for that.
Public Instance Methods — Ruby
I know you are going to say, “ahh we can google these methods if/when we need”. Yes you can, But i’m telling you below “&” method cost me a job. I had a live coding interview and I forgot that this method exists, and I created like 8 lines code to complete the task. And you know the result of that. So it’s good to remember these methods.
- Let’s talk about first Method “&”:
“&” method will return a new array containing common elements from both arrays.
Eg: array1 & array2
a = [1,2,3,4,5,6,7]b = [1,1,2,8,9,0]a&b #=> [1,2]
2. Method to build a new array with a number of copies itself.
Eg: array * n
Note: n must be a positive value
my_arr = [“a”, “b”, “c”]my_arr * 2 #=> [“a”, “b”, “c”, “a”, “b”, “c”]
3. Method to combine two arrays to create new array
Eg: array1 + array2
[“a”, “b”, “c”] + [“d”, “e”, “f”, “a”] #=> [“a”, “b”, “c”, “d”, “e”, “f”, “a”]
4. Method to remove common element from array
Eg: array1 - array2
[“a”, “b”, “c”, “d”] — [“d”, “a”] #=> [“b”, “c”]
5. Method to compare two arrays to see those are less than, equal or greater than
Eg: array1 <=> array2
This will return -1 for less than, 0 for equal and 1 greater than.
[“a”, “b”, “c”, “d”] <=> [“d”, “a”] #=> -1[“a”, “b”, “c”] <=> [“a”, “b”, “c”] #=> 0[“a”, “b”, “d”] <=> [“a”, “b”] #=> 1
6. Method to compare two arrays to see those equal
Eg: array1 == array2
This will return boolean value as a result. You can use this as a conditional statement.
[“a”, “b”, “c”] == [“a”, “b”, “c”] #=> true[“a”, “b”, “c”] == [“a”, “b”, “c”, “d”] #=> false
7. Method to conditionally check all elements in array
Let’s check contain nil elements
Eg: array.all?
[“a”, “b”, “c”].all? #=> true[“a”, “b”, “c”, nil].all? #=> false
Let’s check array elements pass the given condition
Eg: array.all? { |element| … }
[10, 20, 30 ,55].all? {|ele| ele < 60} #=> true[10, 20, 30 ,55].all? {|ele| ele > 60} #=> false
Is there a way to find out every element in an array is the same given element ? Yes it is. We can use the same .all? Method for that.
Eg: array.all?(“a”)
[“a”, “a”, “a”].all?(“a”) #=> true[“a”, “b”, “c”].all?(“a”) #=> false
8. We checked .all? Now we are going to check .any?
Eg: array.any?
[].any? #=> false[“a”, “b”, “c”].any? {|ele| ele == “a” } #=> true[10, 12, 15].any? {|ele| ele < 13 } #=> true[10, 12, 15].any? {|ele| ele > 20 } #=> false
9. bsearch
Did you know that you can perform binary search on an array in Ruby ?
10. A method to count how many times a given element is repeated.
Eg: array.count
There are three ways to use the count method.
a) To count how many elements in a array
b) To count how many times repeats given value
c) To get a count with conditional statement
[“a”, “b”, “c”, “d”].count #=> 4[“a”, “b”, “c”, “d”, “a”].count(“a”) #=> 2[1, 2, 3, 4, 5, 6, 7].count {|element| element > 3} #=> 4
11. Lets delete element(s) from an Array.
Delete element from given index.
Eg: array.delete_at(index)
arr = [“a”, “b”, “c”, “d”]arr.delete_at(2) #=> “c”arr #=> [“a”, “b”, “d”]arr.delete_at(-2) #=> “b”arr #=> [“a”, “d”]
Delete element with condition
Eg: array.delete_if {|element| … }
[“Apple”, “Banana”, “Bilberry”, “Carrot”, “Mango”, “Pineapple”].delete_if {|element| element.to_s.start_with?(‘B’)} #=> [“Apple”, “Carrot”, “Mango”, “Pineapple”]
Let’s drop some elements from an Array
Eg: array.drop(n)
This will return a new array with “n” elements removed from index 0.
arr = [“a”, “b”, “c”, “d”]arr.drop(0) #=> [“a”, “b”, “c”, “d”] removed 0 elementsarr.drop(1) #=> [“b”, “c”, “d”] remove 1 elementarr.drop(2) #=> [“c”, “d”] remove 2 elements
We can drop element with a conditional statement
Eg: array.drop_while {|element| … }
arr = [0, 1, 2, 3, 4, 5]arr.drop_while {|element| element < 3 } #=> [3, 4, 5]
We can use so many methods to work with Arrays in Ruby. I’ll see you in next article.