Tuesday 31 May 2016

Ruby class methods



Hi Readers,

In this post you are going to learn how to define class methods and how to call those methods.

If you want to define a class method there are two ways.

1. Inside the class by using the self keyword
2. Inside the class by using "class << self"

Refer the below example to get the Info.

  class Person 
      def self.class_method 
        puts "class method invoked" 
      end 
     
      class << self 
        def class_method2; puts "class_method2"; end 
        def class_method3; puts "class_method3"; end 
      end 
    end 
     
    class << Person 
      def class_method4; puts "class_method4"; end 
    end 
     
    # Invocation of class method 
    Person.class_method


Just practice this, i will come back with new concept.
To get more updates subscribe and follow my blog.
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...