Sunday, July 02, 2006

Ruby Miscellanea

This is a Work-In-Progress and will cover Ruby Miscellanea topics but relevant to Ruby on Rails.
  1. More on Constants:
    • Constants defined within a class or module may be accessed anywhere within the class or module.
    • Outside the class or module, they may be accessed using the scope operator, :: prefixed by an expression that returns the appropriate class or module.
    • Constants defined outside any class or module may be accessed as it is or by using the scope operator with no prefix.
    • Constants may not be defined in methods.
    • Constants may be added to existing classes and modules from the outside by using the class or module name and the scope operator before the constant name. The program Const.rb shows all of this:
      OUTER_CONST = 99

      class Const
      def get_const
      CONST
      end
      CONST = OUTER_CONST + 1
      end

      puts Const.new.get_const
      puts Const::CONST
      puts ::OUTER_CONST
      puts Const::NEW_CONST = 123
  2. Modules
    Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions. Unlike classes, you cannot create objects based on modules.

    Modules serve two purposes:
    • First they act as namespace, letting you define methods whose names will not clash with those defined elsewhere. The examples MyTrig.rb, MyMoral.rb and UseModule.rb illustrates this.
      # MyTrig.rb
      module Trig
      PI = 3.1416
      # class methods
      def Trig.sin(x)
      # ...
      end
      def Trig.cos(x)
      # ...
      end
      end

      # MyMoral.rb
      module Moral
      VERY_BAD = 0
      BAD = 1
      def Moral.sin(badness)
      # ...
      end
      end

      # UseModule.rb
      require 'MyTrig'
      require 'MyMoral'
      y = Trig.sin(Trig::PI/4)
      wrongdoing = Moral.sin(Moral::VERY_BAD)
    • Second, they allow you to share functionality between classes - if a class mixes in a module, that module’s instance methods become available as if they had been defined in the class. They get mixed in. The program Mixins.rb illustrates this:
      # Mixins.rb
      module D
      def initialize(name)
      @name =name
      end
      def to_s
      @name
      end
      end

      module Debug
      include D
      # Methods that act as queries are often
      # named with a trailing ?
      def who_am_i?
      "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
      end
      end

      class Phonograph
      # the include statement simply makes a reference to a named module
      # If that module is in a separate file, use require to drag the file in
      # before using include
      include Debug
      # ...
      end

      class EightTrack
      include Debug
      # ...
      end

      ph = Phonograph.new("West End Blues")
      et = EightTrack.new("Real Pillow")
      puts ph.who_am_i?
      puts et.who_am_i?

  3. Symbols
    A symbol looks like a variable name but it’s prefixed with a colon. Examples: :action, :line_items. You can think of symbols as string literals that are magically turned into constants. Alternatively, you can consider the colon to mean "thing named" so :id is "the thing named id." You can also think of :id as meaning the name of the variable id, and plain id as meaning the value of the variable.

First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home