Thead-safe instance-level memoization.
class MemoExample attr_accessor :a def m safe_memo{ @a } end end ex = MemoExample.new ex.a = 10 ex.m #=> 10 ex.a = 20 ex.m #=> 10
# File lib/facets/standard/facets/thread.rb, line 36 def safe_memo(*args, &block) if args.empty? args = block.binding.eval('[self, __method__]') end $MEMO_MUTEX.synchronize do if $MEMO.key?(args) $MEMO[args] else $MEMO[args] = block.call end end end
As with to_yaml but removes the header line (i.e. '---') to create a "YAML fragment".
CREDT: Thomas Sawyer
# File lib/facets/standard/facets/yaml.rb, line 21 def to_yamlfrag y = to_yaml y.sub!(/---\ */, '') y end
CREDIT: Zucker
# File lib/facets/standard/facets/thread.rb, line 79 def sandbox(rescueblock_or_default=nil) #:yield: Thread.start do $SAFE = 4 yield end.value rescue SecurityError => e if !rescueblock_or_default.nil? if rescueblock_or_default.is_a? Proc rescueblock_or_default.call e else rescueblock_or_default end else raise e end end
Generated with the Darkfish Rdoc Generator 2.