Answer: Module that includes both class and instance methods?

S

Steve Midgley

Hello,

After an hour of farting around trying to figure this out, of course I
do figure it out 20 seconds after writing to the list. Well, here's one
solution to my issue - I'd still love to see a one-line solution if
anyone can come up with one (but it's not necessary for me at all, just
curiosity and desire to learn cool Rubyisms):

[code starts]

module Models
module Ver
def is_versioned
include Models::VersionsTables::InstanceMethods
self.extend Models::VersionsTables::ClassMethods
end
end
module VersionsTables
module InstanceMethods
def instance_test
"instance test"
end
end
module ClassMethods
def class_test
"class_test"
end
end
end
end


class Live
extend Models::Ver

is_versioned
end

puts "::"+Live.class_test
live = Live.new
puts live.instance_test

[code ends]

So it uses two lines of code, but I can live with that because only the
second line of code actually causes the class AND instance methods to
be included - so they're all or nothing - you either get class and
instance or nothing (the reason I care is that some monkey-see,
monkey-do coder down the road could pull something into their class out
of context and I want these functions to either work or fail, not yield
partial and erroneous capability).

Sorry for being the noise in your signal,

Steve
-
The law is an envious monster.
- Rex Stout
 
T

Trans

Hello,

After an hour of farting around trying to figure this out, of course I
do figure it out 20 seconds after writing to the list. Well, here's one
solution to my issue - I'd still love to see a one-line solution if
anyone can come up with one (but it's not necessary for me at all, just
curiosity and desire to learn cool Rubyisms):

Hi--

Have a look at facets' module/class_extension. You can read the
conversations that led to it's development here:

http://groups.google.com/group/ruby...ogle&q=class_extension&qt_g=Search+this+group

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top