Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create classes in ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 148
    Comment on it

    Ruby is a pure object oriented language and everything in ruby is an object of some class. In ruby we can create classes and create objects of the classes. In the below example we will create 3 classes Person, Address and Company. An object of a class can be created with public method new. Internally this public method(new) calls private method initialize. Initialize works as a constructor of the class.

    Setter method: The setter method sets the value of instance variable for the object. Getter method: The getter method get the value of instance variable of the object

    class Person
      def initialize(name, age, email, gender)
        @name = name
        @age = age
        @email = email
        @gender = gender
        @addresses = []
      end
    
      def add_new_address(address)
        addresses << address
      end
    
      def name
        @name
      end
    
      def age
        @age
      end
    
      def email
        @email
      end
    
      def gender
        @gender
      end
    
    def addresses
        @addresses
      end
    end
    class Address
      def initialize(city, zip, country)
        @city = city 
        @zip = zip 
        @country = country
      end
      def city
        @city
      end
    
      def zip
        @zip
      end
    
    def country
        @country
      end
    end
    class Company
      def initialize(name, address)
        @name = name
        @address = address
        @employees = []
      end
    
      def employees
        @employees
      end
    
    def add_employee person
        employees << person
      end
    end
    

    Using classes to create objects:

    creating objects of address class

    address1 = Address.new("Delhi", 110018, "India")
    address2 = Address.new("Dehradun", 248001, "India")
    address2 = Address.new("Punjab", 258008, "India")

    creating objects of person class

    person1 = Person.new('Martin', 22, "martin@yahoo.com", "Male")
    person2 = Person.new('Virat', 22, "virat@yahoo.com", "Male")
    person3 = Person.new('Priya', 22, "priya@yahoo.com", "Female")

    adding address to person

    person1.add_new_address(address1)
    person1.add_new_address(address2)
    person2.add_new_address(address3)
    person2.add_new_address(address2)
    person3.add_new_address(address3)
    person3.add_new_address(address1)

    creating objects of company class and add employees

    company = Company.new("Evontech", address2)
    company.add_employee(person1)
    company.add_employee(person2)
    company.add_employee(person3)

    showing all employees of company

    company.employees

    first employee

    company.employees[0]

    name of first employee

    company.employees[0].name

    addresses of first employee

    company.employees[0].addresses

    last address of first employee

    company.employees[0].addresses[-1]

 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: