Undocumented feature: Array#one? (ruby 1.9.2)

In (my) ruby 1.9.2 class Array has an undocumented method #one? . It
takes no arguments and returns true if the array contains one element,
false otherwise. Is this something someone forgot to delete?

On Oct 30, 2010, at 4:45 PM, Siep K. wrote:

In (my) ruby 1.9.2 class Array has an undocumented method #one? . It
takes no arguments and returns true if the array contains one element,
false otherwise. Is this something someone forgot to delete?

$ ri -T Enumerable#one?
Enumerable#one?

(from ruby core)

enum.one? [{|obj| block }] => true or false


Passes each element of the collection to the given block. The method
returns
true if the block returns true exactly once. If the block is not
given, one? will return true only if exactly one of the
collection members is true.

%w{ant bear cat}.one? {|word| word.length == 4} #=> true
%w{ant bear cat}.one? {|word| word.length > 4} #=> false
%w{ant bear cat}.one? {|word| word.length < 4} #=> false
[ nil, true, 99 ].one? #=> false
[ nil, true, false ].one? #=> true

James Edward G. II

James Edward G. II wrote in post #958258:

On Oct 30, 2010, at 4:45 PM, Siep K. wrote:

In (my) ruby 1.9.2 class Array has an undocumented method #one? . It
takes no arguments and returns true if the array contains one element,
false otherwise. Is this something someone forgot to delete?

$ ri -T Enumerable#one?
Enumerable#one?

(from ruby core)

enum.one? [{|obj| block }] => true or false


Passes each element of the collection to the given block. The method
returns
true if the block returns true exactly once. If the block is not
given, one? will return true only if exactly one of the
collection members is true.

%w{ant bear cat}.one? {|word| word.length == 4} #=> true
%w{ant bear cat}.one? {|word| word.length > 4} #=> false
%w{ant bear cat}.one? {|word| word.length < 4} #=> false
[ nil, true, 99 ].one? #=> false
[ nil, true, false ].one? #=> true

James Edward G. II

Ah, it takes a block. That certainly makes more sense.

Thanks.

Siep

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs