Sha256: 26ee5945b8568b27a605e134e5651b6b0b066eda06107adaa6516908f00c619a

Contents?: true

Size: 1.99 KB

Versions: 68

Compression:

Stored size: 1.99 KB

Contents

# Note: Must be required explicitely!
# This is a best attempt to fake BasicObject in Ruby 1.8.x
# What you do get:
#  * as few methods as the real BasicObject (at the moment the library is required...)
#  * BasicObject === <anything> # ==> returns true
# What you don't get:
#  * BasicObject is not in the ancestor list of all classes and thus
#  * Comparisons between classes won't work, e.g.
#      Object < BasicObject # ==> returns true instead of false
#  * Instance methods added to Object or Kernel after you require 'backports/basic_object'
#    might also be available in instances of BasicObject and subclasses
#    (they will only be undefined whenever a subclass of BasicObject is created)
# Because of all the fineprint, BasicObject must be required explicitely

unless Object.const_defined? :BasicObject

  class BasicObject
    KEEP = %w[== equal? ! != instance_eval instance_exec __send__]
    KEEP.concat KEEP.map { |e| e.to_sym }

    # undefine almost all instance methods
    begin
      old_verbose, $VERBOSE = $VERBOSE, nil # silence the warning for undefining __id__
      (instance_methods - KEEP).each do |method|
        undef_method method
      end
    ensure
      $VERBOSE = old_verbose
    end

    class << self
      def === (cmp)
        true
      end

      # Let's try to keep things clean, in case methods have been added to Object
      # either directly or through an included module.
      # We'll do this whenever a class is derived from BasicObject
      # Ideally, we'd do this by trapping Object.method_added
      # and M.method_added for any module M included in Object or a submodule
      # Seems really though to get right, but pull requests welcome ;-)
      def inherited(sub)
        BasicObject.class_eval do
          (instance_methods - KEEP).each do |method|
            if Object.method_defined?(method) && instance_method(method).owner == Object.instance_method(method).owner
              undef_method method
            end
          end
        end
      end
    end
  end
end

Version data entries

68 entries across 68 versions & 5 rubygems

Version Path
backports-3.25.1 lib/backports/basic_object.rb
backports-3.25.0 lib/backports/basic_object.rb
backports-3.24.1 lib/backports/basic_object.rb
backports-3.24.0 lib/backports/basic_object.rb
backports-3.23.0 lib/backports/basic_object.rb
backports-3.22.1 lib/backports/basic_object.rb
backports-3.22.0 lib/backports/basic_object.rb
backports-3.21.0 lib/backports/basic_object.rb
backports-3.20.2 lib/backports/basic_object.rb
backports-3.20.1 lib/backports/basic_object.rb
backports-3.20.0 lib/backports/basic_object.rb
backports-3.19.0 lib/backports/basic_object.rb
backports-3.18.2 lib/backports/basic_object.rb
backports-3.18.1 lib/backports/basic_object.rb
backports-3.18.0 lib/backports/basic_object.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/backports-3.12.0/lib/backports/basic_object.rb
backports-3.17.2 lib/backports/basic_object.rb
backports-3.17.1 lib/backports/basic_object.rb
backports-3.17.0 lib/backports/basic_object.rb
backports-3.16.1 lib/backports/basic_object.rb