diff options
| author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-10-04 18:56:32 +0000 |
|---|---|---|
| committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-10-04 18:56:32 +0000 |
| commit | 97f82f2c5bb991cde4866f0879dbc3cbf61b7848 (patch) | |
| tree | 6f27e43cbaa0e9d52fe2c1a6171e192b71358331 /lib | |
| parent | c48637df5431625cbd9c89b73da73e1abfa53ff1 (diff) | |
| download | ruby-97f82f2c5bb991cde4866f0879dbc3cbf61b7848.tar.gz ruby-97f82f2c5bb991cde4866f0879dbc3cbf61b7848.tar.xz ruby-97f82f2c5bb991cde4866f0879dbc3cbf61b7848.zip | |
* lib/ipaddr.rb (<=>): Implement IPAddr#<=> and make IPAddr
comparable.
* lib/ipaddr.rb (succ): Implement IPAddr#succ. You can now create
a range between two IPAddr's, which (Range) object is
enumeratable.
* lib/ipaddr.rb (to_range): A new method to create a Range object
for the (network) address.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@13628 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ipaddr.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 336c5e1b2..56b906c80 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -312,6 +312,37 @@ class IPAddr return _reverse + ".ip6.int" end + # Returns the successor to the ipaddr. + def succ + return self.clone.set(@addr + 1, @family) + end + + # Compares the ipaddr with another. + def <=>(other) + other = coerce_other(other) + + return nil if other.family != @family + + return @addr <=> other.to_i + end + include Comparable + + # Creates a Range object for the network address. + def to_range + begin_addr = (@addr & @mask_addr) + + case @family + when Socket::AF_INET + end_addr = (@addr | (IN4MASK ^ @mask_addr)) + when Socket::AF_INET6 + end_addr = (@addr | (IN6MASK ^ @mask_addr)) + else + raise "unsupported address family" + end + + return clone.set(begin_addr, @family)..clone.set(end_addr, @family) + end + # Returns a string containing a human-readable representation of the # ipaddr. ("#<IPAddr: family:address/mask>") def inspect |
