コード例 #1
0
ファイル: kernel.c プロジェクト: kimhmadsen/mruby
/*
 *  call-seq:
 *     obj.instance_variable_get(symbol)    -> obj
 *
 *  Returns the value of the given instance variable, or nil if the
 *  instance variable is not set. The <code>@</code> part of the
 *  variable name should be included for regular instance
 *  variables. Throws a <code>NameError</code> exception if the
 *  supplied symbol is not valid as an instance variable name.
 *
 *     class Fred
 *       def initialize(p1, p2)
 *         @a, @b = p1, p2
 *       end
 *     end
 *     fred = Fred.new('cat', 99)
 *     fred.instance_variable_get(:@a)    #=> "cat"
 *     fred.instance_variable_get("@b")   #=> 99
 */
mrb_value
mrb_obj_ivar_get(mrb_state *mrb, mrb_value self)
{
  mrb_sym id;

  mrb_get_args(mrb, "n", &id);

  check_iv_name(mrb, id);
  return mrb_iv_get(mrb, self, id);
}
コード例 #2
0
ファイル: kernel.c プロジェクト: kimhmadsen/mruby
/*
 *  call-seq:
 *     obj.instance_variable_set(symbol, obj)    -> obj
 *
 *  Sets the instance variable names by <i>symbol</i> to
 *  <i>object</i>, thereby frustrating the efforts of the class's
 *  author to attempt to provide proper encapsulation. The variable
 *  did not have to exist prior to this call.
 *
 *     class Fred
 *       def initialize(p1, p2)
 *         @a, @b = p1, p2
 *       end
 *     end
 *     fred = Fred.new('cat', 99)
 *     fred.instance_variable_set(:@a, 'dog')   #=> "dog"
 *     fred.instance_variable_set(:@c, 'cat')   #=> "cat"
 *     fred.inspect                             #=> "#<Fred:0x401b3da8 @a=\"dog\", @b=99, @c=\"cat\">"
 */
mrb_value
mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
{
  mrb_sym id;
  mrb_value val;

  mrb_get_args(mrb, "no", &id, &val);
  check_iv_name(mrb, id);
  mrb_iv_set(mrb, self, id, val);
  return val;
}
コード例 #3
0
ファイル: kernel.c プロジェクト: kimhmadsen/mruby
/*
 *  call-seq:
 *     obj.instance_variable_defined?(symbol)    -> true or false
 *
 *  Returns <code>true</code> if the given instance variable is
 *  defined in <i>obj</i>.
 *
 *     class Fred
 *       def initialize(p1, p2)
 *         @a, @b = p1, p2
 *       end
 *     end
 *     fred = Fred.new('cat', 99)
 *     fred.instance_variable_defined?(:@a)    #=> true
 *     fred.instance_variable_defined?("@b")   #=> true
 *     fred.instance_variable_defined?("@c")   #=> false
 */
mrb_value
mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self)
{
  mrb_sym mid;

  mrb_get_args(mrb, "n", &mid);
  check_iv_name(mrb, mid);
  if (mrb_obj_iv_defined(mrb, mrb_obj_ptr(self), mid))
    return mrb_true_value();
  return mrb_false_value();
}
コード例 #4
0
ファイル: kernel.c プロジェクト: NARKOZ/mruby
/*
 *  call-seq:
 *     obj.instance_variable_defined?(symbol)    -> true or false
 *
 *  Returns <code>true</code> if the given instance variable is
 *  defined in <i>obj</i>.
 *
 *     class Fred
 *       def initialize(p1, p2)
 *         @a, @b = p1, p2
 *       end
 *     end
 *     fred = Fred.new('cat', 99)
 *     fred.instance_variable_defined?(:@a)    #=> true
 *     fred.instance_variable_defined?("@b")   #=> true
 *     fred.instance_variable_defined?("@c")   #=> false
 */
mrb_value
mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self)
{
  mrb_sym mid;
  mrb_bool defined_p;

  mrb_get_args(mrb, "n", &mid);
  check_iv_name(mrb, mid);
  defined_p = mrb_obj_iv_defined(mrb, mrb_obj_ptr(self), mid);

  return mrb_bool_value(defined_p);
}
コード例 #5
0
ファイル: kernel.c プロジェクト: 8823-scholar/cocos2dx-mruby
/*
 *  call-seq:
 *     obj.remove_instance_variable(symbol)    -> obj
 *
 *  Removes the named instance variable from <i>obj</i>, returning that
 *  variable's value.
 *
 *     class Dummy
 *       attr_reader :var
 *       def initialize
 *         @var = 99
 *       end
 *       def remove
 *         remove_instance_variable(:@var)
 *       end
 *     end
 *     d = Dummy.new
 *     d.var      #=> 99
 *     d.remove   #=> 99
 *     d.var      #=> nil
 */
mrb_value
mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self)
{
  mrb_sym sym;
  mrb_value val;

  mrb_get_args(mrb, "n", &sym);
  check_iv_name(mrb, sym);
  val = mrb_iv_remove(mrb, self, sym);
  if (mrb_undef_p(val)) {
    mrb_name_error(mrb, sym, "instance variable %S not defined", mrb_sym2str(mrb, sym));
  }
  return val;
}
コード例 #6
0
ファイル: kernel.c プロジェクト: Bovi-Li/mruby
static mrb_sym
get_valid_iv_sym(mrb_state *mrb, mrb_value iv_name)
{
  mrb_sym iv_name_id;

  mrb_assert(mrb_symbol_p(iv_name) || mrb_string_p(iv_name));

  if (mrb_string_p(iv_name)) {
    iv_name_id = mrb_intern_cstr(mrb, RSTRING_PTR(iv_name));
    valid_iv_name(mrb, iv_name_id, RSTRING_PTR(iv_name), RSTRING_LEN(iv_name));
  }
  else {
    iv_name_id = mrb_symbol(iv_name);
    check_iv_name(mrb, iv_name_id);
  }

  return iv_name_id;
}