8/05/2005

more ruby

today, i needed a class for handling IP addresses for hosts and the IPAddr class from the standard library didn’t quite do everything I needed, so I ended up writing one for myself. Now, when people do data entry into my hosts database I can keep them from entering all kinds of hokey, bad data:

here’s some trimmed output from irb:




host = IP_Addr.new(‘192.168.1.0’, ‘255.255.255.0’)
RuntimeError: bad ip address, can’t use network address
host = IP_Addr.new(‘192.168.1.255’, ‘255.255.255.0’)
RuntimeError: bad ip address, can’t use broadcast address
host = IP_Addr.new(‘192.168.1.253’, ‘255.255.255.0’)
host.network
=> “192.168.1.0”
host.broadcast
=> “192.168.1.255”




now, I can count on my db not having network or broadcast addresses (or other invalid addresses that I’m checking for), and I can build dhcp and dns config files automaticly.

No comments: