Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create getter and setter methods in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.06k
    Comment on it

    Ruby setter & getter methods are similar to properties used in .net. They are used to access the instance variables of a class outside the class using the object of the class. By default instance variables are only accessible inside the instance methods, but setter & getter allow us to access them outside the class directly with the use of object of the class.



    Setter & getter can be accessed in the following ways in ruby



    1) By defining the setter & getter methods which would be called using the object of the class.

    class Vehicle
      def initialize(name, company_name, color)
        @name = name
        @company_name = company_name
        @color = color
      end
    
      def color=(new_color)
          @color = new_color
      end
    
      def color
        @color
      end  
    end
    
    vehicle = Vehicle.new("eon","Hyundai","white")
    vehicle.color = "Blue"
    p "New color for eon is = " + vehicle.color
    

    After Execution we get :

    taran@taran-System-Product-Name:~/Documents/ruby_prgms$ ruby Vehicle.rb
    "New color for vehicle is = Blue"
    


    2) By using the attr_reader & attr_writer methods given by ruby which automatically creates the setter & getter method for us

    class VehicleNew
      attr_reader :color
      attr_writer :color
      def initialize(name, company_name, color)
        @name = name
        @company_name = company_name
        @color = color
      end
    end
    
    vehicle = VehicleNew.new("baleno","Maruti","white")
    vehicle.color = "Red"
    p "New color for baleno is = " + vehicle.color
    

    After Execution we get :

    taran@taran-System-Product-Name:~/Documents/ruby_prgms$ ruby VehicleNew.rb
    "New color for vehicle is = Red"
    


    3) By using attr_accessor which provides both setter & getter method automatically. It is equivalent of using attr_reader & attr_writer

    class VehicleNew1
      attr_accessor :color
      def initialize(name, company_name, color)
        @name = name
        @company_name = company_name
        @color = color
      end
    end
    
    vehicle = VehicleNew1.new("XUV 500","Mahindra","white")
    vehicle.color = "Black"
    p "New color for XUV 500 is = " + vehicle.color
    

    After Execution we get :

    taran@taran-System-Product-Name:~/Documents/ruby_prgms$ ruby VehicleNew1.rb
    "New color for XUV 500 is = Black"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: