Skip to content

Instantly share code, notes, and snippets.

@paneq
Created February 6, 2012 13:39
Show Gist options
  • Save paneq/1752086 to your computer and use it in GitHub Desktop.
Save paneq/1752086 to your computer and use it in GitHub Desktop.
fibers and ||= behaviour
ruby-1.9.2-p180-fastrequire :001 > f = Fiber.new do
ruby-1.9.2-p180-fastrequire :002 > @something ||= begin
ruby-1.9.2-p180-fastrequire :003 > Fiber.yield # external query
ruby-1.9.2-p180-fastrequire :004?> 2
ruby-1.9.2-p180-fastrequire :005?> end
ruby-1.9.2-p180-fastrequire :006?> end
=> #<Fiber:0x000000028d45a8>
ruby-1.9.2-p180-fastrequire :007 > f.resume
=> nil
ruby-1.9.2-p180-fastrequire :008 > puts @something.inspect
nil
=> nil
ruby-1.9.2-p180-fastrequire :009 > instance_variables
=> [:@prompt]
ruby-1.9.2-p180-fastrequire :010 > f.resume
=> 2
ruby-1.9.2-p180-fastrequire :011 >
ruby-1.9.2-p180-fastrequire :012 > instance_variables
=> [:@prompt, :@something]
ruby-1.9.2-p180-fastrequire :013 > puts @something.inspect
2
=> nil
ruby-1.9.2-p180-fastrequire :014 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment