# Using .delete\_if and reject on elements in an array

**URL:** <https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839>\
**Category:** Ruby\
**Created:** [October 25, 2009, 3:57am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839 "2009-10-25T03:57:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 3:57am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/1 "2009-10-25T03:57:33Z")

</div>

Working on code to fill an array with 200 random integer elements. The  
elements range from 1-100 in value. Next I use the .sort method to see  
how many values fall between 1-10 to visually check how many random  
numbers will fall between 1-10. Now I wanna take my array and apply a  
method to it that will check which elements are less 21 but more then  
10. For you math types 10\<x\<21. How do I accomplish that with ruby. Do I  
accomplish it by doing it this way or am I overlooking a better method  
for evaluating and comparing the elements in my array? I think I got the  
right method but the block is wrong?

a = nums1.reject {|x| x\<10 && x\>21}

Heres what I have so far of my program:

* * *

nums = []  
200.times do  
nums \<\< rand(100)  
end

nums1 = nums.sort  
nums1.each {|n| print n, “,” }

* * *

Please include the code so I can try it out on my editor and get it  
figured out!  
Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 4:40am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/2 "2009-10-25T04:40:43Z")

</div>

> I think I got the  
> right method but the block is wrong?
> 
> a = nums1.reject {|x| x\<10 && x\>21}

A number can’t be both less than 10 and greater than 21, so your block  
won’t match anything in the array.

Replace && with || (or better still, to my eyes, “or”).

Also, “reject” seems so _negative_. Why not be positive? 😉

nums1.select { |x| (10…21).include? x }

Some other suggestions for your code:

> nums = []  
> 200.times do  
> nums \<\< rand(100)  
> end

nums = (1…200).map { rand(100) }

> nums1 = nums.sort  
> nums1.each {|n| print n, “,” }

You don’t really need a new variable, do you?

nums.sort.each do |n| puts n end

or just

puts nums.sort

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 7:56am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/3 "2009-10-25T07:56:12Z")

</div>

Matt B. wrote:

> Now I wanna take my array and apply a  
> method to it that will check which elements are less 21 but more then  
> 10. For you math types 10\<x\<21.
> 
> a = nums1.reject {|x| x\<10 && x\>21}

What you want is:

a = nums.find\_all { |n| n \> 10 && n \< 21 }

OR using Range

a = nums.find\_all { |n| (11…20) === n }

Generating random number

> nums \<\< rand(100)

rand(100) will generate a random number from 0 to 99,  
what you want to do is

nums \<\< rand(100)+1 # random number from 1 to 100

> Thanks,  
> Matt

–  
Kind Regards,  
Rajinder Y.

[http://DevMentor.org](http://DevMentor.org)

Do Good! - Share Freely, Enrich and Empower people to Transform their  
lives.

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 8:37am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/4 "2009-10-25T08:37:10Z")

</div>

Thanks for the pointers, this sure is a good community/forum!

I appreciate both inputs, while I learn Ruby. The && and or make better  
sense now. I’ll be looking at the .map method. Also didn’t know you  
could apply multiple methods in one line, very neat. Thanks for the  
help!

Matt

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 10:00am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/5 "2009-10-25T10:00:53Z")

</div>

On 10/25/2009 03:57 AM, Matt B. wrote:

> a = nums1.reject {|x| x\<10 && x\>21}
> 
> * * *
> 
> Please include the code so I can try it out on my editor and get it  
> figured out!

If I understand your requirement properly you want to sort numeric  
values into buckets. Here is one way that you can do it, assuming your  
buckets are 10 numbers wide each:

nums …  
buckets = Hash.new 0

nums.each do |x|  
buckets[x - x % 10] += 1  
end

buckets.sort.each do |b,count|  
printf “%4d %5d\n”, b, count  
end

If you have different alignments of buckets the code in the block of  
course becomes more complicated.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 25, 2009, 11:51am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/6 "2009-10-25T11:51:30Z")

</div>

Hi –

On Sun, 25 Oct 2009, Rajinder Y. wrote:

> OR using Range
> 
> a = nums.find\_all { |n| (11…20) === n }

You can also do:

a = nums.grep(11…20)

which is a wrapper for a bunch of calls to ===.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [November 3, 2009, 8:51am UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/7 "2009-11-03T08:51:19Z")

</div>

One liner…  
b=(a=Array.new(200).collect{rand(100)+1}).grep(11…21)

From IRB:

irb(main):046:0\> b=(a=Array.new(200).collect{rand(100)+1}).grep(11…21)  
=\> [14, 21, 15, 12, 18, 15, 12, 17, 20, 21, 19, 19, 14, 21, 17, 15, 18,  
20, 17, 11, 17, 11, 19, 12]  
irb(main):047:0\> a  
=\> [90, 76, 14, 84, 66, 37, 77, 92, 46, 24, 79, 46, 87, 90, 21, 90, 59,  
48, 62, 67, 55, 33, 58, 47, 23, 4, 95, 76, 66, 95, 66, 10, 49, 36, 75,  
74, 53, 99, 30, 15, 58, 26, 77, 95, 6, 89, 92, 43, 55, 54, 38, 12, 41,  
18, 6, 15, 12, 9, 58, 36, 17, 40, 8, 42, 68, 26, 71, 2, 3, 92, 20, 46,  
84, 95, 21, 90, 74, 31, 4, 59, 49, 36, 45, 52, 100, 98, 32, 25, 70, 3,  
69, 19, 45, 7, 46, 19, 34, 27, 82, 85, 59, 82, 94, 7, 4, 14, 21, 41, 29,  
88, 26, 42, 83, 33, 27, 42, 61, 72, 17, 66, 66, 73, 68, 96, 15, 5, 6,  
49, 18, 23, 72, 90, 84, 96, 46, 83, 44, 10, 75, 20, 43, 17, 63, 39, 25,  
3, 88, 81, 11, 7, 62, 24, 62, 22, 17, 72, 77, 82, 64, 11, 2, 50, 50, 19,  
40, 10, 93, 52, 4, 66, 23, 70, 3, 62, 44, 83, 38, 76, 53, 97, 48, 12,  
68, 64, 70, 77, 77, 49, 72, 34, 94, 94, 99, 79, 31, 73, 85, 59, 46, 64]  
irb(main):048:0\> b  
=\> [14, 21, 15, 12, 18, 15, 12, 17, 20, 21, 19, 19, 14, 21, 17, 15, 18,  
20, 17, 11, 17, 11, 19, 12]

---

<div class="post-metadata">

**Author:** ![burrowsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@burrowsm](https://www.ruby-forum.com/u/burrowsm)\
**Post date:** [October 31, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/using-delete-if-and-reject-on-elements-in-an-array/177839/8 "2009-10-31T21:05:47Z")

</div>

- Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (2009-10-25) schrieb:

> buckets = Hash.new 0
> 
> nums.each do |x|  
> buckets[x - x % 10] += 1  
> end

Why not just

buckets = num.inject([]) do | a, x |  
i = x.div 10  
a[i] = a.fetch(i, 0) + 1  
a  
end

mfg, simon … l
