Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Ruby: Modules, Mixins, Fixins, and Rails
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby: Modules, Mixins, Fixins, and Rails
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 12
    2008-04-21

    Table of Contents:
  • Ruby: Modules, Mixins, Fixins, and Rails
  • A Few Notes
  • Going off the Rails of the Crazy Train
  • Controlling the Situation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Ruby: Modules, Mixins, Fixins, and Rails - A Few Notes


    (Page 2 of 4 )

    If you are calling the modules from outside of Ruby, you should use the include statement instead of the require statement, like so:


    include 'addition.rb'

    You can store classes inside of modules if you wish:


    module Mathstuff

    class Adding

    def Adding.add(operand_one, operand_two)

    return operand_one + operand_two

    end

    end

    end

    And then to call the method (which resides in the class inside of the module) you can use the scope resolution operator(::):


    module Mathstuff

    class Adding

    def Adding.add(operand_one, operand_two)

    return operand_one + operand_two

    end

    end

    end

    puts “What is 10 + 10?”

    puts “The answer is: “ + Mathstuff::Adding.add(10, 10).to_s

    Here the result is:

      What is 10 + 10?

      The answer is: 20

    Puttin' on the Mix

    The best way to describe a mixin is to say that it is a class mixed with a module. One of the benefits of using a mixin is that it gives you the ability to inherit from several modules all at once.

    Here is an example:


    module Adding

    def add(value_one, value_two)

    return value_one + value_two

    end

    end

    module Subtract

    def subtract(value_one, value_two)

    return value_one - value_two

    end

    end

    class Mathmagician

    include Adding

    include Subtract

    end

    mathmagician = Mathmagician.new()

    puts "The difference between my looks and your looks is: " + mathmagician.subtract(10,2).to_s

    puts "Our combined looks are " + mathmagician.add(10,2).to_s

    The result of this program is:

      The difference between my looks and your looks is: 8

      Our combined looks are 12

    In the above example we create two modules and add them to the Mathmagician class, which inherits the adding and subtract methods, basically meaning that Mathmagician can now add or subtract, or both. If we had added a division module, we could have added it to the Mathmagician class, and then been able to divide by typing mathmagician.divide.

    More Ruby-on-Rails Articles
    More By James Payne


       · Thanks for stopping by. In this article we discuss how to work with Mixins and...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT