Denny Mueller
Denny Mueller

Reputation: 3615

Ruby and integer in method name

I have to name methods based on the index number of some external documentation:

def 51_bic
end

This is wrong, as shown by the color with the syntax highlighting. And also the code fails with trailing `_' in number (SyntaxError).

Using bic_51 works just fine. But why is that? What's the nature of the fact that I can't use integer + underscore + string? My understanding is that everything after a def is just a method name as a string.

Upvotes: 2

Views: 772

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230316

Identifiers can have numbers in them, but can't start with a number. This is how it is in most programming languages (that I heard of).

What's the nature of the problem that I can't use integer + underscore + string?

Because if you allow identifiers to start with a number, you must then mandate that they contain a letter after (to differentiate them from numbers). Now, food for thought. Imagine you can start identifiers with numbers. Which of these are method calls, local variables and which are number literals?

0xa0 + 0b10_100 + 3_456

Upvotes: 5

Related Questions