Whats the difference between modules and classes in ruby? This is a good question, and rails beginners often miss the point. In this blogpost I do not want to explain the explicit difference between modules and classes.
For detailed information just check out these links or google a bit :-).
I want to explain how you could manage the following structure in a pure ruby project:
Imagine you have a rails project with a similar structure like this:
Here you can simple define models like that:
class Foo < ActiveRecord::Base end class Foo::OtherFoo < Foo end class Foo::NextFoo < Foo end
Rails handels the requirement and loading in a different way than Ruby does it. Rails extens the Ruby functionallity.
In a pure Ruby project you can implement the structure in the following way:
require "other_foo" class Foo end require "next_foo" class Foo class OtherFoo end end require "foo" class Foo class NextFoo end end
You can also work with modules:
require "other_foo" module Foo end require "next_foo" module Foo module OtherFoo end end require "foo" class Foo module NextFoo end end
It depends from your needs, with which style you work with.
But you cannot mix up modules and classes, if you are using the folder structure from above! Otherwise you will get following error:
foo.rb:3:in `': Foo is not a class (TypeError)
This should just be a short insight into the topic Rails vs. Ruby Module vs. Class loading and requires.