Tuesday 31 May 2016

Ruby Methods



 Hi Readers,
   Today we will know about the Ruby methods and parenthesis:
  1. When invoking a method argument parenthesis are optional
  2. Methods always have a receiver. The implicit receiver is self.
  3. Methods are identified by their name only. No overloading on argument signatures.
  4. There are class methods and instance methods
  5. Methods can be public, protected, or private
  6. The last evaluated expression in a method is the return value
  7. Arguments can have default values: def my_method(a, b = {})

Methods and Parenthesis:


    def square(number) 
      number * number 
    end 
     
    square(2+2)*2 # => square(4)*2 = 32 
    square (2+2)*2 # => square(4*2) = 64

Method Visibility:

    class MyClass   
      def method1 # Methods are public by default 
      end   
      # Protected methods can be invoked by any instance of the same class or a 
      # subclass of MyClass 
      protected   
      def method2 
      end 
      # Private methods can only be invoked within an instance of MyClass or a subclass 
      # of MyClass. The receiver of a private method is always self. 
      private   
      def method3 
      end 
    end  


 Hope you will understood, in next session i will come up with new concept.

Do you like my posts follow this blog by clicking the Follow button About me in right side menu.

Thank you.

No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...