where does the syntactic sugar get defined?

K

Key Bern

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
 
K

Key Bern

core syntax vs syntactic sugar
----------- ---------------
4.+(12) 4 + 12
array.[ ](13) 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?
 
A

Alex Gutteridge

core syntax vs syntactic sugar
----------- ---------------
4.+(12) 4 + 12
array.[ ](13) 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 Gutteridge

Bioinformatics Center
Kyoto University
 
L

Logan Capaldo

core syntax vs syntactic sugar
----------- ---------------
4.+(12) 4 + 12 Yes
array.[ ](13) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top