Class: ActiveSupport::OrderedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/core-extensions/ordered_hash.rb

Instance Method Summary collapse

Instance Method Details

#insert_at_start(key, value) ⇒ Object

Insert a given element at the beginning, not end, of an ordered hash.



17
18
19
# File 'lib/core-extensions/ordered_hash.rb', line 17

def insert_at_start(key,value)
replace(OrderedHash[self.to_a.insert(0,[key,value])])
end

#stable_select(&block) ⇒ Object

Version of enumerable#select for an OrderedHash which is order-preserving Output is an array of key-value pairs.



11
12
13
14
# File 'lib/core-extensions/ordered_hash.rb', line 11

def stable_select(&block)
  #Annoyingly, default ordered hash select is not stable
  self.map{|k,v| block.call(k,v) ? [k,v] : nil}.compact
end