Module: Enumerable

Defined in:
lib/robot_enumerable.rb

Instance Method Summary collapse

Instance Method Details

#bump_to_back(&block) ⇒ Object

Moves any elements for which the block returns true to the back of the list, otherwise maintaining the original order.

[1,2,3,4,5].bump_to_back { |x| x == 3 }
# => [1,2,4,5,3]


23
24
25
26
# File 'lib/robot_enumerable.rb', line 23

def bump_to_back(&block)
  matches, failures = partition(&block)
  failures + matches
end

#bump_to_front(&block) ⇒ Object

Moves any elements for which the block returns true to the front of the list, otherwise maintaining the original order. One place where this method is useful is the situation where you have a bunch of options for a select box and you want the default value to be first.

[1,2,3,4,5].bump_to_front { |x| x == 3 }
# => [3,1,2,4,5]


12
13
14
15
# File 'lib/robot_enumerable.rb', line 12

def bump_to_front(&block)
  matches, failures = partition(&block)
  matches + failures 
end

#uniq_byObject

Just like uniq, but lets you use an attribute or quality of an object for the comparison test rather than the object’s structure.

["Sarah", "SARAH", "sarah"].uniq_by { |x| x.downcase }
# => ["Sarah"]


34
35
36
37
38
39
40
# File 'lib/robot_enumerable.rb', line 34

def uniq_by
  seen = {}
  select { |v|
    key = yield(v)
    (seen[key]) ? nil : (seen[key] = true)
  }
end