# Where does the syntactic sugar get defined?

**URL:** <https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767>\
**Category:** Ruby\
**Created:** [May 22, 2007, 9:49am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767 "2007-05-22T09:49:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 9:49am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/1 "2007-05-22T09:49:01Z")

</div>

Where exactly does the syntactic sugar get defined in Ruby? Is it  
built-in into the Ruby core syntax? It seems to me like a preprocessing  
thing…

Can I define my own syntactic sugar for Ruby?

Thx,  
Keybern

---

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 9:55am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/2 "2007-05-22T09:55:16Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf  
Of  
Key Bern  
Sent: Tuesday, May 22, 2007 10:49 AM

> Where exactly does the syntactic sugar get defined in Ruby? Is it  
> built-in into the Ruby core syntax? It seems to me like a preprocessing  
> thing…
> 
> Can I define my own syntactic sugar for Ruby?

Definition of what is “core” and what is “sugar” is more theoretical  
than  
practical. Different parts of language can be seen as one or another  
depending on point of view.

If you would narrow your question (what constructs are you talking  
about),  
you can receive more concrete answer.

V.

---

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 10:02am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/3 "2007-05-22T10:02:41Z")

</div>

core syntax vs syntactic sugar

* * *

4.+(12) 4 + 12  
array. array[13]  
persoon1.==(persoon2) persoon1 == persoon2  
if not unless  
Proc.new lambda

Aren’t the things from to right column preprocessed into the left  
column?

---

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 10:12am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/4 "2007-05-22T10:12:20Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf  
Of  
Key Bern  
Sent: Tuesday, May 22, 2007 11:03 AM

> column?  
> My guess is NO.

But this makes your point more clear. If you want to define new “infix  
operator” (like “a mycoolnewoperator b”), it’s impossible.

And the most natural for Ruby way of defining new control structures is  
usage of blocks. Suppose, the language has no “unless” keyword. We CAN’T  
define something to look like:

unless A  
B  
else  
C  
end

All we CAN is define something to look like:

unless A do  
B  
end.else do  
C  
end

this may be enough or not, depends of your requirements.

hope this helps.

V.

---

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/5 "2007-05-22T18:40:54Z")

</div>

On Tue, May 22, 2007 at 05:03:04PM +0900, Key Bern wrote:

> core syntax vs syntactic sugar
> 
> * * *
> 
> 4.+(12) 4 + 12  
> Yes  
> array. array[13]  
> Yes  
> persoon1.==(persoon2) persoon1 == persoon2  
> Yes  
> if not unless  
> Yes (If you don’t believe me, check parse tree’s AST for an unless cond  
> then  
> …)  
> Proc.new lambda  
> No

One more:  
!(a == b) a != b

Of course these things only apply for MRI, JRuby, etc. may do different  
transformations, I do not know.

---

<div class="post-metadata">

**Author:** ![keybern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8edcca/32.png) [@keybern](https://www.ruby-forum.com/u/keybern)\
**Post date:** [May 22, 2007, 10:32am UTC](https://www.ruby-forum.com/t/where-does-the-syntactic-sugar-get-defined/104767/6 "2007-05-22T10:32:22Z")

</div>

On 22 May 2007, at 17:03, Key Bern wrote:

> core syntax vs syntactic sugar
> 
> * * *
> 
> 4.+(12) 4 + 12  
> array. array[13]  
> persoon1.==(persoon2) persoon1 == persoon2  
> if not unless  
> Proc.new lambda
> 
> Aren’t the things from to right column preprocessed into the left  
> column?

‘unless’ is a full ‘reserved word’ in the parser just like ‘if’ and  
‘not’ so I don’t think it’s really correct to say it is being  
preprocessed into them. Likewise operators such as ‘+’ and ‘==’ are  
defined in parse.y. lambda and Proc.new are not identical. The  
behaviour of lambda is defined in eval.c.

Alex G.

Bioinformatics Center  
Kyoto University
