Monday, 4 November 2019

How to use module as Namespace in ruby?

 Module or Mix-in used as a Namespace in ruby. It is used to avoid collision between two classes.


module Rajkumar
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email is #{@email}"
        end
    end
end

module Somu
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email 2 is #{@email}"
        end
    end
end

puts raj = Rajkumar::Uthayaa.new
puts raj.email='rajutaya@gmail.com'
puts raj.display_email
puts raj = Somu::Uthayaa.new
puts raj.email='lalalslsls@gmail.com'
puts raj.display_email

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...