Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats a good ruby idiom for breaking up a large class into modules?
    primarykey
    data
    text
    <p>I have a large class with lots of methods and it's starting to get a bit unorganized and hard to navigate. I'd like to break it up into modules, where each module is a collection of class and instance methods. Perhaps something like this:</p> <p>UPDATE: I've now realized that this is a pretty poor example. You probably wouldn't want to move validations or attributes out of the core class.</p> <pre><code>class Large include Validations include Attributes include BusinessLogic include Callbacks end </code></pre> <p>After reading Yehuda's post about <a href="http://yehudakatz.com/2009/11/12/better-ruby-idioms/" rel="nofollow noreferrer">Better Ruby Idioms</a>, I'm curious how others are tackling this problem. Here's the two methods I can think of.</p> <h2>First Method</h2> <pre><code>module Foo module Validations module ClassMethods def bar "bar" end end module InstanceMethods def baz "baz" end end end class Large extend Validations::ClassMethods include Validations::InstanceMethods end end </code></pre> <h2>Second Method</h2> <pre><code>module Foo module Validations def self.included(base) base.extend ClassMethods end module ClassMethods def bar "bar" end end def baz "baz" end end class Base include Validations end end </code></pre> <h2>My questions are:</h2> <ul> <li>Is there a better way to do this?</li> <li>How do you get a one-liner module mixin for a set of class/instance methods with the least amount of magic?</li> <li>How do you namespace these modules to the base class without namespacing the class itself?</li> <li>How do you organize these files?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload