2

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.

1
  • 3
    You could create such method via define_method('51_bic') { ... } but it also has to be invoked via public_send('51_bic'). It might therefore be more convenient to add a non-numeric prefix, e.g. index_51_bic Commented Aug 13, 2018 at 14:51

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

3 Comments

Well, technically one might start method names with a digit, if the name is explicitly converted to a Symbol: define_method :"51_bic" do puts __callee__ end.
@mudasobwa: I carefully avoided using words "method name" in this answer. :)
@mudasobwa I don't think you need to make it a symbol. Just define_method "51_bic" do ... end is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.