class RangeHash
Public Instance Methods
[](key)
click to toggle source
# File lib/rangehash.rb, line 5 def [](key) value = find_value key value == :notfound ? default : value end
assoc(key)
click to toggle source
# File lib/rangehash.rb, line 18 def assoc(key) found = find_pair(key) found.empty? ? nil : found.first end
delete(key)
click to toggle source
Calls superclass method
# File lib/rangehash.rb, line 32 def delete(key) super end
fetch(key, default = nil) { |key| ... }
click to toggle source
# File lib/rangehash.rb, line 10 def fetch(key, default = nil) value = find_value key return value if value != :notfound return default if default != nil yield key if block_given? raise IndexError, "key " + key.to_s + " not found" if not block_given? end
key?(key)
click to toggle source
# File lib/rangehash.rb, line 23 def key?(key) found = find_pair(key) !(found.empty?) end
sorted_keys()
click to toggle source
# File lib/rangehash.rb, line 36 def sorted_keys keys.sort do |a, b| sort_key(a) <=> sort_key(b) end end
Private Instance Methods
find_pair(key)
click to toggle source
# File lib/rangehash.rb, line 43 def find_pair(key) each.select {|k, v| (k == key || k === key)} end
find_value(key)
click to toggle source
# File lib/rangehash.rb, line 47 def find_value(key) (find_pair(key).first || [nil, :notfound])[1] end
sort_key(a)
click to toggle source
# File lib/rangehash.rb, line 51 def sort_key(a) return Range === a ? a.first : a end