method

reject

v1_9_3_125 - Show latest stable - Class: Array
reject()
public

Returns a new array containing the items in self for which the block is not true. See also Array#delete_if

If no block is given, an enumerator is returned instead.

2Notes

Usage example

Mange · Feb 16, 20096 thanks

Some examples:

Remove even numbers

(1..30).reject { |n| n % 2 == 0 }

=> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]

Remove years dividable with 4 (this is not the full leap years rule)

(1950..2000).reject { |y| y % 4 != 0 }

=> [1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000]

Remove users with karma below arithmetic mean

total = users.inject(0) { |total, user| total += user.karma } mean = total / users.size good_users = users.reject { |u| u.karma < mean }

see also

ronald · Mar 26, 20091 thank

Enumerable#reject