Sha256: 3acd067ea0cb294a617c5fcb9d976b317a5bae72ad95c147a5f190de59b2deb5

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

=begin rdoc

= 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

== Legal

Adapted from Ruby Treasures 0.4
Copyright (C) 2002 Paul Brannan <paul@atdesk.com>

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.

=end

module NotCopyable
  def clone
    raise TypeError, "Object not copyable"
  end
  def dup
    raise TypeError, "Object not copyable"
  end
  # if object/copy has been required?
  def copy
    raise TypeError, "Object not copyable"
  end
  alias_method( :deep_copy, :copy )
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat/notcopyable.rb