3

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?

bdx
  • 3,316
  • 4
  • 32
  • 65
  • What do you mean? Are you asking how Ruby decides that `0.15` is a `Float` literal rather than a call of method `15` on `Fixnum` literal `0`? I don't see why it should matter. It seems obvious that `15` isn't a method, but the only way to know for sure is to examine the Ruby compiler. It is much the same question as why `0` on its own isn't a method call. It puzzles me how you think looking at the list returned by `0.methods` would help. – Borodin Jul 29 '13 at 11:18
  • 1
    Yep, that's what I want to know. It doesn't matter for any programming purpose or exercise, but I want to understand if there's something there that might be helpful down the track. I'm just curious, that's all, and I certainly hope curiousity will be encouraged by other programmers who know better than I, not just pushed aside by those who don't know the answer. I'm quite certain that if I tried to understand the ruby compiler that I'd get lost. I've not written a lexer or parser myself, so trying to understand one which is undoubtedly quite complex is above me for now. – bdx Jul 29 '13 at 11:21
  • I looked at `0.methods` because from a layman's point of view, it seems like the place to look when trying to work out what Ruby would do when a `.` follows a literal. I didn't know what I was hoping to find, but I didn't find anything meaningful to me. – bdx Jul 29 '13 at 11:22

3 Answers3

3

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.

Aaron Cronin
  • 2,093
  • 14
  • 13
  • 1
    Excellent spotting... I'm going to have to go over that a few times to get it properly. – bdx Jul 29 '13 at 20:48
0

As Methods in Ruby cannot begin with numbers, it's pretty easy to determine that 6.foo is a Method call and 6.12 is a Float.

You can distinguish both of them by pretty simple regular grammar specs, which is all what a Lexer needs to tokenize the source code.

Community
  • 1
  • 1
nTraum
  • 1,426
  • 11
  • 14
  • That seems quite reasonable. Thanks for the additional link there, that helped clear it up nicely, and also gave me a bit more information about the restrictions and flexibility of method names in Ruby. – bdx Jul 29 '13 at 11:36
  • 3
    This is nothing but a restatement of the question: *“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“* It is (probably perfectly correct) speculation, and not a definitive description of *how* Ruby determines the difference between a float and a method call. No one can provide that without checking the Ruby source. – Borodin Jul 29 '13 at 13:30
  • No, Borodin. It is an expansion on what I thought may be the case, with both a link to a useful answer elsewhere and someone who knows more about lexer's than I adding weight to my assumption. – bdx Jul 29 '13 at 20:46
-3

I don't know for sure, but I think it is safe to assume that the two are distinguished by method names being unable to start with a number.

I don't see that it's an especially interesting or useful thing to know, and I think your curiosity is best directed elsewhere.

Borodin
  • 126,100
  • 9
  • 70
  • 144