Module: RubyIs
- Defined in:
- lib/ruby_is.rb,
lib/ruby_is/version.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
- .block(&b) ⇒ Object
- .cache ⇒ Object
- .eval_memoized(f, args, proc) ⇒ Object
- .eval_time_cached(f, caching_time, args, proc) ⇒ Object
- .proc_or_block(args, block) ⇒ Object
- .with_memo(f, caching_time = nil) ⇒ Object
Class Method Details
.block(&b) ⇒ Object
14 |
# File 'lib/ruby_is.rb', line 14 def block(&b); b; end |
.cache ⇒ Object
16 |
# File 'lib/ruby_is.rb', line 16 def cache; @@cache ||= {}; end |
.eval_memoized(f, args, proc) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_is.rb', line 18 def eval_memoized(f, args, proc) cache = RubyIs.cache cache_key = [f, args, proc] if cache.has_key?(cache_key) cache[cache_key] else cache[cache_key] = f.call(*args, &proc) end end |
.eval_time_cached(f, caching_time, args, proc) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby_is.rb', line 28 def eval_time_cached(f, caching_time, args, proc) cache = RubyIs.cache cache_key = [f, args, proc] if cache.has_key?(cache_key) last_call_time, value = cache[cache_key] return value if (Time.now - last_call_time) < caching_time end value = f.call(*args, &proc) cache[cache_key] = [Time.now, value] value end |
.proc_or_block(args, block) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/ruby_is.rb', line 5 def proc_or_block(args, block) case when args.empty? && block then block when (args.count == 1) && block.nil? then args.first else raise "Incorrect syntax for `is` statement" end end |
.with_memo(f, caching_time = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_is.rb', line 40 def with_memo(f, caching_time=nil) ->(*args, &proc){ if caching_time RubyIs.eval_time_cached(f, caching_time, args, proc) else RubyIs.eval_memoized(f, args, proc) end } end |