Since Ruby is very dynamic, methods added to the ancestors of BasicObject after BasicObject is defined will show up in the list of available BasicObject methods. We handle this by defining hooks in Object, Kernel and Module that will hide any defined.
ASSOCIATIONS | = | Hash.new{ |h,k,v| h[k]=[] } |
Define an association with self.
[ show source ]
# File lib/facets/more/association.rb, line 110 def >>(to) ASSOCIATIONS[self] << to Association.new(self, to) end
[ show source ]
# File lib/facets/more/association.rb, line 115 def associations ASSOCIATIONS[self] end
[ show source ]
# File lib/facets/more/bitmask.rb, line 66 def bitmask(bit) 1 << bit end
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.
[ show source ]
# File lib/facets/more/lazy.rb, line 219 def demand( promise ) if promise.respond_to? :__result__ promise.__result__ else # not really a promise promise end end
Schedules a computation to be run asynchronously in a background thread and returns a promise for its result. An attempt to demand the result of the promise will block until the computation finishes.
As with Kernel.promise, this passes the block a promise for its own result. Use wisely.
[ show source ]
# File lib/facets/more/lazy.rb, line 234 def future( &computation ) #:yields: result Lazy::Future.new &computation end
Outputs objs to STDOUT as JSON strings in the shortest form, that is in one line.
[ show source ]
# File lib/facets/more/json.rb, line 644 def j(*objs) objs.each do |obj| puts JSON::unparse(obj) end nil end
Ouputs objs to STDOUT as JSON strings in a pretty format, with indentation and over many lines.
[ show source ]
# File lib/facets/more/json.rb, line 653 def jj(*objs) objs.each do |obj| puts JSON::pretty_unparse(obj) end nil end
[ show source ]
# File lib/facets/more/nackclass.rb, line 104 def nack(err=nil, *data, &ctrl) NackClass.new(err, *data, &ctrl) end
[ show source ]
# File lib/facets/more/nullclass.rb, line 46 def null NullClass.new end
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.
[ show source ]
# File lib/facets/more/lazy.rb, line 208 def promise( &computation ) #:yields: result Lazy::Promise.new &computation end
Shortcut reference constructor.
[ show source ]
# File lib/facets/more/reference.rb, line 65 def ref(x) Reference.new(x) end
Takes a block and returns the total time it took to execute.
[ show source ]
# File lib/facets/more/timer.rb, line 235 def timed yield( timer = Timer.new.start ) return timer.total_time end