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

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.

Variable/Method Ambiguity Gotcha


In the below mentioned program we are defining a variable called paid, while initializing the class object we are creating instance variable default value is false.

We have make_payment instance method defined here and inside that method we assigning value to the paid variable to true, just notice that in this method we are not overriding/replacing the actual instance variable so after calling the make_payment actual instance variable value won't change, if you try to print the paid value of instance will be false.

  1.  
  2. class Person  
  3.   attr_accessor :paid  
  4.     
  5.   def initialize  
  6.     @paid = false  
  7.   end  
  8.     
  9.   def make_payment  
  10.     puts "making payment..."  
  11.     paid = true  
  12.   end  
  13. end  
  14.   
  15. person = Person.new  
  16. person.make_payment  
  17. puts "paid=#{person.paid}" 
  18.  


Try to execute the above code and practice it by changing the variables.

Hope you will understand, i will get back to you with new concept.

Ruby Attr_accessor


You can declare the attributes for the ruby class using the keyword 'attr_accessor'.

While initializing object you can assign the value to it. Check the following example and practice it.

  1. class Person  
  2.   attr_accessor :name  
  3.     
  4.   def initialize(name)  
  5.     self.name = name  
  6.   end  
  7. end  
  8.   
  9. person = Person.new("Andreas")  
  10. puts person.name  
  11. person.name = "David"  
  12. puts person.name  

Hope you will practice it, i will get back to you with new concept.

Monday 30 May 2016

Ruby creator(Yukihiro Matsumoto) talk in Rubyconf India

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 march 2016. This was great opportunity to meet lot of rubiest.

I met lot of people who are started their career freshly and they want to share their opinions. Actually me and my friend went there 18 march afternoon, that was actual check in time they mentioned in the website.

That was nice hosting provided by le meridian hotel management. Food and accommodation was good. 18 march my friend me were having dinner that is around 9:00 P.m i think, My friend looking at one person standing in the dining hall and everyone wishing him to welcome India.

Finally my friend telling me that "Hey, he is Ruby creator 'Yukihiro Matsumoto'". Everyone calling him Matz. He is very simple and looking so tiered. Me and my friend went there to welcome him and take a photograph with him. Finally me and my friend got the photo of him.

Me and Yukihiro Matsumoto
Next day 19 march actual conference started, First talk was given by Matz, that was awesome, it went like funny and interesting. Matz was promising that in the next version of the ruby is going be more powerful, mainly his team focusing on the performance part there is no new features are introducing.

I observe that lot of people came to conf but most of them from Bengaluru, that to there are many startups of-course i am also one of them.

Some of the talks were boring and few of them are interesting. i am writing this things because i want share with you guys.




Getter and Setter Methods



In my previous posts you learn about the class and inheritance
Now we are going to get the information regarding the setter and getter methods of a ruby class

    class Person  
      def initialize(name)  
        self.name = name  
      end  
      def name  
        @name  
      end  
      def name=(name)  
        @name = name  
      end  
    end  
      
    person = Person.new("Andreas")  
    puts person.name  
    person.name = "David"  
    puts person.name  


In the above person class name definition is the one contains setter and getter methods.

In next post we will learn what attr_accessor.

Class Inheritance


Continue with previous post

require 'person_class'  
  
class Programmer < Person  
  def initialize(name, favorite_ide)  
    super(name)  
    @favorite_ide = favorite_ide  
  end  
  
  # We are overriding say_hi in Person  
  def say_hi  
    super  
    puts "Favorite IDE is #{@favorite_ide}"  
  end  
end  
  
peter = Programmer.new("Peter", "TextMate")  
peter.say_hi


In the above mentioned class we are creating a class called Programmer and inheriting the Person class which is created in previous post.

In the Programmer class we are overriding the "say_hi" definition which is all ready defined in the Person class.

Practice this code and give me the output result as comment.

<<Back

In next you are going to setter and getter methods in ruby. 

Defining a Class and Instantiating an Object


Ruby Class:

class Person  
  def initialize(name)  
    @name = name  
  end  
  
  def say_hi  
    puts "#{@name} says hi"  
  end  
end  
  
andreas = Person.new("Andreas")  
andreas.say_hi

After writing this code in file create filename with .rb extenstion.
In you command line execute the file:


> ruby example.rb 

Andreas says hi 

Just practice this In next post we will learn Class Inheritance.

Back to Ruby Introduction >> click

Ruby Introduction

Ruby

"I always thought Smalltalk would beat Java. I just didn’t know it would be called ‘Ruby’ when it did”
- Kent Beck

Ruby Language:

  • Generic, interpreted, reflective, with garbage collection
  • Optimized for people rather than computers
  • More powerful than Perl, more object oriented than Python
  • Everything is an object. There are no primitive types.
  • Strong dynamic typing

Everything in Ruby is:

  • Assignment – binding names to objects
  • Control structures – if/else, while, case
  • Sending messages to objects – methods

Ruby is Line Oriented

  • Statements are separated by line breaks
  • You can put several statements on one line if you separate them by semicolon
  • For long statements you can escape the line break with backslash
  • After an operator, comma, or the dot in a method invocation you can have a line break and Ruby will know that the statement continues on the next line
  • You can have line breaks in strings

For Quick Start follow the link:


 In next post you will get introduction of Ruby class.

Myself



Hi Readers,

My name is Shaikshavali, you can call me simply "shaik".
From last two months in my mind always thinking about to write a blog where i can share my knowledge, thoughts etc,. Finally it comes true through this blog.
I am working in a company as a Ruby on Rails developer.

In my work experience i met lot of people, Some of them are inspired me to do something better for the society. Recently i created a website called www.knowgovtdata.com. Currently this website contains hospitals, blood banks and Pin codes details of India.

My intension is to Netizens can get all this info from one site. Then they can easily find the details inform the people who are in need.

Thanks

Hope you may get some info in future posts.

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...