Name

Enumerable — Enumerable mix-in module

Synopsis

The Enumerable module assumes that the including class has an each method. You can add the following methods to a class that provides each, by just including this module.

Instance Methods

e.collect {|x|...}
e.map {|x|...}

Returns an array containing the results of running the block on each item in e.

e.detect {|x|...}

See e.find {|x|...}

e.each_with_index {|x, i|...}

Executes the block once for each item in e, passing both the item and its index to the block.

["foo","bar","baz"].each_with_index {|x,i|
  printf "%d: %s\n", i, x
}
# prints:
#  0: foo
#  1: bar
#  2: baz.
e.entries
e.to_a

Returns an array containing the items passed to it by e.each.

e.find {|x|...}
e.detect {|x|...}

Returns the first item for which the block returns true.

["foo","bar","baz"].detect {|s| /^b/ =~ s} # => "bar"
e.find_all {|x|...}
e.select {|x|...}

Returns an array of all items for which the block returns true.

["foo","bar","baz"].select {|s| /^b/ =~ s} # => ["bar","baz"]
e.grep(re)
e.grep(re) {|x|...}

Returns an array containing all items matching re. Uses ===. If a block is specified, it’s run on each matching item, with the results returned as an array.

["foo","bar","baz"].grep(/^b/)  # => ["bar","baz"]
[1,"bar",4.5].grep(Numeric)     # => [1,4.5]
[1,"bar",4.5].grep(Numeric) {|x|
   puts x+1
}
# prints:
#  2
#  5.5
e.include?(item)
e.member?(item)

Returns true if an item equal to item is present in e. Items are compared using ==.

e.map {|x|...}

See e.collect {|x|...}

e.max

Returns the ...

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.