5 Sept 2013

Simple but must have Ruby knowledges

Yesterday I have been interviewed by some company and here is most interesting question that represents how deeply you need to understand Ruby lang.

Question was 'How does override class method using module in Ruby'?

For sure it should be done like this:

class Klass
  def self.say
    puts 'class'
  end
end

module FooModule
  def self.included base
    base.instance_eval do
      def say
        puts "module"
      end
    end
  end
end

Klass.send(:include, FooModule)

Klass.say # => 'module'
More explanation you can find here http://stackoverflow.com/questions/9128515/how-to-override-static-class-method-using-module-in-ruby

And one more:

What is differences between module and class in Ruby?

Shortly says: module acts like libraries and provide function for classes, and classes are about objects.

More here http://stackoverflow.com/questions/151505/difference-between-a-class-and-a-module


Keep learning!

No comments:

Post a Comment