A few days ago I was contrasting extend and include in Ruby. In that post I said I would show you how to make a method available to both the class scope and the instance scope. You can do it by using the included and extend methods:
module Log
def Log.included cls
cls.extend Log #also works by passing self, but Log is less confusing
end
def log str
p str
end
end
class Thing
include Log
log "works from class" # => "works from class"
def test_log
log "works from instance"
self.log "works from self (in instance)"
Thing.log "works from class (in instance)"
end
end
Thing.new.test_log # => "works from instance"
# => "works from self (in instance)"
# => "works from class (in instance)"
Module#included is called anytime the module is included in a class. When it is, we just call class.extend on the class that included the module and pass in self (which is the module).
Most people think just using the class method is fine, e.g. Rails.logger, but I like having a method available in both places. It makes me feel warm and fuzzy, like a blanket wrapped around me on a cold winter’s night…. Obviously you shouldn’t use this technique all the time, only when it makes sense.
Here is a nice general pattern for making modules:
module Mod
module ClassMethods
#put class methods here
end
module InstanceMethods
#put instance methods here
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
If you wanted it to build a product you’d find a way to get time to work on it. If you really wanted to start that new hobby you’d sacrifice something to find the time and money to do it.
I'll define a "Wannabe Entrepreneur" as someone who has never made money from their businesses. Here are the different types of wannabes.
In the past few years I've built go-carts, built a 200+ sq ft workshop, written several eBooks. How do I create a life where I have time to work on side projects?
Receive 5 Software projects mistakes we have made over the years and how to avoid them.