public Method

Module.class_variable_set(p1, p2)

obj.class_variable_set(symbol, obj)     obj

Sets the class variable names by symbol to object.

class Fred
  @@foo = 99
  def foo
    @@foo
  end
end

def Fred.foo
  class_variable_set(:@@foo, 101)      #=> 101
end
Fred.foo
Fred.new.foo                             #=> 101

Source Code

/*
*  call-seq:
*     obj.class_variable_set(symbol, obj)    => obj
*  
*  Sets the class variable names by <i>symbol</i> to
*  <i>object</i>.
*     
*     class Fred
*       @@foo = 99
*       def foo
*         @@foo
*       end
*     end
*
*     def Fred.foo
*       class_variable_set(:@@foo, 101)      #=> 101
*     end
*     Fred.foo
*     Fred.new.foo                             #=> 101
*/

static VALUE
rb_mod_cvar_set(obj, iv, val)
   VALUE obj, iv, val;
{
   ID id = rb_to_id(iv);

   if (!rb_is_class_id(id)) {
       rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
   }
   rb_cvar_set(obj, id, val, Qfalse);
   return val;
}
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.