Class: Whatmask
- Inherits:
-
Object
- Object
- Whatmask
- 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
- #address ⇒ Object
- #avail ⇒ Object
- #broadcast ⇒ Object
- #cidr ⇒ Object
- #first ⇒ Object
- #hex ⇒ Object
-
#initialize(mask, address = nil) ⇒ Whatmask
constructor
A new instance of Whatmask.
- #inverse ⇒ Object
- #last ⇒ Object
- #mask ⇒ Object
- #network ⇒ Object
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
#address ⇒ Object
14 15 16 |
# File 'lib/whatmask.rb', line 14 def address return bin_to_octets(@address) if @address end |
#avail ⇒ Object
56 57 58 59 |
# File 'lib/whatmask.rb', line 56 def avail return ((2**(32-cidr))-2) if cidr <= 30 return 1 end |
#broadcast ⇒ Object
22 23 24 |
# File 'lib/whatmask.rb', line 22 def broadcast return bin_to_octets(get_broadcast) if @address end |
#cidr ⇒ Object
52 53 54 |
# File 'lib/whatmask.rb', line 52 def cidr return @mask.to_s(2).count('1') end |
#first ⇒ Object
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 |
#hex ⇒ Object
48 49 50 |
# File 'lib/whatmask.rb', line 48 def hex return bin_to_hex(@mask) end |
#inverse ⇒ Object
40 41 42 |
# File 'lib/whatmask.rb', line 40 def inverse return bin_to_octets(get_inverse) end |
#last ⇒ Object
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 |
#mask ⇒ Object
44 45 46 |
# File 'lib/whatmask.rb', line 44 def mask return bin_to_octets(@mask) end |
#network ⇒ Object
18 19 20 |
# File 'lib/whatmask.rb', line 18 def network return bin_to_octets(get_network) if @address end |