Skip to main content

Command Palette

Search for a command to run...

Common Array Methods in Ruby: A Comprehensive Overview

Published
2 min read
A

My skill set reads like a tech enthusiast's wishlist, encompassing mastery in JavaScript, React JS, and a keen proficiency in testing frameworks such as Jest and RTL. With a creative flair backed by hands-on experience, I've woven intricate web applications using Ruby on Rails and ensured their robustness with RSpec. My comfort with databases extends to PostgreSQL and MongoDB, and I navigate complex data structures with ease.My journey as a software craftsman is enriched by a Bachelor's degree in Computer Science from Kurukshetra University, a testament to my commitment to knowledge and growth. My work history boasts contributions that reflect my dedication to delivering cutting-edge solutions.I'm not just a Software Engineer; I'm an architect of digital experiences. Let's connect and explore how my expertise can bring unparalleled value to your projects. Open to exciting opportunities that align with my passion for innovation and drive for excellence.

An extensive list of array methods in Ruby along with code examples for each:

1. Adding and Removing Elements:

- << or push: Adds an element to the end of the array.

array = [1, 2, 3]

array << 4

array.push(5)

unshift: Adds an element to the beginning of the array.

array.unshift(0)

pop: Removes and returns the last element of the array.

last_element = array.pop

shift: Removes and returns the first element of the array.

first_element = array.shift

delete_at(index) : Deletes the element at the specified index.

array.delete_at(2)

2. Accessing Elements:

[]: Accesses an element at a specific index.

puts array[0] # Output: 1

first: Returns the first element of the array.

first_element = array.first

last: Returns the last element of the array.

last_element = array.last

3. Iterating Over Elements:

each: Executes a block for each element in the array.

array.each { |element| puts element }

- `map` or `collect`: Transforms each element of the array using a block.

new_array = array.map { |element| element * 2 }

select or filter: Returns an array containing elements for which the block returns true.

even_numbers = array.select { |element| element.even? }

reject:

Returns an array containing elements for which the block returns false.

odd_numbers = array.reject { |element| element.even? }

each_with_index: Iterates over each element with its index.

array.each_with_index { |element, index| puts "#{index}: #{element}"

4. Sorting and Reversing:

- sort: Returns a new array with elements sorted.

sorted_array = array.sort

reverse:

Returns a new array with elements in reverse order.

reversed_array = array.reverse

5. Checking and Searching:

include?: Checks if a certain element is included in the array

puts array.include?(3) # Output: true

index:

Returns the index of the first occurrence of a specified value.

index = array.index(2)

count: Returns the number of elements in the array.

count = array.count

- `empty?`: Checks if the array is empty.

puts array.empty? # Output: false

6. Transforming Arrays:

flatten:

Returns a new array that is a one-dimensional flattening of the original array.

nested_array = [[1, 2], [3, 4]]

flattened_array = nested_array.flatten

compact:

Returns a new array with nil values removed.

array_with_nil = [1, nil, 3, nil, 5]

compacted_array = array_with_nil.compact

7. Combining Arrays:

- `+`: Concatenates two arrays.

concatenated_array = array + [6, 7, 8]

- `concat`: Appends the elements of another array to the current array.

array.concat([6, 7, 8])

These examples demonstrate how to use various array methods in Ruby for different purposes.

More from this blog

Aqir lone

13 posts

Software developer with over two years of experience in technologies Ruby on Rails, Reactjs, PostgreSQL.