# Overwriting "if"

**URL:** <https://www.ruby-forum.com/t/overwriting-if/109820>\
**Category:** Ruby\
**Created:** [July 19, 2007, 11:14am UTC](https://www.ruby-forum.com/t/overwriting-if/109820 "2007-07-19T11:14:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:14am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/1 "2007-07-19T11:14:12Z")

</div>

I want to overwrite if,  
but it doesn’t seem to work.

module Kernel  
def if(arg)  
yield  
end  
end

def if(arg)  
yield  
end

doesn’t seem to work.  
I know I could define my own if,  
but it seems like ruby, being so dynamic, should be capable of this.

Any amusing suggestions on how to implement this will be much welcomed.

MatthewRudy

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:29am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/2 "2007-07-19T11:29:05Z")

</div>

Matthew R. wrote:

> I want to overwrite if,  
> Any amusing suggestions on how to implement this will be much welcomed.
> 
> MatthewRudy

Be like Smalltalk!

irb\> class Object  
irb\> def if  
irb\> if self  
irb\> yield  
irb\> end  
irb\> end  
irb\> end  
=\> nil

irb\> (4==4).if { p :true }  
:true  
=\> nil  
irb\> (4==6).if { p :true }  
=\> nil

irb\> class Object  
irb\> def if(blocks)  
irb\> if self  
irb\> blocks[:then].call  
irb\> else  
irb\* blocks[:else].call  
irb\> end  
irb\> end  
irb\> end  
=\> nil

irb\> (4==4).if :then =\> lambda { p :true }, :else =\> lambda { p :false }  
:true  
=\> nil  
irb\> (4==6).if :then =\> lambda { p :true }, :else =\> lambda { p :false }  
:false  
=\> nil  
irb\>

Be like Arc!!

irb\> alias fn lambda  
=\> nil  
irb\> (4==6).if :then =\> fn { p :true }, :else =\> fn { p :false }  
:false  
=\> nil

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:34am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/3 "2007-07-19T11:34:03Z")

</div>

that’s cool,  
but can we not redefine the default “if”  
so that the following code will work

if false  
return “false is true”  
end

without resorting to calling a method on a class / instance / module  
explicitly

I’m thinking that intead we can overwrite the evaluation of False and  
NilClass  
?

Daniel L. wrote:

> Be like Smalltalk!

> irb\> (4==4).if { p :true }  
> :true  
> =\> nil  
> irb\> (4==6).if { p :true }  
> =\> nil

> irb\> (4==4).if :then =\> lambda { p :true }, :else =\> lambda { p :false }  
> :true  
> =\> nil  
> irb\> (4==6).if :then =\> lambda { p :true }, :else =\> lambda { p :false }  
> :false  
> =\> nil  
> irb\>

> irb\> (4==6).if :then =\> fn { p :true }, :else =\> fn { p :false }  
> :false  
> =\> nil

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:46am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/4 "2007-07-19T11:46:04Z")

</div>

Matthew R. wrote:

> that’s cool,  
> but can we not redefine the default “if”

Don’t think so: ‘if’ is a keyword. You’ll never get it to look for a  
method called ‘if’ without rewriting the interpreter.

> if false  
> return “false is true”  
> end

Is there a reason for all this or are you just amusing yourself? 🙂

Dan

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:49am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/5 "2007-07-19T11:49:48Z")

</div>

just for fun  
🙂

if only keywords could be overwritten.  
I guess I could just rebuild the parser.

anyway,  
thanks for your suggestions,  
back to work

Mj

Daniel L. wrote:

> Matthew R. wrote:
> 
> > that’s cool,  
> > but can we not redefine the default “if”
> 
> Don’t think so: ‘if’ is a keyword. You’ll never get it to look for a  
> method called ‘if’ without rewriting the interpreter.
> 
> > if false  
> > return “false is true”  
> > end
> 
> Is there a reason for all this or are you just amusing yourself? 🙂
> 
> Dan

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [July 19, 2007, 11:51am UTC](https://www.ruby-forum.com/t/overwriting-if/109820/6 "2007-07-19T11:51:10Z")

</div>

> yield  
> end
> 
> doesn’t seem to work.  
> I know I could define my own if,  
> but it seems like ruby, being so dynamic, should be capable of this.

Of course this is something we really need in Ruby 😉

This reminds me on the following, valid (and at its time popular),  
PL/1 code:

IF IF=THEN  
THEN THEN=ELSE  
ELSE ELSE=IF;

Ronald
