Where does the syntactic sugar get defined?

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

From: [email protected] [mailto:[email protected]] 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.

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?

From: [email protected] [mailto:[email protected]] 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.

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.

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

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