What are the rules and conventions to be followed in Ruby for naming a method?

Questions by sripri   answers by sripri

Showing Answers 1 - 9 of 9 Answers

gikie

  • Dec 14th, 2009
 

By convention method names begin with lower case letter (they can begin with the upper case letter but that makes it look like a constant).

When a method name has more than one word then the usual convention is to use underscore (e.g like_this)

Method names may end with a question mark, equasl sign and sign of excalmation.

The first convention is the method which ends with question mark returns a value that answers the question posed by the method invocation.

The second convention is any method which ends with sign of excalmation must be used with caution. Often the mehods which end with excalmation mark are mutators.

  Was this answer useful?  Yes

hari

  • Feb 3rd, 2012
 

method should start with small letter.If the method name contain two words use underscore to separate the words
ex:get_name

  Was this answer useful?  Yes

Christopher Kleeschulte

  • Feb 13th, 2012
 

According to Martin Fowler, http://martinfowler.com/bliki/ClassInstanceVariable.html, A Class Instance Variable is a class-level variable that DOES NOT get inherited in any of the subclasses like a class variable would. Class Instance Variables are commonly used to hold references to their own instances. Using a class variable in this case possibly wouldnt make sense because all the subclasses would also get a reference to an instance of their superclass. This may not be what the programmer intended. Inserting the code below allows for class instance variables.

Code
  1. class << self; attr_accessor :instances; end

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions