From: Ezra Zygmuntowicz Date: 2006-06-10T06:36:55+09:00 Subject: Re: ... building my own end-fix statements. On Jun 9, 2006, at 1:44 PM, Jeff Wood wrote: > I believe it's possible to define my own end-fix qualifiers like "if" > and "unless" ... > > Can anybody provide code examples for defining such a construct in > Ruby? > > For instance : > > x.doSomething() when x.ready? > > which would call x.ready? repeatedly until it returned a true then > would call x.doSomething() > > ... I know I could do > > while( not x.ready? ) > sleep 0.5 > wend > > x.doSomething() > > but I'd rather not. > > even a > > when( x.ready? ) { |result| x.doSomething() } would be fine I guess > ... but wouldn't work because x.ready? would be precalculated before > when gets to it... so it wouldn't ever change... I basically need to > be able to pass two blocks if I were to do this ... > > when( isReadyProc, bodyProc ) ... or something like that. > > Any ideas ??? > > --jw. Hey Jeff- You can use until for this. Here is a contrived example: irb(main):001:0> class X irb(main):002:1> def initialize irb(main):003:2> @foo = 0 irb(main):004:2> end irb(main):005:1> attr_reader :foo irb(main):006:1> def increment irb(main):007:2> @foo += 1 irb(main):008:2> end irb(main):009:1> irb(main):010:1* def ready? irb(main):011:2> @foo == 50 irb(main):012:2> end irb(main):013:1> end => nil irb(main):014:0> irb(main):015:0* x = X.new => # irb(main):016:0> x.increment until x.ready? => nil irb(main):017:0> puts x.foo 50 Cheers- -Ezra