Upcoming SlideShare
Loading in...5

Like this? Share it with your network








Total Views
Views on SlideShare
Embed Views



2 Embeds 64 52
http://localhost 12



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Classes Presentation Transcript

  • 1. Ruby Classes --Sowjanya Mudunuri
  • 2. References • The Ruby Programming Language by David Flanagan and Yukihiro Matsumoto • Beginning Ruby by Peter Cooper • Disclaimer: This notes is composed from the above referenced books. This presentation is used to help fellow friends who are trying to learn ruby.
  • 3. Definitions: Object Oriented Programming: Programming using classes and objects to model real-world concepts Class: is a definition of a concept. Ex: Box, Person, Ticket…. A class acts a blueprint. Object: is an instance of a class. Think of them as real-life objects. Ex: Mary, my_real_ticket. Encapsulation: the ability of an object to hide their data behind an abstracted interface.
  • 4. Defining a class class Point end  class names must begin with a capital letter  In addition to defining a class the class keyword creates a new constant to refer to the class  Within the body of the class but outside of any instance methods defined by the class, the self keyword refers to the class being defined
  • 5. Creating an Object p = All class objects have a method named ‘new’ that creates a new instance of the class the ‘new’ method automatically invokes the ‘initialize’ method on that instance. Whatever arguments you passed on to ‘new’ are passed onto ‘initialize’.
  • 6. Initialize class Point def initialize(x,y) @x = x @y = y end end The initialize method is private so you cannot explicitly call initialize on p to reinitialize its state
  • 7. Instance methods Instance method: An instance method is a method that is invoked on an instance of the class. Inside a instance method the value of self is an instance of the class in which the method is defined class Person def name end end p =
  • 8. Instance Variables  Instance variables begin with an @  Each instance of a class has its own copy of these variables  The instance variables of an object can only be accessed by the instance methods of that object. ie., Code that is not inside an instance method cannot read or set the value of an instance variable  Ruby objects are strictly encapsulated! instance variables cannot be directly accessed from outside of the object
  • 9. Accessor/getter methods Accessor methods return the value of the instance variables class Point def initialize(x,y) @x = x @y = y end def x @x end def y @y end end
  • 10. Setter Methods They set the value of the instance variables class Point def x=(value) @x = value end def y=(value) @y = value end end
  • 11. Watchout setter methods will not work within other instance methods of your class @x = 2 (RIGHT) x=2 (WRONG).It simply creates a new variable self.x=2 (RIGHT)
  • 12. attr_reader & attr_accessor methods attr_reader & attr_accessor methods are defined by the Module class and they create instance methods All Classes are Modules. The Class class is a subclass of Module class attr_reader creates getter methods attr_accessor creates getter and setter methods class Point attr_reader :x, :y attr_accessor :z end
  • 13. Class methods • Methods that are invoked on a class class Point def puts “I am from class method” end def name puts “I am from instance method” end end
  • 14. When do we use Class Methods? When we want a class to do something that is relevant to the class as a whole than asking individual objects ActiveRecord::Base#find Class Methods can only be called on classes and instance methods can only be called on an instance of a class
  • 15. More examples to define Class Methods class Point def puts “I am inside a class method” end end class Point class << self def name puts “I am inside a class method” end end end
  • 16. More examples to define Class Methods We can also Open up the Point class so we can add methods to it. After Defining the Point class class << Point def name puts “I am inside a class method” end end
  • 17. Inheritence When a ChildClass inherits from the ParentClass, the ChildClass gets all the behavior(instance methods + class methods) from the ParentClass We say ChildClass is a subclass of ParentClass ParentClass is a superclass of ChildClass SYNTAX: class Rectangle < Shape end
  • 18. Inheritence  If you do not specify a superclass when you define a class, your class implicitly extends from Object  Ruby’s instance variables are not inherited and have nothing to do with the inheritence mechanism  A class can have as many subclasses as possible can only have one superclass.  Ruby doesnot support multiple inheritence
  • 19. Advantages of Inheritence • More generic behavior goes into the superclass and the specific behavior goes into the subclass • Saves a lot of repetition. You need to change the common behavior only at one place rather than all the classes class Animal class Human < Animal class Dog < Animal def eyes def legs def legs 2 2 4 end end end end end end
  • 20. Method Overriding  When a subclass defines a method with the same name as a method in the super class, the method in subclass is used class Person def name “General name” end def smart “yes” end end class Engineer < Person def name puts “I am an Engineer” end end
  • 21. Augmenting behavior by chaining when we override a method we replace it altogether To augment the method by adding new code the keyword ‘super’ comes to our rescue class Engineer < Person def name “Engineer” + super end end
  • 22. Exercise Time!!!! Step 1: Go to IRB or (or) Step 1: you can create each of the following classes in a separate ruby file. Step 2: Define a class named “operation” which takes two values upon intialize and stores them in instance variables. Step 3: Create getter methods for this variables.( hint you can use attr_reader) Step 4: Define a class named “sum” and inherit from “operation”
  • 23. Exercise Time!!!! • step 5: Define an instance method inside the “sum” class named “value” and return the value of the sum of the two instance variables • step 6: Similarly implement “difference” class • step 7: Similarly implement “multiply” class • step 8: Similarly implement “divide” class • step 9: create a class named “calculate” which instantiates each of these classes and test if the result is correct
  • 24. For people who have ruby and git on their machine. • once you are done you can place all the ruby files in git with “git init” and push it to your github. • Yay…