Classes and Objects

The very root, the very heart of ruby is objects. Everything is an object and they receive messages and have attributes.

Creating Objects

Since everything is an object in Ruby the very nature of typing nearly anything at all would create an object. Such as:

"string"    # a String object
30          # a Fixnum object
3.444       # a Float object
[1, 3, 4]   # an Array object

All create objects. To define your own object or most other objects you need to use the syntax Object.new. This will actually create a blank and generic object with some messages it can already receive.

We can create our own abstract object ideas using Class.new or just class. like the following.

class World

end

We’ve just created an abstract object World. That means from there we can create a World object or instance by typing World.new

Messages

One of the big things that objects do is they receive messages, these are defined as methods on the class and look something like.

class World
  def hello
    # Code goes here
  end
end

It’s sending messages to objects that give them the ability to respond, change state or set attributes. For example, you could have method that just outputs a string.

class World
  def hello
    "Hello World!"
  end
end

Or you could send a message to an object that adds or changes attributes on the model such as:

class World
  # Here we initialize a world with 7 billion people
  def initialize
    @people = 7000000000
  end

  def new_born
    @people = @people + 1
  end
end

You can even make it change what it is completely! Like in the case of to_s on a number, this turns it into a string! Watch.

5.class #=> Fixnum
5.to_s #=> "5"
5.to_s.class #=> String

Attributes

In class we talked about attributes. And attributes are denoted by the @ symbol and are more commonly referred to as instance variables. Let’s see an example.

class House
  def initialize
    @location = "New York, NY" # @location is an attribute/ instance variable
  end
end

Attributes are automatically created if they weren’t there before hand. So when I said @location = "New York, NY" That attribute is immediately created when I say House.new

attributes aren’t immediately accessible to us. We can access these attribute by creating methods.

class Phone
  def initialize(brand)
    @brand = brand
  end

  def brand
    @brand # this gives us the ability to access the @brand attribute
  end

  def brand=(new_brand)
    @brand = new_brand # this allows us to change @brand
  end
end

###attr

  • attr_reader
  • attr_writer
  • attr_accessor

Since these are attributes and they are very common in ruby we have some syntax that is given to us to do these tasks.

welcome attr_ syntax. These can can be given at the beginning of the class to replicate what we just did with out attributes. For example

Using attr_reader gives us the ability to see the value of that attribute. For example.

class Chair
  attr_reader :material
  def initialize(material)
    @material = material
  end
end

#### Above is the same as

class Chair
  def initialize(material)
    @material = material
  end

  def material
    @material
  end
end

Next is attr_writer or the ability to explicitly change the attribute. This is done like so.

class Clock
  attr_writer :size
  def initialize(size)
    @size = size
  end
end

#### Above is the same as

class Clock
  def initialize(size)
    @size = size
  end

  def size=(new_size)
    @size
  end
end

Finally, attr_accessor just makes it so you have both.

End

That’s a crash course in how we do classes and objects. Use it how you wish.