i think this is clearest for people reading your code:
module M
module InstanceMethods
end
module ClassMethods
end
def self::included other
other.module_eval{ include InstanceMethods }
other.extend ClassMethods
other
end
end
class C
include M
end
but it's an opinion...
-a
···
On Sun, 25 Sep 2005, Michael Roth wrote:
Hello all,
I'm a little bit new to the wonderful world of ruby and have a small
problem. I would like to mixin a module method:
module M
def M.foobar id
puts "Hello Ruby: #{id}"
end
end
class C
include M
foobar :example
end
But that doesn't work. I'm trying to write some helper class methods
like attr_accessor and friends.
I don't like to write
class C
include M
M.foobar :example
end
because 'M' has to many chars to type...
My helper class methods should look like attr_accessor and friends.
What is the right way to solve this problem?
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna
module M
module InstanceMethods
end
module ClassMethods
end
def self::included other
other.module_eval{ include InstanceMethods }
other.extend ClassMethods
other <------------------?
end
end
class C
include M
end
Why the "other" return (see question mark above)? It seems to work
without.
This is fine if you want to add the foobar method to ~all~ classes and
modules. I'd recommend Ara's way if you want to be more selective
about which classes get the methods.
Sean
···
On 9/25/05, michele <michelemendel@gmail.com> wrote:
How about:
class Module
def foobar id
puts "Hello Ruby: #{id}"
end
end