Thursday, February 4, 2016

Designing utility methods in ruby

This demo is to introduce ways of defining static methods in ruby. Generally utility classes will not store any state information and thread safe.

# Demo for defining utility methods in ruby
# In Java it is known as static methods

module Universe
  module Earth
    def sayHello
      puts "Hello from earth!"
    end

    def sayHi
      puts "Hi from earth!"
    end

    def sayBye
      puts "Bye from earth!"
    end
  end
end

module Universe
  module Moon
    class << self
      include Universe::Earth
      private
        def foobar
          puts "Hello from foobar!"
        end
    end
  end
end

Universe::Moon.sayHello