until and while are similar enough, but not 'when' ...

I see that you are saying I could get away with two statements ..

sleep 0.5 until a.ready?
a.doSomething

... but I'm still looking for that one layer deeper.

a.doSomething when a.ready?

... anyways ... thanks for the info ... hadn't seen until

--jw.

On 6/9/06, Ezra Zygmuntowicz <ezmobius / gmail.com> wrote:
>
> 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
> => #<X:0x337c6c @foo=0>
> irb(main):016:0> x.increment until x.ready?
> => nil
> irb(main):017:0> puts x.foo
> 50
>
>
> Cheers-
> -Ezra
>
>
>