diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-02-07 15:24:09 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-02-07 15:24:09 +0000 |
commit | f287cc6723f4ab9d9673ae361638e14b41e5ba1d (patch) | |
tree | 6ce4f302add79b643078cc9a511b9909de0e6397 /lib/resolv.rb | |
parent | 0984101b731cf40251abf08f13362c77abe8c038 (diff) |
* 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/branches/ruby_1_8@7915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/resolv.rb')
-rw-r--r-- | lib/resolv.rb | 31 |
1 files changed, 31 insertions, 0 deletions
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 |