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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -