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.
… 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”