Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

insensitive_hash.rb: internal cleanup

  • Loading branch information...
commit e600480b9ed80711ac8aee2bdc64e0c9cbe64f66 1 parent 616a366
@quix authored
Showing with 15 additions and 13 deletions.
  1. +15 −13 lib/quix/insensitive_hash.rb
View
28 lib/quix/insensitive_hash.rb
@@ -8,16 +8,18 @@ class InsensitiveHash
DataPair = Struct.new(:key, :value)
- def to_neutral_key(key)
+ def transform_key(key)
key.to_s.downcase
end
def dup
- result = InsensitiveHash.new
+ result = self.class.new
each_pair { |key, value|
result[key] = value
}
- result.taint if self.tainted?
+ if tainted?
+ result.taint
+ end
result
end
@@ -79,7 +81,7 @@ def ==(other)
end
def [](key)
- if data = @hash[to_neutral_key(key)]
+ if data = @hash[transform_key(key)]
data.value
elsif @default
@default
@@ -93,7 +95,7 @@ def [](key)
end
def []=(key, value)
- @hash[to_neutral_key(key)] = DataPair.new(key, value)
+ @hash[transform_key(key)] = DataPair.new(key, value)
value
end
@@ -116,7 +118,7 @@ def default_proc
end
def delete(key)
- neutral_key = to_neutral_key(key)
+ neutral_key = transform_key(key)
if @hash.has_key?(neutral_key)
@hash.delete(neutral_key).value
elsif block_given?
@@ -127,9 +129,9 @@ def delete(key)
end
def delete_if
- dup.each_pair { |key, value|
- if yield(key, value)
- @hash.delete(to_neutral_key(key))
+ keys.each { |key|
+ if yield key, self[key]
+ @hash.delete(transform_key(key))
end
}
self
@@ -165,7 +167,7 @@ def empty?
def fetch(key, *args, &block)
case args.size
when 0
- @hash.fetch(to_neutral_key(key)) {
+ @hash.fetch(transform_key(key)) {
if block
return block.call(key)
else
@@ -174,7 +176,7 @@ def fetch(key, *args, &block)
end
}.value
when 1
- @hash.fetch(to_neutral_key(key), args.first)
+ @hash.fetch(transform_key(key), args.first)
else
raise ArgumentError,
"wrong number of arguments (#{args.size + 1} for 2)"
@@ -182,7 +184,7 @@ def fetch(key, *args, &block)
end
def has_key?(key)
- @hash.has_key?(to_neutral_key(key))
+ @hash.has_key?(transform_key(key))
end
def has_value?(target)
@@ -274,7 +276,7 @@ def reject!(&block)
changed = false
dup.each_pair { |key, value|
if yield(key, value)
- @hash.delete(to_neutral_key(key))
+ @hash.delete(transform_key(key))
changed = true
end
}
Please sign in to comment.
Something went wrong with that request. Please try again.