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?

···

--
Posted via http://www.ruby-forum.com/.

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

(from ruby core)

···

On Oct 30, 2010, at 4:45 PM, Siep Korteling 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?

------------------------------------------------------------------------------
  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 Gray II

James Edward Gray II wrote in post #958258:

···

On Oct 30, 2010, at 4:45 PM, Siep Korteling 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 Gray II

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

Thanks.

Siep

--
Posted via http://www.ruby-forum.com/\.