Any object can be annotated.
ASSOCIATIONS | = | Hash.new{ |h,k,v| h[k]=[] } |
OPERATORS | = | %w{ +@ -@ + - ** * / % ~ <=> << >> < > === == =~ <= >= | & ^ []= [] } |
OPERATORS_REGEXP | = | Regexp.new( '(' << OPERATORS.collect{ |k| Regexp.escape(k) }.join('|') << ')' ) |
OPERATORS_ESC_TABLE | = | { "+@" => "op_plus_self", "-@" => "op_minus_self", "+" => "op_plus", "-" => "op_minus", "**" => "op_pow", "*" => "op_mul", "/" => "op_div", "%" => "op_mod", "~" => "op_tilde", "<=>" => "op_cmp", "<<" => "op_lshift", ">>" => "op_rshift", "<" => "op_lt", ">" => "op_gt", "===" => "op_case_eq", "==" => "op_equal", "=~" => "op_apply", "<=" => "op_lt_eq", ">=" => "op_gt_eq", "|" => "op_or", "&" => "op_and", "^" => "op_xor", "[]=" => "op_store", "[]" => "op_fetch" |
caller | -> | pp_call_stack |
Parse a caller string and break it into its components, returning an array. Returns:
For example, from irb, call_stack(1) produces [["(irb)", 2, :irb_binding], ["/usr/lib/ruby/1.8/irb/workspace.rb", 52, :irb_binding], ["/usr/lib/ruby/1.8/irb/workspace.rb", 52, nil]] Note: If the user decides to redefine caller() to output data in a different format, prior to requiring this, then the results will be indeterminate. |
||
dup | -> | object_dup |
clone | -> | object_clone |
class | -> | object_class |
Defines object_classas an alias of class. This is an alternative to class, akin to object_id. | ||
class | -> | __class__ |
Defines core method class as an alias of class. This allows you to use class as your own method, without loosing the ability to determine the object‘s class. | ||
is_a? | -> | is? |
May deprecate so as not to confuse with module method. | ||
lambda | -> | fn |
A nice shorthand for lambda.
adder = fn { |x,y| x + y } adder[1,2] #=> 3 |
||
binding | -> | here |
A shorthand pronoun for binding().
a = 3 b = here eval( "a", b ) #=> 3 |
Returns a As-functor that allows one to call any ancestor‘s method directly of the given object.
class A def x ; 1 ; end end class B < A def x ; 2 ; end end class C < B def x ; as(A).x ; end end C.new.x #=> 1
Set instance vars using another object.
class O attr_accessor :d def initialize( a, b, c, d) @a = a @b = b @c = c @d = d end end o1 = O.new(1,2,3,4) o2 = O.new(0,0,0,0) o2.assign_from( o1, '@a', '@b', '@c', '@d' ) o2.instance_eval{ @a } #=> 1 o2.instance_eval{ @b } #=> 2 o2.instance_eval{ @c } #=> 3 o2.instance_eval{ @d } #=> 4
See also assign_with.
Set setter methods and/or vars using a hash (or assoc array). assign_with is a meta-programming method, which allows you to use a hash to do any kind of variable assignment and/or setting:
For example, assuming that there is an accessor defined as d:
assign_with( '@a'=>1, '@@b'=>2, '$c'=>3, 'd'=>4 ) @a #=> 1 @@b #=> 2 $c #=> 3 d #=> 4
Note that while the global variable is strictly unnecessary, it works for completeness sake.
Autoreload feature files.
Automatically reload, at regular intervals, any previously loaded features, and/or other files not already loaded, if they have been modified since the last interval check. A numeric parameter sets the reload interval in seconds and the file parameter can either be a glob string or an array of file paths. If a glob string, it is expanded only once on the initial method call. Supplying a boolean parameter of ‘false’ will force autreload to skip previously loaded features and only reload the specified files. Also keeps a "dirty" flag.
Same as autoreload, but does not include previously loaded features. This is equivalent to as adding a ‘false’ parameter to autoreload.
Returns true is an object is class TrueClass or FalseClass, otherwise false.
true.bool? #=> true false.bool? #=> true nil.bool? #=> false
Raises a ScritBug error with a message.
if find_bug then bug! "unknown bug found" end
Object#cache is essentially like Module#memoize except it can also be used on singleton/eigen methods. OTOH, memoize‘s implementation is arguably better for it‘s use of bind instead of alias. Eventually the two implmenations will be reconciled with a single implmentation.
This is similar to +Module#const_get+ but is accessible at all levels, and, unlike const_get, can handle module hierarchy.
constant("Fixnum") # -> Fixnum constant(:Fixnum) # -> Fixnum constant("Process::Sys") # -> Process::Sys constant("Regexp::MULTILINE") # -> 4 require 'test/unit' Test.constant("Unit::Assertions") # -> Test::Unit::Assertions Test.constant("::Test::Unit") # -> Test::Unit
Anything that can be marshaled can be copied in totality. This is also commonly called a deep_copy.
"ABC".copy #=> "ABC"
Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal.
Adds deep_clone method to an object which produces deep copy of it. It means if you clone a Hash, every nested items and their nested items will be cloned. Moreover deep_clone checks if the object is already cloned to prevent endless recursion.
obj = [] a = [ true, false, obj ] b = a.deep_clone obj.push( 'foo' ) p obj # >> [ 'foo' ] p b[2] # >> []
Anything that can be marshaled can be copied in totality. This is also just called copy.
"ABC".deep_copy #=> "ABC"
Forces the result of a promise to be computed (if necessary) and returns the bare result object. Once evaluated, the result of the promise will be cached. Nested promises will be evaluated together, until the first non-promise result.
If called on a value that is not a promise, it will simply return it.
For debugging and showing examples. Currently this takes an argument of a string in a block.
demo {%{ a = [1,2,3] }} demo {%{ a.slice(1,2) }} demo {%{ a.map { |x| x**3 } }}
Produces:
a = [1,2,3] #=> [1, 2, 3] a.slice(1,2) #=> [2, 3] a.map { |x| x**3 } #=> [1, 8, 27]
Returns true is an object is class FalseClass, otherwise false.
true.false? #=> false false.false? #=> true nil.false? #=> false
Universal assignment. This is a meta-programming method, which allows you to assign any type of variable.
Random generator that returns true or false. Can also take a block that has a 50/50 chance to being executed.
maybe #=> true maybe #=> false
Provides access to an object‘s metaclass (ie. singleton) by-passsing access provisions. So for example:
class X meta.attr_accesser :a end X.a = 1 X.a #=> 1
Easy access to method as objects, and they retain state!
def hello puts "Hello World!" end p method!(:hello) #=> <Method: #hello>
Returns a list of methods according to symbol(s) given.
Usable symbols include:
It no symbol is given then :public is assumed. Unrecognized symbols raise an error.
def test puts("Hello World!") end methods(:local) #=> ['test']
Pretty prints an exception/error object usefull for helpfull debug messages
Input: The Exception/StandardError object
Output: the pretty printed string
The promise() function is used together with demand() to implement lazy evaluation. It returns a promise to evaluate the provided block at a future time. Evaluation can be demanded and the block‘s result obtained via the demand() function.
Implicit evaluation is also supported: the first message sent to it will demand evaluation, after which that message and any subsequent messages will be forwarded to the result object.
As an aid to circular programming, the block will be passed a promise for its own result when it is evaluated. Be careful not to force that promise during the computation, lest the computation diverge.
Like respond_to? but returns the result of the call if it does respond.
(This is used to be called respond_with_value.)
Set setter methods using a another object.
class X attr_accessor :a, :b def initialize( a, b ) @a,@b = a,b end end obj1 = X.new( 1, 2 ) obj2 = X.new obj2.set_from(obj1) obj2.a #=> 1 obj2.b #=> 2
Assign via setter methods using a hash, associative array or block.
object.set_with( :a => 1, :b => 2 ) object.set_with( :a, 1, :b, 2 ) object.set_with( [:a, 1], [:b, 2] ) object.set_with( *[[:a, 1], [:b, 2]] ) object.set_with{ |s| s.a = 1; s.b = 2 }
These are all the same as doing:
object.a = 1 object.b = 2
The array forms gaurentees order of operation.
This method does not check to make sure the object repsonds to the setter method. For that see populate.
Silences any stream for the duration of the block.
silence_stream(STDOUT) do puts 'This will never be seen' end puts 'But this will'
Access to an object‘s "special" class, otherwise known as it‘s eigenclass or metaclass, etc.
One day these names must be reconciled!
Access to an object‘s "special" class, otherwise known as it‘s eigenclass or metaclass or own, etc.
One day these names must be reconciled!
Returns true is an object is class TrueClass, otherwise false.
true.true? #=> true false.true? #=> false nil.true? #=> false
Like warn produces the current line number as well.
warn_with_line("You have been warned.")
produces
3: Warning: You have been warned.
Note that this method depends on the output of caller.