Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ruby have the array method select! or not?

Tags:

ruby

> a = %w(a b c)
 => ["a", "b", "c"] 
> a.select!{|e| !e.nil?}
NoMethodError: undefined method `select!' for ["a", "b", "c"]:Array
    from (irb):2

ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.5.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02

The doc says there is a method called select! in array.

http://www.ruby-doc.org/core/classes/Array.html#M000252

like image 978
Nick Vanderbilt Avatar asked Mar 11 '11 17:03

Nick Vanderbilt


2 Answers

Ruby 1.8.7 does have Array#reject!, though:

>> a = [1, 2, nil]
=> [1, 2, nil]
>> a.reject! &:nil?
=> [1, 2]
like image 67
Josh Lee Avatar answered Oct 05 '22 14:10

Josh Lee


The docs you linked to are for Ruby 1.9.2. Ruby 1.8.7 Arrays don't have select!.

EDIT: As a future reference, you can view documentation for the different versions of Ruby that are out there.

like image 44
theIV Avatar answered Oct 05 '22 14:10

theIV