Module: Enumerable
- Defined in:
- lib/extensions/enumerable.rb
Overview
TODO: Get these added to the pretty_ruby gem, and use that. (Note that it uses refinements, not monkey-patching.)
Instance Method Summary collapse
- #map_dig ⇒ Object
-
#nth(n) ⇒ Object
Prefer 1-based indexing to get the
nthelement. -
#only ⇒ Object
Like
firstorlast, but requires that there be exactly one element. -
#rest(count = 1) ⇒ Object
Allow Scheme-style
rest. -
#same? ⇒ Boolean
Returns
trueiff all the elements are equal. -
#second ⇒ Object
Allow Rails-style
second. -
#tail ⇒ Object
Allow Scheme-style
tail.
Instance Method Details
#map_dig ⇒ Object
38 39 40 |
# File 'lib/extensions/enumerable.rb', line 38 def map_dig(*) map{ |a| a.dig(*) } end |
#nth(n) ⇒ Object
Prefer 1-based indexing to get the nth element.
34 35 36 |
# File 'lib/extensions/enumerable.rb', line 34 def nth(n) # rubocop:disable Naming/MethodParameterName drop(n - 1).first end |
#only ⇒ Object
Like first or last, but requires that there be exactly one element.
23 24 25 26 |
# File 'lib/extensions/enumerable.rb', line 23 def only fail IndexError, "expected to have exactly 1 element" unless size == 1 first end |
#rest(count = 1) ⇒ Object
Allow Scheme-style rest.
6 7 8 |
# File 'lib/extensions/enumerable.rb', line 6 def rest(count = 1) drop(count) end |
#same? ⇒ Boolean
Returns true iff all the elements are equal. Returns false if there are no elements.
18 19 20 |
# File 'lib/extensions/enumerable.rb', line 18 def same? uniq.size == 1 end |
#second ⇒ Object
Allow Rails-style second.
29 30 31 |
# File 'lib/extensions/enumerable.rb', line 29 def second drop(1).first end |
#tail ⇒ Object
Allow Scheme-style tail.
FIXME: Shouldn't this be the same as rest?
12 13 14 15 |
# File 'lib/extensions/enumerable.rb', line 12 def tail return self if empty? last(size - 1) end |