# Just For Fun: Ternaryish thingie on steroids

**URL:** <https://www.ruby-forum.com/t/just-for-fun-ternaryish-thingie-on-steroids/97081>\
**Category:** Ruby\
**Created:** [July 4, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/just-for-fun-ternaryish-thingie-on-steroids/97081 "2006-07-04T20:13:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![naething](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@naething](https://www.ruby-forum.com/u/naething)\
**Post date:** [July 4, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/just-for-fun-ternaryish-thingie-on-steroids/97081/1 "2006-07-04T20:13:37Z")

</div>

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.

[http://pastie.caboo.se/3361](http://pastie.caboo.se/3361)

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.

[http://rubyurl.com/dHd](http://rubyurl.com/dHd)

Happy fourth to those celebrating it!
