16 Jul 2013
- Classes are instances of Class which is a subclass of Module
- Objects of the same class can have different sets of instance variables as they only exist upon assignment
- The object that the method is called on is referred to as the reciever
- “one step to the right and then up” rule, go over to the right (into the object’s class) and then up the ancestery chain looking for the method
- To get a Class’s ancestery chain do:
- Modules are above the including class in the ancestery chain
- self starts as “main”. When a method is called on an object that object becomes self. From that point forward, any method call on a different object results in transfer of self to that other object.
- private methods can only be called by an implicit reciever
Bindings are essentially scopes without code. They are packaged scopes that you can be used with eval later on to evaluate code within a given context. Example:
TOPLEVEL_BINDING allows you to access the top-level scope from anywhere.
Tainted Strings and Safe Levels
In Ruby, tainted strings are those that Ruby marks as coming from an external source (not clear on how it makes this designation). Calling tainted? will allow you to determine if an object is from or mixed with an external source. This helps when deciding on whether to use eval() on a string. Further, setting the ruby value $SAFE sets the safe level, which allows or disallows certain operations. The range is 0..4 and anything other than 0 disallows evaluating tainted strings.
Singleton methods are those which are defined on a single object:
Class methods are a type of singleton object. When defining a class method, you are actual using a self to refrence the Class object you are working on.
Call methods based on their name
Dynamically create methods at runtime
Dynamic Proxies (e.g. ActiveRecord) forwards ghost method calls to another object (e.g. a database). Be mindful of:
- Name collisions with real methods. Use either a blank slate or make the class inherit from BasicObject.
- Infinite loops from missing method bugs within the patched missing_method
- Use “super” to call Object’s method_missing if your wrapped object doesn’t respond to your method name.
remove methods from Object to prevent method name collisions