Ruby Array reject() Method

Learn via video courses
Topics Covered

Have you ever come across a situation where you need to filter out certain elements from an array in Ruby? In this article, we'll explore the reject method, a powerful tool that allows you to exclude elements based on specific conditions. Whether you're a beginner or an experienced Ruby developer, understanding reject will undoubtedly enhance your coding skills. So let's dive right in!

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

The syntax for using the reject method is quite straightforward. You can call it on an array object, followed by a block of code that defines the condition for exclusion.

Here's the syntax:

The reject method iterates over each element in the array and returns a new array that excludes elements for which the condition evaluates to true. The original array remains unchanged.

Parameters

The Ruby Reject method accepts a block of code as its parameter. Within this block, you define the condition for exclusion using a placeholder variable, typically named element. You can access each element of the array and apply any necessary comparisons or operations to determine if it should be excluded.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Return Value

The Ruby Reject method returns a new array that includes all of the original array's elements minus the ones which met the exclusion condition. It's important to note that the original array remains unaltered.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Exception

The Ruby Reject method does not raise any exceptions on its own. However, it's crucial to ensure that the block of code you provide doesn't produce any errors. Otherwise, those errors could propagate outside the reject method.

Examples:

Let's explore some examples to better understand how the reject method works.

Consider the following array:

Example 1: Excluding even numbers

In this example, the reject method filters out all the even numbers from the numbers array, leaving us with [1, 3, 5].

Example 2: Filtering by a condition

In this case, we exclude all the numbers greater than 3 from the numbers array, resulting in [1, 2, 3].

Example 3: Excluding strings

Here, we use reject to remove all the strings from the mixed_array, producing [1, 3, 5].

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

In summary, the Ruby Reject method in Ruby provides a convenient way to filter out elements from an array based on specific conditions. By leveraging this method, you can easily manipulate arrays and obtain new arrays that exclude unwanted elements.

Remember these key points:

  • The Ruby Reject method excludes elements based on a provided condition within a block.
  • It returns a new array without altering the original array.
  • Ensure your block of code does not raise any errors to avoid unexpected behavior.
  • reject is a powerful tool for filtering arrays efficiently and maintaining clean code.