Method: Object#instance_variable_get

Defined in:
object.c

#instance_variable_get(symbol) ⇒ Object #instance_variable_get(string) ⇒ Object

Returns the value of the given instance variable, or nil if the instance variable is not set. The @ part of the variable name should be included for regular instance variables. Throws a NameError exception if the supplied symbol is not valid as an instance variable name. String arguments are converted to symbols.

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

Overloads:

  • #instance_variable_get(symbol) ⇒ Object

    Returns:

  • #instance_variable_get(string) ⇒ Object

    Returns:



2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
# File 'object.c', line 2879

static VALUE
rb_obj_ivar_get(VALUE obj, VALUE iv)
{
    ID id = id_for_var(obj, iv, instance);

    if (!id) {
	return Qnil;
    }
    return rb_ivar_get(obj, id);
}