Class: Babushka::IPRange

Inherits:
IP show all
Defined in:
lib/babushka/ip.rb

Direct Known Subclasses

IPTail

Instance Attribute Summary

Attributes inherited from IP

#bytes

Instance Method Summary collapse

Methods inherited from IP

#==, #initialize, #next, #prev, #to_s

Constructor Details

This class inherits a constructor from Babushka::IP

Instance Method Details

#broadcastObject



129
130
131
132
133
# File 'lib/babushka/ip.rb', line 129

def broadcast
  padded_bytes.map {|byte|
    byte == 'x' ? '255' : byte
  }.join('.')
end

#firstObject



115
116
117
# File 'lib/babushka/ip.rb', line 115

def first
  ip_for 'x.0.0.1'
end

#ip_for(address_part) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/babushka/ip.rb', line 107

def ip_for address_part
  IP.new padded_bytes.zip(
    IPTail.new(address_part).padded_bytes
  ).map {|(network, address)|
    [network, address, 0].detect {|i| i != 'x' }
  }
end

#lastObject



119
120
121
# File 'lib/babushka/ip.rb', line 119

def last
  ip_for 'x.255.255.255'
end

#padded_bytesObject



103
104
105
# File 'lib/babushka/ip.rb', line 103

def padded_bytes
  bytes.concat(['x'] * (4 - bytes.length))
end

#subnetObject



123
124
125
126
127
# File 'lib/babushka/ip.rb', line 123

def subnet
  padded_bytes.map {|byte|
    byte == 'x' ? '0' : '255'
  }.join('.')
end

#valid?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/babushka/ip.rb', line 99

def valid?
  !bytes.empty?
end