What has been in the works lately? It'll blow your mind.

Generic placeholder image

This stuff is pretty amazing

Ruby Classes an Example: class Person

I'd like to tell you a little bit about classes with the example written below: class Person.

A class in ruby is a blue print that specifies :

1. What the object can keep as data. This is also known as the properties or instance variables or state of the object.

2. What the object can do to interact with other objects or itself. This is also known as the objects methods or behavior or instance methods.

Other concepts are inheritance and composition. Inheritence occurs when a class inherits properties and behavior of another class. Inheritance is known as an is-a relationship because a child class is a type of a parent class. Composition occurs when a class is composed of other classes. Composition is also described as a has-a relationship, because a class can have another class composed within it.

State of an object

The class Name has instance variables first middle and last. The class Date has instance variables day month and year. The class Person has instance variables birth_date and name. These are all examples of the properties or state of each object that is instantiated with the classes.

The read/write properties of the attributes are given by the line:

attr_accessor :birth_date, :name

attr_accessor simply means that we have given the property methods to read the value and write the value. This is also known as getter and setter methods.

Behavior of an object

      def initialize(first_name, middle_name, last_name)
        @name = Name.new(first_name, middle_name, last_name)
        @birth_date = Date.new()
      end

      def to_s
        @name.to_s + " Born on " + @birth_date.to_s
      end

      def what_do_you_do
        puts "I'm a typical person.  I eat, sleep and I love to code Ruby on Rails."
      end

The classes Name, Date, Person have an instance method called initialize this method initializes instance variables in those classes. All 3 classes also have a to_s method that outputs information about the object in a string format. The Person class also has another method what_do_you_do which prints a string. These are all very basic examples of the behavior of these objects.

Inheritance

class Dev_boot_camp_student < Person

The class Dev_boot_camp_student is inherited from the class Person. This means that a Dev_boot_camp_student object is a type of Person object.

Composition

    class Person

    attr_accessor :birth_date, :name

      def initialize(first_name, middle_name, last_name)
        @name = Name.new(first_name, middle_name, last_name)
        @birth_date = Date.new()
      end

In the example below the class Person makes use of the principle of composition. Person is actually composed of 2 objects which are instances of class Date and class Name.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    class Person

    attr_accessor :birth_date, :name

      def initialize(first_name, middle_name, last_name)
        @name = Name.new(first_name, middle_name, last_name)
        @birth_date = Date.new()
      end

      def to_s
        @name.to_s + " Born on " + @birth_date.to_s
      end

      def what_do_you_do
        puts "I'm a typical person.  I eat, sleep and I love to code Ruby on Rails."
      end

    end

    class Dev_boot_camp_student < Person
    end

    class Date
      attr_accessor :day, :month, :year
      def initialize(day=0, month=0, year=0)
        @day = day
        @month = month
        @year = year
      end

      def to_s
        "Date: " + @month.to_s + "/" + @day.to_s + "/" + @year.to_s
      end

    end

    class Name

      attr_accessor :first, :middle, :last
      def initialize(first, middle, last)
        @first = first
        @middle = middle
        @last = last
      end

      def to_s
        "Name: " + @first + " " + @middle + " " + @last
      end

    end

We have the following driver code for the class written above:

puts
#Instantiating a new object with a type of class Dev_boot_student.  We instantiate with arguments first name, middle name, last name, which are specifications of the Dev_boot_student#initialize method

puts "The calvin object created"
    p calvin =  Dev_boot_camp_student.new("Calvin", "Cal", "Settachatgul")
puts
#We see that the birth_date of this object is 0 0 0

puts "Right now the birthdate is set to 0 0 0 values"
    p calvin.birth_date
puts
#We can set the individual properties for the birth_date by calling the Date setter methods .day = .month = .year =

puts "We now set the birthdate to 12/20/1978"
    calvin.birth_date.day = 20
    calvin.birth_date.month = 12
    calvin.birth_date.year = 1978
puts
#We can call the getter method .name and .birth_date and output it with the to_s method.

puts "ouput the name and birthdate of calvin"
    puts calvin.name
    puts calvin.birth_date
puts
# We can call what_do_you_do which outputs a string

puts "They ouput of what_do_you_do"
    calvin.what_do_you_do

And we get the following ouput

:> ruby person.rb

The calvin object created
#<Dev_boot_camp_student:0x007ff0a3190a50 @name=#<Name:0x007ff0a3190a28 @first="Calvin", @middle="Cal", @last="Settachatgul">, @birth_date=#<Date:0x007ff0a3190a00 @day=0, @month=0, @year=0>>

Right now the birthdate is set to 0 0 0 values
#<Date:0x007ff0a3190a00 @day=0, @month=0, @year=0>

We now set the birthdate to 12/20/1978

ouput the name and birthdate of calvin
Name: Calvin Cal Settachatgul
Date: 12/20/1978

They ouput of what_do_you_do
I'm a typical person.  I eat, sleep and I love to code Ruby on Rails.