Prev | Current Page 104 | Next

Brad Ediger

"Advanced Rails"


Enumerable.included_in_classes.include? Array # => true
Enumerable.included_in_classes.include? String # => true
Enumerable.included_in_classes.include? Numeric # => false
??? Module#parent and Module#parents inspect the module namespace hierarchy:
module A
module B
class C
end
end
end
Object
Numeric Conversions | 71
A::B::C.parent # => A::B
A::B.parent # => A
A.parent # => Object
A::B::C.parents # => [A::B, A, Object]
Numeric Conversions core_ext/numeric/bytes.rb
These methods convert byte expressions, such as 45.megabytes, into the equivalent number
of bytes (45.megabytes == 47_185_920). As with numeric conversions to time units, both
singular and plural units are accepted (1.kilobyte or 3.kilobytes). Valid units are bytes,
kilobytes, megabytes, gigabytes, terabytes, petabytes, and exabytes.
Object
instance_exec core_ext/object/extending.rb, core_ext/proc.rb
The instance_exec method allows us to evaluate a block in the context of an instance. It is
like instance_eval, except it allows arguments to be passed into the block. An implementation
is in object/extending.rb.
The instance_exec implementation uses the Proc#bind implementation from proc.rb. This
method allows Procs to be treated like UnboundMethods; they can be bound to an object and
invoked as methods. The implementation is instructive:
class Proc #:nodoc:
def bind(object)
block, time = self, Time.


Pages:
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116