Class: RangeHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/rangehash.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



5
6
7
8
# File 'lib/rangehash.rb', line 5

def [](key)
  value = find_value key
  value == :notfound ? default : value
end

#assoc(key) ⇒ Object



18
19
20
21
# File 'lib/rangehash.rb', line 18

def assoc(key)
  found = find_pair(key)
  found.empty? ? nil : found.first
end

#delete(key) ⇒ Object



32
33
34
# File 'lib/rangehash.rb', line 32

def delete(key)
  super
end

#fetch(key, default = nil) {|key| ... } ⇒ Object

Yields:

  • (key)

Raises:

  • (IndexError)


10
11
12
13
14
15
16
# 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) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


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

def key?(key)
  found = find_pair(key)
  !(found.empty?)
end

#sorted_keysObject



36
37
38
39
40
# File 'lib/rangehash.rb', line 36

def sorted_keys
  keys.sort do |a, b|
    sort_key(a) <=> sort_key(b)
  end
end