Ruby Module Inclusion in Methods

Posted by Federico Builes on Stack Overflow See other posts from Stack Overflow or by Federico Builes
Published on 2010-06-01T22:58:13Z Indexed on 2010/06/01 23:03 UTC
Read the original article Hit count: 220

Filed under:
|
|

In class Foo I'd like to include method Bar under certain conditions:

 module Bar
   def some_method
     "orly"
   end
 end

 class Foo
   def initialize(some_condition)
     if !some_condition
       "bar"
     else
       class << self; include Bar; end
     end
   end
 end

Is there any cleaner (and clearer) way to achieve the include in the method without having to do it inside the singleton class?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about metaprogramming