# How to alias keywords?

**URL:** <https://www.ruby-forum.com/t/how-to-alias-keywords/180346>\
**Category:** Ruby\
**Created:** [December 18, 2009, 2:51am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346 "2009-12-18T02:51:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 2:51am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/1 "2009-12-18T02:51:22Z")

</div>

It’s easy to alias method names. Is there a way I can alias keywords  
like if or end? I want to localize Ruby code to another human language  
for learning/educational purposes.

Any way I can get ruby or to understand new synonyms for keywords?

---

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 2:57am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/2 "2009-12-18T02:57:41Z")

</div>

El Viernes, 18 de Diciembre de 2009, Sonja Kisa escribiÃ³:

> It’s easy to alias method names. Is there a way I can alias keywords  
> like if or end? I want to localize Ruby code to another human language  
> for learning/educational purposes.
> 
> Any way I can get ruby or to understand new synonyms for keywords?

IMHO this is not possible at all.

---

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 3:41am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/3 "2009-12-18T03:41:49Z")

</div>

you can do it like this:  
def IF(condition)  
if condition  
yield  
end  
end

IF(true){  
puts “yes”  
}

---

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 3:41am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/4 "2009-12-18T03:41:58Z")

</div>

Maybe I need to edit parse.y or something.

---

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 4:21am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/5 "2009-12-18T04:21:47Z")

</div>

From #ruby-lang on Freenode IRC tonight:

[21:44] Sonja:

> <https://github.com/shyouhei/ruby/blob/trunk/parse.y#L10106-10154>

[21:44] \<argv[0]\> right. you either get “if” or you get “somethingelse”,  
but not both  
[21:45] if you want both, you’ll have to declare both  
english and otherlanguage as keywords and then modify the grammar  
productions to accept both  
[21:46] \<argv[0]\> yea, that.  
[21:46] \<argv[0]\> which is not quite as easy  
[21:46] essentially: if expr then? expr =\> (if|fi) expr  
(then|neht)?  
[21:46] just a bit tedious considering how f’d up the yacc  
file is  
[21:47] but start with something that isn’t replicated in C  
like class or module  
[21:47] experiment with that one change  
[21:47] and then expand  
[21:48] should all just be algebraic changes, substitute A|B  
for all A  
[21:50] looks like the 1.9 (trunk) version is very cleaned  
up… keyword\_elsif… very easy to track down all versions then  
[21:51] yacc kinda sucks… but we’re stuck with it  
(_cough_for now_cough_)  
[21:52] my algebraic statement above isn’t quite that easy  
since yacc doesn’t support EBNF productions cleanly… but you’ll get it  
[22:06] \<argv[0]\> Sonja this is about as good as it gets without messing  
with parse.y [insane.rb · GitHub](http://gist.github.com/259256)

---

<div class="post-metadata">

**Author:** ![Sonja\_Elen\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sonja\_Elen\_K](https://www.ruby-forum.com/u/Sonja_Elen_K)\
**Post date:** [December 18, 2009, 4:38am UTC](https://www.ruby-forum.com/t/how-to-alias-keywords/180346/6 "2009-12-18T04:38:19Z")

</div>

On Dec 17, 2009, at 11:12 PM, Sonja Elen K. wrote:

> Maybe I need to edit parse.y or something.

Most definitely. But I don’t see much of a point in it:

1. You will have to distribute the changed version of  
Ruby by yourself. If the users have to run it on their  
own computer, this can be a hassle.

2. The users will have a mismatch between their stuff  
and every piece of ruby code out there, especially  
tutorials and books, basically making self-tutoring  
a pain.

3. You will have to make sure that “normal” Ruby code  
still runs.

I don’t know whether thats worth the hassle if your  
course is not very “special interest”.

Regards,  
Florian
