At Scotland On Rails a few months ago, I was shown a nice way to put
class methods in a module, alongside instance methods, in such a way as
they will get added as class methods automatically, without any work
required on the part of the person including the module.
Unfortunately i failed to make a note of how to do this and i’ve since
forgotten. Can anyone show me?
It was something along these lines…(the following doesn’t work btw)
module Mappable
#instance methods
def foo
“foo”
end
#class methods
class << self.class
def bar
“bar”
end
end
At Scotland On Rails a few months ago, I was shown a nice way to put
class methods in a module, alongside instance methods, in such a way as
they will get added as class methods automatically, without any work
required on the part of the person including the module.
Unfortunately i failed to make a note of how to do this and i’ve since
forgotten. Can anyone show me?
I’m not sure if you can pick up the singleton methods from the module
and
automatically copy them to the class – I’ve got type errors before when
trying to move and rebind methods. I did come up with this, though,
which is
more expressive than writing an included() hook on every module:
At Scotland On Rails a few months ago, I was shown a nice way to put
class methods in a module, alongside instance methods, in such a way as
they will get added as class methods automatically, without any work
required on the part of the person including the module.
Unfortunately i failed to make a note of how to do this and i’ve since
forgotten. Can anyone show me?
It was something along these lines…(the following doesn’t work btw)
module Stuff
module ClassMethods
def biff(*args)
# …
end
def baz(*args)
# ...
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
The methods defined in Stuff::ClassMethods are added as class methods
when