Class: IP::IP

Inherits:
Object
  • Object
show all
Defined in:
lib/ip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#subnetObject (readonly)

Returns the value of attribute subnet.



4
5
6
# File 'lib/ip.rb', line 4

def subnet
  @subnet
end

Instance Method Details

#to_binaryObject



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