Struct_cond
alias ¤ puts
module Enumerable
def €(*ar, &bl) = ( each(*ar, &bl) )
end
Cond = Struct.new(:t, :f) {
def ĸ(cond) = (cond ? t.call : f.call)
}
c = Cond.new(
-> { ¤ 'this is even' },
-> { ¤ 'this is odd' }
)
# now you can use:
(2..12).€ { c.ĸ(_1 % 2 == 0) }
It’s not that I wanna do code-golfing.. But I try to find my personal style of coding. There’s a lot of repetition in code that I don’t like.
In this example I was aiming for a way to separate the condition test from the then - and else blocks. If I can keep them apart.. it means I can modify and adjust them without having to touch the logic part of the code.
I started with a class. Then thought of refining TrueClass and FalseClass. It was then the Struct came to mind.
You can have an array of Structs and send the same results to each instance of the Cond.. it sort of opens doors.