Object#clone

obj.clone -> obj
Returns: obj · Updated March 13, 2026 · Core Classes
objects copy duplication

clone creates a shallow copy of an object. Unlike dup, it also copies the object’s singleton class (methods defined on that specific object) and preserves the frozen status.

Syntax

obj.clone

Parameters

clone accepts no parameters.

Examples

Basic usage

original = "hello"
copy = original.clone

original << " world"
puts original   # => "hello world"
puts copy       # => "hello"

Preserving freeze status

original = "immutable".freeze
copy_clone = original.clone

# clone preserves frozen status
puts copy_clone.frozen?  # => true

Cloning with singleton methods

obj = Object.new
def obj.greet; "hello"; end

cloned = obj.clone
cloned.respond_to?(:greet)  # => true
# dup would not include the greet method

Cloning an array

original = [1, 2, [3, 4]]
copy = original.clone

original[2] << 5
puts original.inspect   # => [1, 2, [3, 4, 5]]
puts copy.inspect       # => [1, 2, [3, 4, 5]] - nested array is shared!

Frozen string literals

In Ruby 3.0+, strings from frozen string literals are frozen by default. When you clone a frozen string, the copy remains frozen:

frozen = "frozen".freeze

puts frozen.clone.frozen?  # => true (still frozen)

This matters when working with immutable data patterns or performance optimization with frozen strings.

Errors

  • TypeError — If the object is uncloneable (some special objects like symbols before Ruby 3.0).

See Also