Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Created January 18, 2013 11:43
Show Gist options
  • Save NigelThorne/4564112 to your computer and use it in GitHub Desktop.
Save NigelThorne/4564112 to your computer and use it in GitHub Desktop.
Functional While look in ruby
class While
def initialize(&src_fn)
@src_fn = src_fn
end
def do
x = @src_fn.call
while(x)
yield(x)
x = @src_fn.call
end
end
end
# While.new{@a = 1 + (@a || 0); @a <= 100 ? @a : nil}.do{|x| puts x}
# counts up to 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment