Mixin in Ruby

C++ allows developers to use multiple inheritance. Unlike C++, Ruby only allows developers to use single inheritance. But Ruby has mixin functionality, which allows code to be included from other modules.

Here’s sample code to demonstrate mixin:

module MixinFirst
	def mixin_first_display
		puts "This is mixin first"
	end
end

module MixinSecond
	def mixin_second_display
		puts "This is mixin second"
	end
end

class Base
	def inheritance_display
		puts "This is inheritance"
	end
end

class Child < Base
	include MixinFirst
	include MixinSecond
end

child = Child.new
child.mixin_first_display
child.mixin_second_display
child.inheritance_display

Sample result:

This is mixin first
This is mixin second
This is inheritance

Inheritance in Ruby

Inheritance is one of Object-Oriented concept. Since Ruby is an Object-Oriented Programming, it has implemented the Inheritance functionality.


#intialize global variable
$value = 0

class Parent
    def parent_value
        $value = 5
    end
end

class Child < Parent
    def child_value
        $value = 10
    end
end

child = Child.new

#calling parent_method using child object
base_value = child.parent_value

#calling child_method
derive_value = child.child_value

#display result
puts "Here's parent value: #{base_value}"
puts "Here's child value: #{derive_value}"

Sample Result:

Here’s parent value: 5
Here’s child value: 10

Encapsulation in Ruby


class Encapsulation
    def get_value
        @value
    end

    def set_value value
        @value = value
        return nil
    end
end

encapsulation = Encapsulation.new
encapsulation.set_value(15)
puts encapsulation.get_value

In this code, you cannot mess around with the state of its variable @value. The only way you can access it is through get & set method (get_value, set_value). This is the encapsulation

Sample Result

15

Variables in Ruby

Here just how’s  to declare global, class, instant, and local variables in Ruby:


$global_variable = "global variable"

class First
    @@class_variable = 0

    def display_global_variable
        puts "#{$global_variable} access from class 'First'"
    end

    def class_variable
        @@class_variable += 1
        puts "value of class variable : #{@@class_variable}"
    end
end   

class Second
    def initialize(id,name)
        @id = id # id is a local variable
        @name = name # name is a local variable
    end

    def display_global_variable
        puts "#{$global_variable} access from class 'Second'"
    end

    def display_instance_variable
        puts "display value of instance variable id = #{@id}, name = #{@name} "
    end
end

#instantiate object
first = First.new
second = Second.new(1, "borey")

#Demonstrate on class variable
first.display_global_variable
second.display_global_variable
puts "\n"

#Demonstrate on class variable
first.class_variable
first.class_variable
puts "\n"

#Demonstrate on instance variable
second.display_instance_variable

Here’s the result after code is executed:

global variable access from class ‘First’
global variable access from class ‘Second’

value of class variable : 1
value of class variable : 2

display value of instance variable id = 1, name = borey

Open-Closed Principle

In object-oriented programming, the open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”; that is, such an entity can allow its behavior to be modified without altering its source code.

Modules that conform to the open-closed principle have two primary attributes:

  1. They are “Open For Extension”. This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications.
  2. They are “Closed for Modification”. The source code of such a module is inviolate. No one is allowed to make source code changes to it.

In the implementation of this principle, normally, the abstract interfaces are used.