Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple and non-magical lazy evaluation mechanism for Ruby

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.textile
Octocat-spinner-32 simple_promise.gemspec
README.textile

A lazy evaluation implementation:

lazy = SimplePromise.new { 1 + 1 } lazy + 3
  1. 5
lazy = SimplePromise.new { puts “executing”; 1 + 1 } lazy + 3 (prints “executing”)
  1. 5

A SimplePromise differs from the result of its promised block only in two ways.
First, #inspect shows that it is in fact a promise:

lazy = SimplePromise.new { 1 + 1 } lazy.inspect
  1. #<SimplePromise computation=#<Proc:0×000@/foo.rb:44> result=[promised]>
lazy + 3 lazy.inspect
  1. #<SimplePromise computation=#<Proc:0×000@/foo.rb:44> result=2>

Second, literal truthiness evaluations will always be positive:

lazy_true = SimplePromise.new { true } lazy_false = SimplePromise.new { false } lazy_true ? “positve truthiness” : “negative truthiness”
  1. “positive truthiness”
lazy_false ? “positve truthiness” : “negative truthiness”
  1. “positve truthiness”
Something went wrong with that request. Please try again.