---
title: "Avoid exception for dup on Integer"
description: "dup now silently fails for Integer"
canonical_url: "https://www.bigbinary.com/blog/avoid-exceptions-for-dup-on-interger-and-similar-cases"
markdown_url: "https://www.bigbinary.com/blog/avoid-exceptions-for-dup-on-interger-and-similar-cases.md"
---

# Avoid exception for dup on Integer

dup now silently fails for Integer

- Author: Rohit Arolkar
- Published: August 1, 2017
- Categories: Ruby 2.4, Ruby

Prior to Ruby 2.4, if we were to `dup` an `Integer`, it would fail with a
`TypeError`.

```ruby

> 1.dup
TypeError: can't dup Fixnum
	from (irb):1:in `dup'
	from (irb):1

```

This was confusing because `Integer#dup` is actually implemented.

```ruby

> Integer.respond_to? :dup
=> true

```

However, if we were to freeze an `Integer` it would fail silently.

```ruby

> 1.freeze
=> 1

```

Ruby 2.4 has now included dup-ability for `Integer` as well.

```ruby

> 1.dup
=> 1

```

In Ruby, some object types are immediate variables and therefore cannot be
duped/cloned. Yet, there was no graceful way of averting the error thrown by the
sanity check when we attempt to dup/clone them.

So now `Integer#dup` functions exactly the way `freeze` does -- fail silently
and return the object itself. It makes sense because nothing about these objects
can be changed in the first place.

## Links

- [Human page](https://www.bigbinary.com/blog/avoid-exceptions-for-dup-on-interger-and-similar-cases)
