Class: Whatmask

Inherits:
Object
  • Object
show all
Defined in:
lib/whatmask/version.rb,
lib/whatmask.rb

Overview

Copyright (c) 2015 Mike Gee (geezyx at gmail.com)

whatmask is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

whatmask is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with whatmask. If not, see http://www.gnu.org/licenses/.

Constant Summary collapse

VERSION =

whatmask version

"1.0.8"

Instance Method Summary collapse

Constructor Details

#initialize(mask, address = nil) ⇒ Whatmask

Returns a new instance of Whatmask.



5
6
7
8
9
10
11
12
# File 'lib/whatmask.rb', line 5

def initialize(mask, address=nil)
  @mask = get_mask(mask)
  if /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(address)
    @address = octets_to_bin(address)
  end
  @allones = "11111111111111111111111111111111".to_i(2)
  validate(@mask)
end

Instance Method Details

#addressObject



14
15
16
# File 'lib/whatmask.rb', line 14

def address
  return bin_to_octets(@address) if @address
end

#availObject



56
57
58
59
# File 'lib/whatmask.rb', line 56

def avail
  return ((2**(32-cidr))-2) if cidr <= 30
  return 1
end

#broadcastObject



22
23
24
# File 'lib/whatmask.rb', line 22

def broadcast
  return bin_to_octets(get_broadcast) if @address
end

#cidrObject



52
53
54
# File 'lib/whatmask.rb', line 52

def cidr
  return @mask.to_s(2).count('1')
end

#firstObject



26
27
28
29
30
31
# File 'lib/whatmask.rb', line 26

def first
  if @mask != @allones
    return bin_to_octets(get_network+1) if @address
  end
  return address if @address
end

#hexObject



48
49
50
# File 'lib/whatmask.rb', line 48

def hex
  return bin_to_hex(@mask)
end

#inverseObject



40
41
42
# File 'lib/whatmask.rb', line 40

def inverse
  return bin_to_octets(get_inverse)
end

#lastObject



33
34
35
36
37
38
# File 'lib/whatmask.rb', line 33

def last
  if @mask != @allones
    return bin_to_octets(get_broadcast-1) if @address
  end
  return address if @address
end

#maskObject



44
45
46
# File 'lib/whatmask.rb', line 44

def mask
  return bin_to_octets(@mask)
end

#networkObject



18
19
20
# File 'lib/whatmask.rb', line 18

def network
  return bin_to_octets(get_network) if @address
end