Exception: NameError

Relationships & Source Files
Namespace Children
Extension / Inclusion / Inheritance Descendants
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: StandardError
Defined in: error.c,


Raised when a given name is invalid or undefined.

puts foo

raises the exception:

NameError: undefined local variable or method `foo' for main:Object

Since constant names must start with a capital:

Integer.const_set :answer, 42

raises the exception:

NameError: wrong constant name answer

Class Attribute Summary

::Exception - Inherited


Returns true if exception messages will be sent to a tty.

Class Method Summary

::Exception - Inherited


With no argument, or if the argument is the same as the receiver, return the receiver.


Construct a new ::Exception object, optionally passing in a message.

Instance Method Summary

::Exception - Inherited


Equality—If obj is not an ::Exception, returns false.


Returns any backtrace associated with the exception.


Returns any backtrace associated with the exception.


Returns the previous exception ($!) at the time this exception was raised.


With no argument, or if the argument is the same as the receiver, return the receiver.


Returns formatted string of exception.


Return this exception's class name and message.


Returns the result of invoking exception.to_s.


Sets the backtrace information associated with exc.


Returns exception's message (or the name of the exception if no message is set).

Constructor Details

.new(msg = nil, name = nil, receiver:nil) ⇒ name_error

Construct a new NameError exception. If given the name parameter may subsequently be examined using the #name method. receiver parameter allows to pass object in context of which the error happened. Example:

[1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
[1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
[ GitHub ]

# File 'error.c', line 1454

static VALUE
name_err_initialize(int argc, VALUE *argv, VALUE self)
    ID keywords[1];
    VALUE values[numberof(keywords)], name, options;

    argc = rb_scan_args(argc, argv, "*:", NULL, &options);
    keywords[0] = id_receiver;
    rb_get_kwargs(options, keywords, 0, numberof(values), values);
    name = (argc > 1) ? argv[--argc] : Qnil;
    rb_call_super(argc, argv);
    name_err_init_attr(self, values[0], name);
    return self;

Instance Method Details


Return a list of the local variable names defined where this NameError exception was raised.

Internal use only.

[ GitHub ]

# File 'error.c', line 1506

static VALUE
name_err_local_variables(VALUE self)
    VALUE vars = rb_attr_get(self, id_local_variables);

    if (NIL_P(vars)) {
	VALUE iseqw = rb_attr_get(self, id_iseq);
	if (!NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
	if (NIL_P(vars)) vars = rb_ary_new();
	rb_ivar_set(self, id_local_variables, vars);
    return vars;


Return the name associated with this NameError exception.

[ GitHub ]

# File 'error.c', line 1490

static VALUE
name_err_name(VALUE self)
    return rb_attr_get(self, id_name);


Return the receiver associated with this NameError exception.

[ GitHub ]

# File 'error.c', line 1706

static VALUE
name_err_receiver(VALUE self)
    VALUE *ptr, recv, mesg;

    recv = rb_ivar_lookup(self, id_recv, Qundef);
    if (recv != Qundef) return recv;

    mesg = rb_attr_get(self, id_mesg);
    if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
	rb_raise(rb_eArgError, "no receiver is available");
    ptr = DATA_PTR(mesg);
    return ptr[NAME_ERR_MESG__RECV];