Object Oriented Programming 


in RUBY


OOP Features:


  1. Data Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
  5. Message Passing

Object Oriented Ruby



  • Ruby is pure object-oriented language, Everything appears to Ruby, is an object.
  • Every value in Ruby is an object, even the most primitive things: strings, numbers and even true and false

Class and Objects


Class and Objects

         Class:
          - Blueprint for a data type
          - It combines data representation and methods 
          - Logical representation of data
          - e.g.  Animal is an class

         Object:
          - Instance of a Class
          - Physical representation of data
          - e.g. dog is an object of class Animal

Defining a Class & Object


       class ClassName
       end

    • We declare the object of class using new keyword

obj = ClassName.new

Types of variables

  •   Local variables
      - scope: local
      - convention: defined with lower snake case e.g. new_result
  •    Instance variables
     - scope: same single instance of class
     - convention: prefixed with @ operator e.g. @result
  •    Class variables
     - scope: different instances of a same class
     - convention: prefixed with @@ operator e.g. @@data
  •   Global variables
    - scope: anywhere in the application
    - convention: prefixed with $ operator e.g. $total_count

Initialize method 

  • It's constructor in ruby
  • useful when you want to initialize some class variables at the time of object creation. 
  • This method may take a list of parameters
class Box
  def initialize
    # do something
  end
end

Access Modifiers


  • private means the method(s) are accessible only when they can be called without an explicit receiver. Only self is allowed to be the receiver of a private method call.

  • A protected method can be called from a class or descendant class instances, but also with another instance as its receiver.

A Class Example

#class with variable types
$global_var = "global"
class Greeter
  attr_accessor :surname
  def initialize(name = "World")
    @name = name
    @surname = "joshi"
    @@company = "WGBL"
    local_name = "local"
  end
  
  def access
    puts "Access global variable #{$global_var}"
    puts "Access class variable #{@@company}"
    puts "Access instance variable #{@name}"
    puts "Access instance variable #{local_name}"
  end
end

Getters and Setters

  • Why? The answer is encapsulation and security
  • What's this? nothing but methods for accessing data members
  • Getter: method for 'getting' the data member's value
  • Setter: method for 'setting' the data member's value

 #getter method
def width
  @width
end

#setter method
def width=(w)
   @width = w
end

Getters and Setters..

  • attr_reader can also be used to generate getter
  • attr_writer can be used to generate setter
  • attr_accessor can be used to generate getter and setter both

class Box
  # generate getter method for width
  attr_reader :width

  # generate setter method for width
  attr_writer :width

  # generate getter and setter methods for   
  #height
  attr_accessor :height
end
 

Types of methods 

  1. Class methods: called on a class, singleton method
  2. Instance methods: called on an instance

class Box
  def initialize(w,h)
    @width, @height = w, h
    @@count +=1
  end

  def self.count  # class method
     @@count
  end

  def area  # instance method
     @width * @height
  end
end

Inheritance in Ruby

  • Does not directly support multiple inheritance but can be achieved using mixins
class Animal
   def make_noise
      “awww……”
   end
end

class Dog < Animal
  def make_noise # overriding method 
     “bhow bhow”
  end
end

class Cat < Animal
  def walk
     “walking”
  end
end

Modules

  • Modules are a way of grouping together methods, classes, and constants. analogous to package in Java
  • Modules give you two major benefits.
  1. Modules provide a namespace and prevent name clashes.
  2. Modules implement the mixin facility.
module Utility
   def self.sin(a)
      # do something
   end

  class Language
    def hindi_hello
       puts “namaste”
    end
  end
end

    Mixins

    • Ruby’s way to deal with the multiple inheritance problem
    • Mixins are a way of adding a set of methods and constants to a class, without using class inheritance.

    module A
       def a
       end
    end
    module B
       def b
       end
    end
    class Sample
      include A
      include B
      def s
      end
    end
    




    File I/O

    Creating Files


    • You can create a File object using File.new method for reading, writing, or both, according to the mode string.
    • Modes: r, r+, w, w+, a, a+

    f = File.new(“filename”, “mode”)
    
    # process file
    
    # closing the file after  performing options is important
    
    f.close
    

    Opening Files


    • You can use File.open method to create a new file object and assign that file object to a file
    • File.open method can be associated with a block, whereas you cannot do the same using the File.new method
    • There are many methods that can be used to perform action on file like readline , write, readlines, read_char, read_byte etc
    
    File.open(“filename”, “mode”) do |file|
      # process files
    end
    
    

    Directories

    • All files are contained within various directories, and Ruby has no problem handling these too. Whereas the File class handles files, directories are handled with the Dir class.
    
    Dir.pwd # gives current directory
    
    Dir.chdir(“path”) # change current directory to specified path
    
    Dir.entries(“path”) # gives list of entries in given path
    
    Dir.mkdir “path” #creates directory specified in path
    



    Questions ?

    References

    • https://www.ruby-lang.org/en/documentation/quickstart/
    • https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/

    Contact



    • Skype id: dattdongare
    • Email id: datt@cart91.com
    • Phone: 8446353453



    Thank you!

    Ruby OOP

    By Datt Dongare

    Ruby OOP

    • 664