# ruby/ipaddr: The IP Address Class Already Inside Your Ruby

> IPAddr is a default gem for parsing, masking and comparing IPv4 and IPv6 addresses. It is small, it ships with Ruby, and the README itself points elsewhere when you need more.

**ruby/ipaddr** — A class to manipulate an IP address

- Repository: https://github.com/ruby/ipaddr
- Stars: 82 · Forks: 42
- Language: Ruby
- License: BSD-2-Clause
- Published: 2026-08-21 · Updated: 2026-08-21 · Language: en
- Canonical page: https://hysenlabs.com/projects/ruby-ipaddr

## What ruby/ipaddr actually solves

Ruby has no IP address type in its core syntax. The standard library's Socket and Resolv modules deal with hostnames and sockets, not with network arithmetic. IPAddr fills that gap: it is a class that represents a single address or a whole network block, and it understands both IPv4 and IPv6. The README describes it as providing "a set of methods to manipulate an IP address." That is the entire scope.

The audience is Ruby developers. If you are writing a firewall rule generator, a subnet allocation script, a log parser that groups requests by network, or a Rails model that stores a CIDR block, IPAddr gives you the parsing and comparison primitives. It is not an IP address management system. It has no database, no allocation policy, no HTTP API. It is a value object with methods.

## How IPAddr represents addresses and masks

An IPAddr instance holds an address and an implicit or explicit netmask. The README's first example constructs one from a string with a prefix length:

```ruby
require 'ipaddr'

ipaddr1 = IPAddr.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 = IPAddr.new "192.168.2.0/24"

p ipaddr3                   #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
```

Two things are visible in that output. First, the address is expanded to full eight-group notation internally, even though `to_s` returns the compressed form `"3ffe:505:2::1"`. Second, the mask is stored alongside the address. When no prefix is given, the mask is all ones: a single host.

The `mask` method returns a new object with a shorter prefix, so the original is untouched. IPv4 works the same way with dotted-quad notation and a prefix length. `IPAddr.new "192.168.2.0/24"` produces an object whose inspect output shows the mask as `255.255.255.0`. The data flow is string in, value object out, string or boolean out. There is no mutable state to manage and no connection to any external service.

## Installing it, and a first real task

IPAddr is part of the standard Ruby distribution as a default gem, so in most cases you do not install anything. The README notes that the gem is "synchronized periodically," which means the version bundled with your Ruby may lag behind the released gem. If you need a newer version than your Ruby ships, the README gives this Gemfile line:

```ruby
gem 'ipaddr', '~> 1.2'
```

Then run `bundle`. To install it without Bundler, the README gives `gem install ipaddr`.

A first real task: check whether an address belongs to a private network. Construct the network with a prefix, construct the address, and compare them. The README's usage section shows construction and masking; it does not document a containment method, so confirm the exact predicate name against your installed version before relying on it in production code. The construction and masking patterns above are the documented ones.

The mask arithmetic is done on the integer value of the address, so it applies uniformly to IPv4 and IPv6. For a `/24` network, an address in the same block and an address outside it should be distinguishable by the mask you apply.

## Where IPAddr stops being the right tool

The README is unusually direct about this: it has an "Alternative" section recommending the `ipaddress` gem, described as "a popular, extensive library for manipulating IP addresses with a layer of compatibility with `ipaddr`." The maintainers are telling you that IPAddr is the minimal option. If your work involves subnet enumeration, address allocation, or the kind of range arithmetic that IPAM tools do, IPAddr will run out of surface area.

There is a second limitation that the README does not address at all: it says nothing about how the class behaves under concurrent mutation, because there is no mutation. Every operation returns a new object. That is good for safety and bad for tight loops that build millions of objects. If you are processing a large log file and constructing an IPAddr per line, you are allocating per line. Whether that matters depends on your workload, and the repository gives no performance guidance either way.

A third boundary: IPAddr is a Ruby library. The related searches show people looking for `ipaddr` in JavaScript, Python, Rust, npm and Ansible. None of those are this project. A search for `ipaddr` on npm or PyPI will land on an unrelated package with a similar name. If your pipeline is not Ruby, IPAddr is not available to you.

## ipaddr against the ipaddress gem

The alternative named by the README is the `ipaddress` gem. The difference in approach is scope and compatibility. IPAddr is a single class in the Ruby standard library with a small method set. The `ipaddress` gem is a third-party library that the README calls "extensive" and that deliberately keeps a compatibility layer with `ipaddr`, so code written against IPAddr's interface has a migration path.

Practically, the choice comes down to dependency policy. IPAddr adds nothing to your Gemfile because it is already there. The `ipaddress` gem is an external dependency you must track, update and audit. If your needs are covered by parsing, masking and containment, the default gem is the smaller commitment. If you find yourself writing helper methods around IPAddr to do things the class does not do, that is the signal the README is pointing at.

Note that the related search terms `ip addr show`, `ip addr add` and `ip addr command` refer to the Linux `ip` command from iproute2, not to this Ruby gem. They are a different tool with a similar name, and the README does not mention iproute2 at all.

## Maintenance, releases and the BSD-2-Clause licence

The repository is not archived. The most recent push was on 2026-04-25, and that date carries the v1.2.9 release. The release before it, v1.2.8, is dated 2026-04-23, and v1.2.7 goes back to 2024-10-19. The spacing suggests maintenance rather than a steady feature cadence: a long quiet period, then two releases two days apart. There is no published roadmap in the repository.

Upgrade cost is low for most users. The gem is versioned and the README shows a pessimistic constraint, `gem 'ipaddr', '~> 1.2'`, which permits patch and minor updates within the 1.2 series. Because it is a default gem, upgrading Ruby can change the IPAddr version under you without any Gemfile change. That is the upgrade path worth watching: a Ruby version bump is also an IPAddr version bump unless you pin it explicitly.

The licence is the 2-Clause BSD License, and the repository carries a `LICENSE.txt` file. BSD-2-Clause is permissive: it allows use, modification and redistribution with the copyright notice and disclaimer retained. This is a description of the licence text, not legal advice. If you redistribute the gem or a modified version, read `LICENSE.txt` in the repository for the exact terms.

## Conclusion

Adopt IPAddr when you need CIDR parsing, masking or containment checks inside a Ruby process and you want no new dependency: it is a default gem, so `require 'ipaddr'` works without a Gemfile entry. Do not adopt it as a general-purpose IP toolkit for other languages, and do not expect it to replace the `ipaddress` gem if you need richer manipulation. Before relying on it, verify the version of the gem that your Ruby ships and whether your project needs a constraint such as `gem 'ipaddr', '~> 1.2'` to get the behaviour you expect.

## FAQ

### What is ruby/ipaddr used for?

It is a Ruby class for manipulating IP addresses, supporting both IPv4 and IPv6. The README describes it as providing a set of methods to manipulate an IP address, and shows constructing instances from strings such as "3ffe:505:2::1" and "192.168.2.0/24".

### How do I install ruby/ipaddr?

You usually do not need to. The README states it is part of the standard Ruby distribution as a default gem, synchronized periodically. If you need a newer version than your Ruby ships, add `gem 'ipaddr', '~> 1.2'` to your Gemfile and run `bundle`, or run `gem install ipaddr`.

### What does a 32 CIDR mean in ruby/ipaddr?

A prefix length of 32 on an IPv4 address means the mask covers the whole address, so the object represents a single host rather than a network block. The README's IPv6 example shows the same idea: an address constructed without a prefix gets an all-ones mask.

## Sources

- [Official README](https://github.com/ruby/ipaddr#readme)
- [Project repository](https://github.com/ruby/ipaddr)
- [Release notes](https://github.com/ruby/ipaddr/releases)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/ruby-ipaddr
