Forum: Ruby ... building my own end-fix statements.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 Jeff Wood (Guest)
on 2006-06-09 22:47
(Received via mailing list)
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.
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-06-09 23:39
(Received via mailing list)
On Jun 9, 2006, at 1:44 PM, Jeff Wood 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
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-06-09 23:48
(Received via mailing list)
On Jun 9, 2006, at 2:36 PM, Ezra Zygmuntowicz wrote:

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


	Actually I totally misread your question Jeff. Sorry ;)

-Ezra
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 Jeff Wood (Guest)
on 2006-06-09 23:52
(Received via mailing list)
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.
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-06-10 00:08
(Received via mailing list)
On Jun 9, 2006, at 2:49 PM, Jeff Wood 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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-06-10 00:08
(Received via mailing list)
On Jun 9, 2006, at 11:49 PM, Jeff Wood 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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-06-11 21:28
(Received via mailing list)
On Jun 9, 2006, at 4:44 PM, Jeff Wood 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!
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 Jeff Wood (Guest)
on 2006-06-12 07:33
(Received via mailing list)
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.
This topic is locked and can not be replied to.