Class: IpNumeric
Overview
Stores an IP address in numeric form.
IpNumeric instances are immutable, and memoize most of their methods.
Constant Summary
IpAddresslike::ONES
Class Method Summary
collapse
Instance Method Summary
collapse
#bitness_max, #bitness_min, #to_hex, #to_s
Constructor Details
#initialize(addr) ⇒ IpNumeric
Returns a new instance of IpNumeric.
79
80
81
|
# File 'lib/gorillib/type/ip_address.rb', line 79
def initialize(addr)
@packed = addr.to_int
end
|
Class Method Details
.from_dotted(dotted) ⇒ Object
106
107
108
109
110
111
112
|
# File 'lib/gorillib/type/ip_address.rb', line 106
def self.from_dotted(dotted)
ip_a, ip_b, ip_c, ip_d = quads = dotted.split(".", 4).map(&:to_i)
obj = new((ip_a << 24) + (ip_b << 16) + (ip_c << 8) + (ip_d))
obj.instance_variable_set('@dotted', dotted.freeze)
obj.instance_variable_set('@quads', quads.freeze)
obj
end
|
.from_packed(pi) ⇒ Object
102
103
104
|
# File 'lib/gorillib/type/ip_address.rb', line 102
def self.from_packed(pi)
new(pi)
end
|
Instance Method Details
87
|
# File 'lib/gorillib/type/ip_address.rb', line 87
def +(int) ; self.class.new(to_int + int) ; end
|
#<=>(other) ⇒ Object
86
|
# File 'lib/gorillib/type/ip_address.rb', line 86
def <=>(other) ; packed <=> other.to_int ; end
|
#==(other) ⇒ Object
85
|
# File 'lib/gorillib/type/ip_address.rb', line 85
def ==(other) ; packed == other.to_int ; end
|
92
93
94
|
# File 'lib/gorillib/type/ip_address.rb', line 92
def dotted
@dotted ||= quads.join('.').freeze
end
|
90
|
# File 'lib/gorillib/type/ip_address.rb', line 90
def packed ; @packed ; end
|
96
97
98
|
# File 'lib/gorillib/type/ip_address.rb', line 96
def quads
@quads ||= [ (@packed >> 24) & 0xFF, (@packed >> 16) & 0xFF, (@packed >> 8) & 0xFF, (@packed) & 0xFF ].freeze
end
|
#receive(val) ⇒ Object
75
76
77
|
# File 'lib/gorillib/type/ip_address.rb', line 75
def receive(val)
new(val)
end
|
83
|
# File 'lib/gorillib/type/ip_address.rb', line 83
def to_i ; packed ; end
|
84
|
# File 'lib/gorillib/type/ip_address.rb', line 84
def to_int ; packed ; end
|