Module: Enumerable

Defined in:
lib/kitchensink/patches/enumerable.rb,
lib/kitchensink/patches/enumerable.rb,
lib/kitchensink/patches/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#restObject

Returns all elements after the first.



4
5
6
# File 'lib/kitchensink/patches/enumerable.rb', line 4

def rest
  self[1..-1]
end

#rrestObject

Returns a new collection containing all elements except for the last – This has a very PHPesque name and needs a better one. (PHP has an idiom where array methods that work in one direction can be reversed by prepending r to them. See sort=>rsort, ksort=>krsort, and asort=>arsort. Problem is, what’s a good word meaning “everything but the last”? I briefly toyed with “lizard” since it’s the Enumeration “with the tail cut off” but that’s not very intuitive to somebody looking at the method for the first time. I need a word that suggests to the reader what it means, and that with a little context the reader could determine the meaning correctly almost all every time.



23
24
25
# File 'lib/kitchensink/patches/enumerable.rb', line 23

def rrest
  self[0..-2]
end

#shuffleObject

Returns an array with its elements shuffled. This is not a particularly efficient method because Enumerable doesn’t provide random access. This is overridden with a more efficient method in Array.



34
35
36
# File 'lib/kitchensink/patches/enumerable.rb', line 34

def shuffle
  self.sort_by { rand }
end