Introducing the subnet_calc gem

require 'subnet_calc'

sc = SubnetCalc.new hosts: 40
sc.to_h #=> {:class_type=>"C", :magic_number=>64, :hosts=>62, :subnet_mask=

puts sc.to_s

Output:

Subnet calculator
=================

Inputs: 

  hosts: 40

Summary
-------

* Network class: C
* magic number: 64
* hosts per subnet: 62
* subnet mask: 255.255.255.192
* subnet bitmask: 11111111.11111111.11111111.11000000
* prefix bit-length: 26
* range: 192.168.0.1-192.168.0.62

* subnet_bits: 2
* maximum subnets: 4



Breakdown
---------

4th octet:

  | 128 | 64  | 32  | 16  | 8   | 4   | 2   | 1   |
  |:----|:----|:----|:----|:----|:----|:----|:----|
  | 1   | 1   | 0   | 0   | 0   | 0   | 0   | 0   |
  | /25 | /26 | /27 | /28 | /29 | /30 | /31 | /32 |


### Subnets


  | Network | 1st | last | broadcast |
  |:--------|:----|:-----|:----------|
  | 0       | 1   | 62   | 63        |
  | 64      | 65  | 126  | 127       |
  | 128     | 129 | 190  | 191       |
  | 192     | 193 | 254  | 255       |


-----------------------------------------------

Resources