In Ruby, you can get the count of the number of even numbers in an array of integers using the Array#count
method with a condition, for example, like so:
numbers = [5, 10, 26, 44, 75] puts numbers.count { | item | item.even? } #=> 3 puts numbers.count { | item | item % 2 == 0 } #=> 3
Similarly, to get the count of number of odd numbers in an array of integers, you could do the following:
numbers = [5, 10, 26, 44, 75] puts numbers.count { | item | item.odd? } #=> 2 puts numbers.count { | item | item % 2 == 1 } #=> 2
Hope you found this post useful. It was published . Please show your love and support by sharing this post.