Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the ruby interpreter tell when you're calling a method on an integer or when you're setting a float?

Tags:

ruby

I had a look through the 0.methods output in irb and couldn't see what path the ruby interpreter would take when it was passed 0.15 as opposed to 0.to_s

I've tried reading up on how ruby determines the difference between a floating point number being defined and a method being called on an integer but I haven't come to any conclusions.

The best guess I have is that because Ruby doesn't allow for a digit to lead a method name, it simply checks whether the character following the . is numeric or alphabetical.

I don't like guessing though, assumptions can lead to misunderstandings. Can someone clear this up for me?

like image 738
bdx Avatar asked Dec 20 '25 18:12

bdx


1 Answers

How well can you read Yacc files? (Rhetorical question)

https://github.com/ruby/ruby/blob/trunk/parse.y#L7380 I believe this is where the Ruby parser handles floating point tokenisation.

Disclaimer: parse.y hurts my head.

like image 68
Aaron Cronin Avatar answered Dec 22 '25 10:12

Aaron Cronin