Module: IPAddrExt::Extensions
- Defined in:
- lib/ipaddr-ext/extensions.rb
Instance Method Summary collapse
-
#+(offset) ⇒ Object
Returns a address greater than the original address by offset.
-
#-(offset) ⇒ Object
Returns a address less than the original address by offset.
-
#==(other) ⇒ Object
Returns true if two ipaddrs are equal.
-
#broadcast ⇒ Object
Returns the broadcast address.
-
#to_host ⇒ Object
Returns the host address.
-
#to_s_with_prefix ⇒ Object
Returns the address with prefix.
-
#wildcard_mask ⇒ Object
Returns the wildcard mask in string format e.g.
Instance Method Details
#+(offset) ⇒ Object
Returns a address greater than the original address by offset
59 60 61 |
# File 'lib/ipaddr-ext/extensions.rb', line 59 def +(offset) self.clone.set(@addr + offset, @family) end |
#-(offset) ⇒ Object
Returns a address less than the original address by offset
67 68 69 |
# File 'lib/ipaddr-ext/extensions.rb', line 67 def -(offset) self.clone.set(@addr - offset, @family) end |
#==(other) ⇒ Object
Returns true if two ipaddrs are equal. Overwrite original == method, fixing to compare address with prefix
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ipaddr-ext/extensions.rb', line 42 def ==(other) # Fix inconsistent behavior between `IPAddr.new("0.0.0.0") == nil` and `nil == IPAddr.new("0.0.0.0")` # https://github.com/ruby/ipaddr/pull/76 if other.nil? return false end other = coerce_other(other) rescue false else @family == other.family && @addr == other.to_i && prefix == other.prefix end |
#broadcast ⇒ Object
Returns the broadcast address
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ipaddr-ext/extensions.rb', line 6 def broadcast case @family when Socket::AF_INET unless prefix == 32 broadcast_addr = @addr + (IPAddr::IN4MASK ^ @mask_addr) return self.clone.set(broadcast_addr, @family) else return self.clone end when Socket::AF_INET6 nil else raise AddressFamilyError, "unsupported address family" end end |
#to_host ⇒ Object
Returns the host address
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ipaddr-ext/extensions.rb', line 73 def to_host case @family when Socket::AF_INET self.mask(32) when Socket::AF_INET6 self.mask(128) else raise AddressFamilyError, "unsupported address family" end end |
#to_s_with_prefix ⇒ Object
Returns the address with prefix
85 86 87 88 89 90 91 |
# File 'lib/ipaddr-ext/extensions.rb', line 85 def to_s_with_prefix if defined?(IPAddr::VERSION) && Gem::Version.new(IPAddr::VERSION) >= Gem::Version.new("1.2.7") # Ruby 3.4+ default gem cidr else "#{self.to_s}/#{prefix}" end end |
#wildcard_mask ⇒ Object
Returns the wildcard mask in string format e.g. 0.0.255.255
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ipaddr-ext/extensions.rb', line 26 def wildcard_mask case @family when Socket::AF_INET mask = IPAddr::IN4MASK ^ @mask_addr when Socket::AF_INET6 mask = IPAddr::IN6MASK ^ @mask_addr else raise AddressFamilyError, "unsupported address family" end _to_string(mask) end |