From 9bdb4b4b0817151ba4ee98d857ac50d6b841b51a Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 7 Feb 2005 15:24:09 +0000 Subject: [PATCH] * lib/resolv.rb (Resolv::DNS::Name#subdomain_of?): new method. (Resolv::DNS::Name#inspect): ditto. Suggested by Sam Roberts. [ruby-talk:129086] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ lib/resolv.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/ChangeLog b/ChangeLog index c9ab5be542..1a54060531 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Feb 8 00:19:02 2005 Tanaka Akira + + * lib/resolv.rb (Resolv::DNS::Name#subdomain_of?): new method. + (Resolv::DNS::Name#inspect): ditto. + Suggested by Sam Roberts. [ruby-talk:129086] + Mon Feb 7 23:14:11 2005 Tanaka Akira * io.c (io_getc): flush rb_stdout before read fro stdin, which is diff --git a/lib/resolv.rb b/lib/resolv.rb index 3861d9190d..e55bb72654 100644 --- a/lib/resolv.rb +++ b/lib/resolv.rb @@ -978,15 +978,38 @@ class Resolv @absolute = absolute end + def inspect + "#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>" + end + def absolute? return @absolute end def ==(other) + return false unless Name === other return @labels == other.to_a && @absolute == other.absolute? end alias eql? == + # tests subdomain-of relation. + # + # domain = Resolv::DNS::Name.create("y.z") + # p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true + # p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true + # p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false + # p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false + # p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false + # p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false + # + def subdomain_of?(other) + raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other + return false if @absolute != other.absolute? + other_len = other.length + return false if @labels.length <= other_len + return @labels[-other_len, other_len] == other.to_a + end + def hash return @labels.hash ^ @absolute.hash end @@ -1003,6 +1026,14 @@ class Resolv return @labels[i] end + # returns the domain name as a string. + # + # The domain name doesn't have a trailing dot even if the name object is + # absolute. + # + # p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z" + # p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z" + # def to_s return @labels.join('.') end