8 How to Trim a String & Remove White Space.5 How to Find Out If a String Contains Another String.Numeric types, for example, perform type conversion across #=, but not across eql?, so: 1 = 1.0 #=> true 1. Subclasses normally continue this tradition by aliasing eql? to their overridden #= method, but there are exceptions. eql? 1.0 #=> falseįor objects of class Object, eql? is synonymous with #=. So any subclass that overrides eql? should also override hash appropriately.įor objects of class Object, eql? is synonymous with #=. For any pair of objects where eql? returns true, the hash value of both objects must be equal. This is used by Hash to test members for equality. The eql? method returns true if obj and other refer to the same hash key. Unlike #=, the equal? method should never be overridden by subclasses as it is used to determine object identity (that is, a.equal?(b) if and only if a is the same object as b): obj = "a" other = obj. Typically, this method is overridden in descendant classes to provide class-specific meaning. Method called when a singleton method is undefined in self.Įquality - At the Object level, #= returns true only if obj and other are the same object. Method called when a singleton method is added removed from self. Method called when a singleton method is added to self.
Method called when an undefined method is called on self. Returns whether self and the given object are the same object.Įvaluates the given string or block in the context of self.Įxecutes the given block in the context of self, passing the given arguments. Returns the integer object identifier for self.Ĭalls the method identified by the given symbol. Returns whether self and the given object are equivalent. Returns whether self and the given object are not equal. Returns the boolean negation of self: true or false. These are the methods defined for BasicObject: Like method_missing, const_missing can be used to delegate constant lookup to Object: class MyObjectSystem < BasicObject def self. include?( name) or super end endĪccess to classes and modules from the Ruby standard library can be obtained in a BasicObject subclass by referencing the desired constant from the root like ::File or ::Enumerator. send( name, * args, & block)Įnd def respond_to_missing?( name, include_private = false)ĭELEGATE. A custom Kernel-like module could be created and included or delegation can be used via method_missing: class MyObjectSystem < BasicObject DELEGATE = ĭef method_missing( name, * args, & block) A subclass could include Kernel to obtain puts, exit, etc.
To avoid polluting BasicObject for other users an appropriately named subclass of BasicObject should be created instead of directly modifying BasicObject: class MyObjectSystem < BasicObject endīasicObject does not include Kernel (for methods like puts) and BasicObject is outside of the namespace of the standard library so common classes will not be found without using a full class path.Ī variety of strategies can be used to provide useful portions of the standard library to subclasses of BasicObject. It's an explicit blank class.īasicObject can be used for creating object hierarchies independent of Ruby's object hierarchy, proxy objects like the Delegator class, or other uses where namespace pollution from Ruby's methods and classes must be avoided. BasicObject is the parent class of all classes in Ruby.