3 ms·
For the multi-block conditional, there is this idiom: def assuming cond Thread.current[:last_cond] = cond Thread.current[:last_value] = (yield if con
by extension 17y ago
For the multi-block conditional, there is this idiom:
def assuming cond
Thread.current[:last_cond] = cond
Thread.current[:last_value] = (yield if cond)
end
def otherwise
if Thread.current[:last_cond]
Thread.current[:last_value]
else
yield
end
end
def alternately_if cond, &b
otherwise { assuming cond, &b }
end
def how_big x
assuming x < 3 do
"small"
end
alternately_if x < 7 do
"medium"
end
otherwise do
"large"
end
end
10.times {|z| puts how_big z }
Seems a bit sketchy though. It could be made cleaner by making them methods of an object that holds the state, but I guess that is starting to defeat the purpose.