Class: IP::IP
- Inherits:
-
Object
- Object
- IP::IP
- Defined in:
- lib/ip.rb
Instance Attribute Summary collapse
-
#subnet ⇒ Object
readonly
Returns the value of attribute subnet.
Instance Method Summary collapse
-
#initialize(ip) ⇒ IP
constructor
This constructor accepts both CIDR-notated IP and also exact IP.
- #to_binary ⇒ Object
Constructor Details
#initialize(ip) ⇒ IP
This constructor accepts both CIDR-notated IP and also exact IP.
7 8 9 10 |
# File 'lib/ip.rb', line 7 def initialize(ip) @address = IPAddr.new(ip) @subnet = 32 - Math.log2(@address.to_range.to_a.size).round end |
Instance Attribute Details
#subnet ⇒ Object (readonly)
Returns the value of attribute subnet.
4 5 6 |
# File 'lib/ip.rb', line 4 def subnet @subnet end |
Instance Method Details
#to_binary ⇒ Object
12 13 14 15 |
# File 'lib/ip.rb', line 12 def to_binary binary = @address.to_i.to_s(2).rjust(32, '0') binary.slice(0, @subnet) end |