Class in Ruby

2020-09-25 hit count image

let's see what the class is and how to use the class in Ruby.

Outline

In this blog post, we will see what the Class is and how to use the Class in Ruby.

Class

The variable and data are all objects in Ruby. The Class is a blueprint of the object. In Ruby, You can create a class and you can combine the multiple classes if you need.

The languages based on objects such as Ruby are called OOP(Object Oriented Project). The biggest feature of OOP is that a class can inherit, and the inherited class can use the parent functions. Also, you can re-write the parent functions in the inherited class.(Override).

Writing the contents of the Class is called Define the Class. We can use the class statement to define the class.

class Book
...
end

We can use the new method to create an object from the class.

book = Book.new

Variables

We can use many kinds of Variables in Class. Let’s see what the variables we can use in Ruby.

Instance Variables

The Instance variables are a global variable in the Class. The variable name is started with @.

class Book
  def printTitle
    puts @title
  end
  ...
end

You can refer to the Instance variable via the method in the object created by the class. You can’t refer to the variable directly outside of the object.

class Book
  def initialize
    @title = 'Ruby'
  end

  def printTitle
    puts @title
  end
end

book = Book.new
book.printTitle
# Ruby

Class variables

The Class variables are a common variable of the class. The class variables are shared in all objects created by the same class, and the variable name is started with @@.

class Book
  @@publisher = 'dev-yakuza'

  def printPublisher
    puts @@publisher
  end
end

book = Book.new
book.publisher
# dev-yakuza

You can init the Class variables when you defined the class. You can refer to the Class variables via the method in the class, and you can’t refer to them outside of the class.

Constant

You can define the Constants in the class, and the variable name is started with the capital alphabet.

class Book
  Language = 'EN'
end

You can refer to the Constants directly outside of the class.

puts Book::Language

The Constants are similar with the Class variables, but the class variables can be reassigned.

class Counter
  @@count = 0

  def plus
    @@count += 1
  end

  def printCount
    puts @@count
  end
end

counter = Counter.new
counter.plus
counter.printCount
# 1

Method

There are the Instance method and Class method in the class. Let’s see what they are.

Instance method

The Instance method is a general method. You can call the Instance method via the object.

class Greeting
  def initialize
    @name = 'World'
  end

  def hello
    puts 'Hello ' + @name
  end
end

greeting = Greeting.new
greeting.hello
# Hello World

Class method

The Class method is used for the process independent with the object. You can call the method via the class

class Greeting
  @@name = 'World'

  def Greeting.hello
    puts 'Hello ' + @@name
  end
end

Greeting.hello
# Hello World

The Class method is called by the class, so you can use the Instance variables because they are created when the instance is created from the class.

Initialize object

You can use the initialize method to initialize the object of the class.

class Book
  def initialize
    @title = 'Ruby'
  end

  def printTitle
    puts @title
  end
end

The initialize method is automatically called when the object is created by the new method.

class Book
  def initialize (author)
    @author = author
  end

  def printAuthor
    puts @author
  end
end

book = Book.new('dev-yakuza')
book.printAuthor
# dev-yakuza

Like above, you can pass the parameters via the new method to the initialize method.

Accessor

You can access the Instance variables outside of the object by using the Accessor instead of using the method. You can define the Accessor with attr_reader, attr_writer, attr_accessor.

class Book
  def initialize
    @title = 'Ruby'
  end

  attr_accessor :title
end

book = Book.new
puts book.title # Ruby
book.title = 'Rails'
puts book.title # Rails

Inheritance

In OOP, The class can be inherited the other classes.

Inheritance of the class

The inheritance of the class means that you can define a new class that extends or limits features from the existing classes. The Super class is a target class of the inheritance and the Sub class is a class which is inherited the super class.

class [Sub Class] < [Super Class]
  ...
end

You can use the Inheritance of the class like below.

class Fruits
  def fruits
    puts "It's a Fruits"
  end
end

class Apple < Fruits
  def apple
    puts "It's an Apple"
  end
end

apple = Apple.new
apple.fruits # It's a Fruits
apple.apple # It's an Apple

As above, The Apple class doesn’t have the fruits method but is inherited from the Super class, we can use the fruits method. Also, You can define a class from the inherited class from the Super class.

class Fruits
  def fruits
    puts "It's a Fruits"
  end
end

class Apple < Fruits
  def apple
    puts "It's an Apple"
  end
end

class Pie < Apple
  def pie
    puts "It's a Pie"
  end
end

pie = Pie.new
pie.fruits # It's a Fruits
pie.apple # It's an Apple
pie.pie # It's a Pie

When you define the class from the Sub class, the class can’t inherit more than two classes.

Override

Redefining the method of the Super class in the Sub class is called the Override of the method. The Override means that

class Fruits
  def name
    puts "It's a Fruits"
  end
end

class Apple < Fruits
  def name
    puts "It's an Apple"
  end
end

apple = Apple.new
apple.name # It's an Apple

Access modifier

You can restrict a method’s access by using the Access Modifier in the class. There are public, private, protected of the Access Modifier. If you don’t set the Access Modifier for the method, all of them are under public.

class Book
  def protectedTest
    puts "this is a protected function"
  end

  protected :protectedTest

  def publicTest
    puts "this is a publicTest"
  end
end

book = Book.new
book.publicTest # this is a publicTest
book.protectedTest # protected method `protectedTest' called for #<Book:0x00007f8c6d88ebd0> (NoMethodError)

The Access Modifiers have characteristics like below.

Access ModifierDescription
publicCalled with no limitation.
privateCan’t be called.
protectedCan’t be called. However, it can be called from the object the method belongs.
class Book
  def privateTest
    puts "this is a private function"
  end

  private :privateTest

  def protectedTest
    puts "this is a protected function"
  end

  protected :protectedTest

  def publicTest
    puts "this is a publicTest"
    protectedTest
    privateTest
    self.protectedTest
    self.privateTest
  end
end

book = Book.new
book.publicTest
# this is a publicTest
# this is a protected function
# this is a private function
# this is a protected function
# private method `privateTest' called for #<Book:0x00007fa2d9066a98> (NoMethodError)

Singular class definition

The Singular class definition means adding the method or instance variable to the existing object.

class Book
  def initialize
    @title = 'Ruby'
    @author = 'dev-yakuza'
  end

  def printTitle
    puts @title
  end
end

book = Book.new

class << book
  def printAll
    puts @title + ' / ' + @author
  end
end

book.printAll
# Ruby / dev-yak

def book.printAuthor
  puts 'Author: ' + @author
end

book.printAuthor
# Author: dev-yakuza

Class split definition

In Ruby, you can split the definition of the class.

class Book
  def initialize
    @title = 'Ruby'
    @author = 'dev-yakuza'
  end

  def printTitle
    puts @title
  end
end

class Book
  def printAuthor
    puts @author
  end
end

book = Book.new
book.printTitle # Ruby
book.printAuthor # dev-yakuza

The method that defined later is added, but if there is the same method, the previous method is deleted, and added a new method.

class Book
  def initialize
    @title = 'Ruby'
    @author = 'dev-yakuza'
  end

  def printTitle
    puts @title
  end
end

class Book
  def printTitle
    puts 'title: ' + @title
  end
end

book = Book.new
book.printTitle # title: Ruby

Completed

We’ve seen what the class is and what the inherit is in Ruby. Ruby is the OOP language so the class is used frequently. So let’s remember the Class usages in this blog post.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts