Sylar
Sylar

Reputation: 12072

Return numbers in array that are less than x

I have an array of numbers:

arr = [1,2,3,87,99,66,44,3,5]

I want to select the elements from it that are less than 3, and count them. How can I return an array with numbers from arr less than 3 ([1,2])? Similarly, I want the numbers less than 50: [1,2,3,3,5,44].

I thought I could use arr.min(3), or arr.any? {|a| a < 3}, which turned out to be not what I want.

Upvotes: 0

Views: 425

Answers (1)

sawa
sawa

Reputation: 168091

Your question is an XY-question.

What you asked for:

arr.select{|e| e < 3} # => [1, 2]

What you needed to do:

arr.count{|e| e < 3} # => 2

Upvotes: 2

Related Questions