Newbie mixin questions

Or as a third alternative, I imagine you could do:

module MyClassMethods
def class_foo
end
def class_bar
end
end

module MyModule
def included(c)
c.extend(MyClassMethods)
super
end

def instance_foo
end
def instance_bar
end
end

That way it looks like you get both instance and
class methods from one module, and you share the
class methods (if I take your objection correctly).

Unless I’m confused and that doesn’t work.

  • Dan
···

----- Original Message -----
From: Robert Klemme bob.news@gmx.net
Date: Tuesday, August 12, 2003 7:23 am
Subject: Re: newbie mixin questions

“Michael Garriss” mgarriss@earthlink.net schrieb im Newsbeitrag
news:3F37F45B.8080002@earthlink.net

dblack@superlink.net wrote:

One other technique that might be relevant is Module#included,
as I understand it is essentially a new name for
append_features. (Is
that accurate?) Here’s a 1.8.0 example:

module M
def self.included(c)
def c.something
puts “I’m a class method!”
end
super
end

def another
puts “I’m an instance method!”
end
end

The inclusion now operates, with fairly clear separation, on
both the
class and the instances:

class A; include M; end

A.something
A.new.another

David

That’s what I needed. Thanks!

But: you now have multiple something methods with the same definition.
That’s ugly. I’d prefer the approach to extend a class with M as
shown in
the other postings:

[matz:]

class Bar
Bar.extend(Foo)
end

class Bar
extend Foo
end