ruby - How do I write my own loop_until? -
i'm practicing ruby meta-programming , trying write own loop method handle of ugliness in listening socket, give programmer chance specify loop break condition , block of things after each io.select/sleep cycle.
what want able write this:
x = 1 while_listening_until( x == 0 ) x = rand(10) puts x end
what i've been able make work is:
def while_listening_until( params, &block ) break_cond = params[ :condition ] || "false" loop { #other listening things happening here yield break if params[:binding].eval( break_cond ) } end x = 1 while_listening_until( :condition => "x==0", :binding => binding() ) x = rand(10) puts x end
so, how make eval
, binding ugliness go away?
this lambdas handy:
def while_listening_until( condition, &block ) loop { #other listening things happening here yield break if condition.call } end x = 1 while_listening(lambda{ x == 0 }) x = rand(10) puts x end
Comments
Post a Comment