module HashOrderHelper
Public Instance Methods
>>(receiver)
click to toggle source
# File lib/hash_order_helper.rb, line 108 def >>(receiver) unless receiver.is_a?(Hash) raise TypeError, "no implicit conversion of #{receiver.inspect}:#{receiver.class} into Hash" end receiver.unshift(self) end
at(nth)
click to toggle source
# File lib/hash_order_helper.rb, line 30 def at(nth) self.to_a.at(nth) end
insert(nth, insert_hash)
click to toggle source
# File lib/hash_order_helper.rb, line 34 def insert(nth, insert_hash) insert_hash ||= {} hash_keys = self.keys hash_keys.insert(nth, *insert_hash.keys) new_hash = {} hash_keys.each do |key| value = insert_hash.has_key?(key) ? insert_hash[key] : self[key] new_hash[key] = value end self.replace(new_hash) end
last(n = nil)
click to toggle source
# File lib/hash_order_helper.rb, line 48 def last(n = nil) if n.nil? self.to_a.last else self.to_a.last(n) end end
nd_push(push_hash)
click to toggle source
# File lib/hash_order_helper.rb, line 69 def nd_push(push_hash) push_hash ||= {} hash_keys = self.keys - push_hash.keys hash_keys.push(*push_hash.keys) new_hash = {} hash_keys.each do |key| value = push_hash.has_key?(key) ? push_hash[key] : self[key] new_hash[key] = value end new_hash end
Also aliased as: +
nd_unshift(unshift_hash)
click to toggle source
# File lib/hash_order_helper.rb, line 89 def nd_unshift(unshift_hash) unshift_hash ||= {} hash_keys = self.keys - unshift_hash.keys hash_keys.unshift(*unshift_hash.keys) new_hash = {} hash_keys.each do |key| value = unshift_hash.has_key?(key) ? unshift_hash[key] : self[key] new_hash[key] = value end new_hash end
pop(n = nil)
click to toggle source
# File lib/hash_order_helper.rb, line 56 def pop(n = nil) ary = self.to_a last_values = n.nil? ? ary.pop : ary.pop(n) new_hash = {} ary.each do |k, v| new_hash[k] = v end self.replace(new_hash) last_values end
push(push_hash)
click to toggle source
# File lib/hash_order_helper.rb, line 83 def push(push_hash) new_hash = nd_push(push_hash) self.replace(new_hash) end
Also aliased as: <<
sort_pair()
click to toggle source
# File lib/hash_order_helper.rb, line 2 def sort_pair new_hash = {} self.sort_by(&:to_s).each do |k, v| new_hash[k] = v end new_hash end
sort_pair!()
click to toggle source
# File lib/hash_order_helper.rb, line 12 def sort_pair! self.replace(self.sort_pair) end
sort_pair_by(&block)
click to toggle source
# File lib/hash_order_helper.rb, line 16 def sort_pair_by(&block) new_hash = {} self.sort_by(&block).each do |k, v| new_hash[k] = v end new_hash end
sort_pair_by!(&block)
click to toggle source
# File lib/hash_order_helper.rb, line 26 def sort_pair_by!(&block) self.replace(self.sort_pair_by(&block)) end
unshift(unshift_hash)
click to toggle source
# File lib/hash_order_helper.rb, line 103 def unshift(unshift_hash) new_hash = nd_unshift(unshift_hash) self.replace(new_hash) end