Module: NotCopyable

Defined in:
lib/carat/notcopyable.rb

Overview

NotCopyable

Disable copying via dup and clone (and copy).

Usage

require 'carat/notcopyable.rb'

Class FixedString < String
  include NotCopyable
end

s = FixedString.new("Don't tred on me!")
puts s
s2 = s.dup

produces

Don't tred on me!
TypeError, "Object not copyable"

Todo

  • If one is reopening a class and including this, it will not work if the class defined #dub, #clone or #copy (and #deep_copy) itself. It only work if it’s ancestors are responsible for those methods. How to fix?

Notes

  • I don’t think this will prove very useful, but just in case here it is.

Author(s)

  • Paul Brannan <paul at atdesk.com>

  • Thomas Sawyer

Adapted from Ruby Treasures 0.4 Copyright © 2002 Paul Brannan <[email protected]>

You may distribute this software under the same terms as Ruby (see the file COPYING that was distributed with this library).

History

  • 2005.04.11 Passed basic test.

Instance Method Summary collapse

Instance Method Details

#cloneObject

Raises:

  • (TypeError)


56
57
58
# File 'lib/carat/notcopyable.rb', line 56

def clone
  raise TypeError, "Object not copyable"
end

#copyObject Also known as: deep_copy

if object/copy has been required?

Raises:

  • (TypeError)


63
64
65
# File 'lib/carat/notcopyable.rb', line 63

def copy
  raise TypeError, "Object not copyable"
end

#dupObject

Raises:

  • (TypeError)


59
60
61
# File 'lib/carat/notcopyable.rb', line 59

def dup
  raise TypeError, "Object not copyable"
end