ruby lexer/parser

A

Artur Merke

Just a short question concerning the ruby lexer/parser

irb(main):001:0> a=1
=> 1
irb(main):002:0> a.to_f * 10.0 #space before and after '*'
=> 10.0
irb(main):003:0> a.to_f*10.0 #no space before and after '*'
=> 10.0

irb(main):005:0* a.to_f *10.0 #space before '*' and no space after
'*'
ArgumentError: wrong number of arguments (1 for 0)
from (irb):5:in `to_f'
from (irb):5

so why does ruby think *10.0 would be an argument to .to_f?

In my opinion the ruby lexer sould recognize '*' as an operator, no
spacing should
have any influence on this behavior ...


Artur
 
F

Farrel Lifson

Just a short question concerning the ruby lexer/parser

irb(main):001:0> a=1
=> 1
irb(main):002:0> a.to_f * 10.0 #space before and after '*'
=> 10.0
irb(main):003:0> a.to_f*10.0 #no space before and after '*'
=> 10.0

irb(main):005:0* a.to_f *10.0 #space before '*' and no space after
'*'
ArgumentError: wrong number of arguments (1 for 0)
from (irb):5:in `to_f'
from (irb):5

so why does ruby think *10.0 would be an argument to .to_f?

In my opinion the ruby lexer sould recognize '*' as an operator, no
spacing should
have any influence on this behavior ...


Artur

* can have dual meanings. It can be a method call on an object (in
your first two examples) or it can act as the 'splat' operator when it
directly preceeds an object.

Farrel
 
A

Artur Merke

Farrel said:
* can have dual meanings. It can be a method call on an object (in
your first two examples) or it can act as the 'splat' operator when it
directly preceeds an object.

Farrel

yes, you are right. I was just suprised, that it only is a problem
after a method invocation (as 'a *10' works fine),
but this behavior seems to be enforced by ruby's slack handling of
parentheses ...

Artur
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top