public Method

Module.attr(...)

attr(symbol, writable=false)     nil

Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (@name) and a corresponding access method to read it. If the optional writable argument is true, also creates a method called name= to set the attribute.

module Mod
  attr  :size, true
end

is equivalent to:

module Mod
  def size
    @size
  end
  def size=(val)
    @size = val
  end
end

Source Code

/*
*  call-seq:
*     attr(symbol, writable=false)    => nil
*  
*  Defines a named attribute for this module, where the name is
*  <i>symbol.</i><code>id2name</code>, creating an instance variable
*  (<code>@name</code>) and a corresponding access method to read it.
*  If the optional <i>writable</i> argument is <code>true</code>, also
*  creates a method called <code>name=</code> to set the attribute.
*     
*     module Mod
*       attr  :size, true
*     end
*     
*  <em>is equivalent to:</em>
*     
*     module Mod
*       def size
*         @size
*       end
*       def size=(val)
*         @size = val
*       end
*     end
*/

static VALUE
rb_mod_attr(argc, argv, klass)
   int argc;
   VALUE *argv;
   VALUE klass;
{
   VALUE name, pub;

   rb_scan_args(argc, argv, "11", &name, &pub);
   rb_attr(klass, rb_to_id(name), 1, RTEST(pub), Qtrue);
   return Qnil;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.