Skip to content

Commit

Permalink
Code cleanup. Moved core extensions into a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Nov 4, 2009
1 parent f35cc57 commit fe55b4f
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 312 deletions.
14 changes: 5 additions & 9 deletions lib/net/dns.rb
@@ -1,10 +1,5 @@
##
#
# dns.rb
#
# $id$
#
##
require 'net/dns/core_ext'


module Net # :nodoc:
module DNS
Expand All @@ -30,6 +25,7 @@ module DNS
# Size of a short int
INT16SZ = 2


module QueryTypes

SIGZERO = 0
Expand Down Expand Up @@ -113,5 +109,5 @@ module QueryClasses
include QueryTypes
include QueryClasses

end # module DNS
end # module Net
end
end
52 changes: 52 additions & 0 deletions lib/net/dns/core_ext.rb
@@ -0,0 +1,52 @@
module Net # :nodoc:
module DNS

module HashKeys # :nodoc:

# Returns an hash with all the
# keys turned into downcase
#
# hsh = {"Test" => 1, "FooBar" => 2}
# hsh.downcase_keys!
# #=> {"test"=>1,"foobar"=>2}
#
def downcase_keys!
hsh = Hash.new
self.each do |key,val|
hsh[key.downcase] = val
end
self.replace(hsh)
end

end

module HashOperators # :nodoc:

# Performs a sort of group difference
# operation on hashes or arrays
#
# a = {:a=>1,:b=>2,:c=>3}
# b = {:a=>1,:b=>2}
# c = [:a,:c]
# a-b #=> {:c=>3}
# a-c #=> {:b=>2}
#
def -(other)
case other
when Hash
delete_if { |k,v| other.has_key?(k) }
when Array
delete_if { |k,v| other.include?(k) }
end
end

end

end
end


class Hash # :nodoc:
include Net::DNS::HashKeys
include Net::DNS::HashOperators
end

0 comments on commit fe55b4f

Please sign in to comment.