IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and IPv6 are supported.
This library is part of the standard ruby distribution as default gem and synchronized periodically, but you can explicitly depend on this gem with version constraints as necessary, like when you need a newer version than comes with older versions of ruby.
For example, you can add this line to your application's Gemfile:
gem 'ipaddr', '~> 1.2'
And then execute:
Or install it yourself as:
$ gem install ipaddr
require 'ipaddr' ipaddr1 = .new "3ffe:505:2::1" p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff> p ipaddr1.to_s #=> "3ffe:505:2::1" ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000> p ipaddr2.to_s #=> "3ffe:505:2::" ipaddr3 = .new "192.168.2.0/24" p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
The ipaddress gem is a popular,
extensive library for manipulating IP addresses with a layer of
ipaddr. If you need a richer set of features
ipaddr has, try this library instead.
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake test to run the tests. You can also run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run
bundle exec rake install. To release a new version, update the version number in
version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/ipaddr.
The gem is available as open source under the terms of the 2-Clause BSD License.