(Ruby_4) Class and Module

Class

In Ruby, almost anything is an object. Block is not an object because it can not live alone.

Object = state + behavior. For example, we can define a human as following:

  1. states: black hair, yellow skin, age 18
  2. behaviors: eat, drink, sleep

Class is a defined structure to create an object. For example,

class Animal
def eat(food)
puts "#{food} tastes good!!"
end
end

Then we can create an object with class

kitty = Animal.new
kitty.eat "tuna" # => print out "tuna tastes good!!"

We can customize the class with some predefined parameters. For example, if we want a cat with name, ‘kitty’ and gender, ‘female’

class Cat  def initialize(name, gender)
@name = name
@gender = gender
end
def say_hello
puts "hello, my name is #{@name}"
end
endkitty = Cat.new("kitty", "female")
kitty.say_hello # => hello, my name is kitty

The variable started with @

The instance variable cannot be acquired outside the class independently. We need predefined method to do so. For example,

class Cat

def initialize(name, gender)
@name = name
@gender = gender
end
def say_hello # the function to print out instance variable
puts "hello, my name is #{@name}"
end
def name
@name
end
def name=(new_name)
@name = new_name
end
endkitty = Cat.new("kitty", "female")
kitty.name = "nancy"
puts kitty.name # => nancy

There is a quicker method, using attr_accessor. Then we can create reader and writer.

class Cat

attr_accessor :name

def initialize(name, gender)
@name = name
@gender = gender
end
def say_hello
puts "hello, my name is #{@name}"
end
end

Instance method example

Instance method applies on instance variable; for example,

kitty = Cat.new("kitty", "female")
kitty.say_hello # this is an instance method

Class method example

Class method applies on class. self means the class itself.

class Cat
def self.all
# ...
end
end
Cat.all # this is a method that applies on class, Cat

For example,

class Cat  def eat # public method
puts "good!"
end
protected
def sleeping # protected method
puts "zzzzzzzzz..."
end
private
def gossip # private method
puts "Don't tell anyone"
end
endpublic # the method can be accessed by all people
private # the method can only accessed by class itself
protected # between public and private

Summary first:

  1. All three method can be called in a class
  2. Only public method can be called outside a class
  3. Private method can not have a receiver in class but protected method can

For example, the following code proves 1 and 2

kitty = Cat.new
kitty.eat # => "gool!"
kitty.sleeping # => NoMethodError
kitty.gossip # => NoMethodError

and the following code proves 3

class Person  

def speak
puts "Hey, Tj!"
end

def whisper_louder
whisper
end
private
def whisper
puts "His name's not really 'Tj'."
end
protected
def greet
puts "Hey, wassup!"
end
endclass Me < Person
def be_nice
greet
end
endtj = Me.new
tj.be_nice # "Hey, wassup!"
tj.greet # NoMethodError

For example, the following code build dog and cat class inherited from animal class

class Animal  def eat(food)
puts "#{food} tastes good!!"
end
endclass Cat < Animal
end
class Dog < Animal
end

In Ruby, any method can be modified. even the built in method.

If we create same class. It merges the two classes into one with all method preserved.

Module

It can be considered as plug-in. If we want a dog which can fly on a sky, we can create a class named dog with method, fly. However, it is quit strange to change the preexisted class just because we need a flyable dog. Then we can create a module and add it into the class, dog; for example,

module Flyable
def fly
puts "I can fly!"
end
end
class dog
include Flyable
end
kitty = dog.new
kitty.fly

The difference between Class and Module

Module is actually the upperclass of Class. It cannot use new to create an instance and cannot inherit from other class.