Just For Fun: Ternaryish thingie on steroids

Not sure if this will ever be useful, but I thought it would be neat
if i could build a little wrapper class that allowed me to pass in two
objects, then define a conditional lambda and depending on what it
returned, forward requests to one object or the other.

That sounds really complicated, but here is a simple example

a = Switch.new(1,2)
a.condition = lambda { |x| x.options.foo }
a + 1
=> 3

a.options.foo = true
=> true

a + 1
=> 2

a.options.foo = false
=> false

a + 1
=> 3

The neat thing is the objects can be lambdas, which can make things a
bit more complex. See the example below.


I put the code online. It was a two minute hack, so I have no idea if
anyone else might find it fun or interesting, but i did, so I figured
I’d share.


Happy fourth to those celebrating it!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs