# ... building my own end-fix statements

**URL:** <https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963>\
**Category:** Ruby\
**Created:** [June 9, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963 "2006-06-09T22:47:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/1 "2006-06-09T22:47:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/2 "2006-06-09T23:39:02Z")

</div>

On Jun 9, 2006, at 1:44 PM, Jeff W. wrote:

> which would call x.ready? repeatedly until it returned a true then  
> but I’d rather not.  
> 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

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/3 "2006-06-09T23:52:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 10, 2006, 12:08am UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/4 "2006-06-10T00:08:13Z")

</div>

On Jun 9, 2006, at 2:49 PM, Jeff W. wrote:

> a.doSomething when a.ready?

So maybe something more like this:

class X

def initialize  
@foo = 0  
end  
def ready?  
@foo += 1  
@foo = 50  
end

end

def \_when(condition, &block)  
until condition.ready?  
sleep 0.1  
end  
block.call  
end

x = X.new

\_when(x) { puts “It’s Ready!” }

#=\> It’s Ready!

```
when is already a keyword so you will have to come up with a better

```

name but this kind of does what you want.

-Ezra

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 9, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/5 "2006-06-09T23:48:41Z")

</div>

On Jun 9, 2006, at 2:36 PM, Ezra Z. wrote:

> > 
> 
> irb(main):011:2\> @foo == 50
> 
> Cheers-  
> -Ezra

```
Actually I totally misread your question Jeff. Sorry ;)

```

-Ezra

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 10, 2006, 12:08am UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/6 "2006-06-10T00:08:24Z")

</div>

On Jun 9, 2006, at 11:49 PM, Jeff W. wrote:

> … anyways … thanks for the info … hadn’t seen until

Maybe observer.rb will fit your needs? Can you give a more specific  
example of what you would like to do?

require ‘observer’

class HomelandSecurityAdvisorySystem  
def initialize  
@level = “LOW”  
end  
include Observable  
attr\_reader :level  
def level=(new\_level)  
changed()  
notify\_observers(new\_level)  
@level = new\_level  
end  
end

class LevelNotifier  
def update(level)  
case level  
when “LOW”  
puts “Everything is ok.”  
when “GUARDED”  
puts “Uh oh”  
when “ELEVATED”  
puts “Watch FOX for the most up-to-date info”  
when “HIGH”  
puts “We’re not fear-mongering.”  
when “SEVERE”  
puts “You’re dead”  
end  
end  
end

x = HomelandSecurityAdvisorySystem.new  
x.add\_observer(LevelNotifier.new)  
x.level = “ELEVATED”  
x.level = “SEVERE”

– Daniel

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 12, 2006, 7:33am UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/7 "2006-06-12T07:33:12Z")

</div>

Yeah, that’s a lot closer to what I was thinking.

thanks… Had forgotten about doing a . after the block … really  
darn’d close.

–jw.

---

<div class="post-metadata">

**Author:** ![jwigal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jwigal](https://www.ruby-forum.com/u/jwigal)\
**Post date:** [June 11, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/building-my-own-end-fix-statements/62963/8 "2006-06-11T21:28:25Z")

</div>

On Jun 9, 2006, at 4:44 PM, Jeff W. wrote:

> which would call x.ready? repeatedly until it returned a true then  
> but I’d rather not.  
> Any ideas ???
> 
> –jw.

I called it “after” since when is a keyword:

% cat after.rb  
class After  
def initialize(&block)  
@test = block  
end

def do  
until @test.call  
sleep 0.5  
end

```
 yield

```

end  
end

module Kernel  
private  
def after(&block)  
After.new(&block)  
end  
end

class X  
def initialize  
@x = 0  
end

def ready?  
if @x == 10  
true  
else  
@x += 1  
false  
end  
end  
end

x = X.new  
after { x.ready? }.do {  
puts “x is finally ready!”  
}

% ruby after.rb  
x is finally ready!
