Forum: Ruby Answer: Module that includes both class and instance methods

Posted by Steve Midgley (stevemidgley)
on 2007-02-19 06:34
(Received via mailing list)
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
Posted by Trans (Guest)
on 2007-02-19 18:14
(Received via mailing list)
On Feb 19, 12:33 am, Steve Midgley <pub...@misuse.org> wrote:
> 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-talk-google/search?hl=en&group=ruby-talk-google&q=class_extension&qt_g=Search+this+group

T.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.