Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the maximum length of a method name in ruby?

I'm using RubyMine which informs me that

unsubscribe_from_all_notifications

...is too long for a method name. What's the max length?

like image 253
TLK Avatar asked Oct 28 '11 23:10

TLK


People also ask

How long can a method name be?

Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals. Use specific names for variables, for example "value", "equals", "data", ... are not valid names for any case. Use meaningful names for variables.

How do you name a method in Ruby?

Ruby allows method names and other identifiers to contain such characters.) Method names may contain letters, numbers, an _ (underscore or low line) or a character with the eight bit set. Method names may end with a ! (bang or exclamation mark), a ? (question mark) or = equals sign.

What is the maximum length of a variable name in java?

Rules to Declare a Variable Java keywords cannot be used as variable names. Variable names are case-sensitive. There is no limit on the length of a variable name but by convention, it should be between 4 to 15 chars.


2 Answers

RubyMine lies :-) Or at least does not mean that it is a limitation of Ruby interpreter.

looong_name = "a" * 10000; # => "aaaaaaaaa.....
a_class = Class.new
a_class.__send__(:define_method, looong_name) { :hello }
a_class.new.__send__(looong_name) # => :hello

puts a_class.instance_methods.inspect # you better not run this :-)
like image 151
Daniel Vartanov Avatar answered Sep 18 '22 13:09

Daniel Vartanov


AFAIK there is no limit to the size of a method name in ruby.

Most likely you have some coding style setup in RubyMine which is showing you this warning. If on a mac check RubyMine > Preferences > Code Style > Ruby and see if there is some sort of preference in there.

Just to let you know I use RubyMine as well but I do not see this type of coding style set for me.

like image 22
Moiz Raja Avatar answered Sep 20 '22 13:09

Moiz Raja