Sha256: 4b059eae3aa1efe8cc8215ce3f290b43a6bb71835248035cf53ead5a2cc1198f

Contents?: true

Size: 1.72 KB

Versions: 16

Compression:

Stored size: 1.72 KB

Contents

# = uninheritable.rb
#
# == Copyright (c) 2003 Austin Ziegler
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Author(s)
#
# * Austin Ziegler

# Author::    Austin Ziegler
# Copyright:: Copyright (c) 2003 Austin Ziegler
# License::   Ruby License

# = Uninheritable
#
# Allows an object to declare itself as unable to be subclassed. The
# technique behind this is very simple (redefinition of #inherited), so this
# just provides an easier way to do the same thing with a consistent error
# message.
#
# == Usage
#
#   class A
#     extend Uninheritable
#   end
#
#   class B < A; end # => raises TypeError
#

module Uninheritable
  # Redefines a class's #inherited definition to prevent subclassing of the
  # class extended by this module.
  def inherited(klass)
    raise TypeError, "Class #{self} cannot be subclassed."
  end
end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin testing

require 'test/unit'

class TC_Uninheritable < Test::Unit::TestCase

  class Cannot
    extend Uninheritable
  end

  class Can
  end

  def test_module
    assert_nothing_raised {
      self.instance_eval <<-EOS
        class A < Can; end
      EOS
    }
    assert_raises(TypeError, "Class Cannot cannot be subclassed.") do
      self.instance_eval <<-EOS
        class B < Cannot; end
      EOS
    end
  end

end

=end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
facets-1.4.2 lib/facets/more/uninheritable.rb
facets-1.4.0 lib/facets/more/uninheritable.rb
facets-1.4.1 lib/facets/more/uninheritable.rb
facets-1.4.3 lib/facets/more/uninheritable.rb
facets-1.4.4 lib/facets/more/uninheritable.rb
facets-1.4.5 lib/facets/more/uninheritable.rb
facets-1.7.0 lib/facets/more/uninheritable.rb
facets-1.7.30 lib/facets/more/uninheritable.rb
facets-1.7.38 lib/facets/more/uninheritable.rb
facets-1.7.46 lib/facets/more/uninheritable.rb
facets-1.8.20 lib/facets/more/uninheritable.rb
facets-1.8.49 lib/facets/more/uninheritable.rb
facets-1.8.51 lib/facets/more/uninheritable.rb
facets-1.8.0 lib/facets/more/uninheritable.rb
facets-1.8.54 lib/facets/more/uninheritable.rb
facets-1.8.8 lib/facets/more/uninheritable.rb