Exception: NameError
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::StandardError,
::Exception
|
|
Instance Chain:
self,
::StandardError,
::Exception
|
|
Inherits: |
StandardError
|
Defined in: | error.c, error.c |
Overview
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
.to_tty? | Returns |
Class Method Summary
-
.new([msg, *, name]) ⇒ name_error
constructor
Construct a new
NameError
exception.
::Exception - Inherited
.exception | With no argument, or if the argument is the same as the receiver, return the receiver. |
.new | Construct a new ::Exception object, optionally passing in a message. |
Instance Method Summary
-
#local_variables ⇒ Array
Return a list of the local variable names defined where this
NameError
exception was raised. -
#name ⇒ String?
Return the name associated with this
NameError
exception. -
#receiver ⇒ Object
Return the receiver associated with this
NameError
exception.
::Exception - Inherited
#== | Equality—If obj is not an ::Exception, returns |
#backtrace | Returns any backtrace associated with the exception. |
#backtrace_locations | Returns any backtrace associated with the exception. |
#cause | Returns the previous exception ($!) at the time this exception was raised. |
#exception | With no argument, or if the argument is the same as the receiver, return the receiver. |
#full_message | Returns formatted string of exception. |
#inspect | Return this exception's class name and message. |
#message | Returns the result of invoking |
#set_backtrace | Sets the backtrace information associated with |
#to_s | Returns exception's message (or the name of the exception if no message is set). |
Constructor Details
.new([msg, *, name]) ⇒ name_error
Construct a new NameError
exception. If given the name parameter may subsequently be examined using the #name method.
# File 'error.c', line 1415
static VALUE name_err_initialize(int argc, VALUE *argv, VALUE self) { VALUE name; VALUE iseqw = Qnil; name = (argc > 1) ? argv[--argc] : Qnil; rb_call_super(argc, argv); rb_ivar_set(self, id_name, name); { const rb_execution_context_t *ec = GET_EC(); rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp)); if (cfp) iseqw = rb_iseqw_new(cfp->iseq); } rb_ivar_set(self, id_iseq, iseqw); return self; }
Instance Method Details
#local_variables ⇒ Array
Return a list of the local variable names defined where this NameError
exception was raised.
Internal use only.
# File 'error.c', line 1457
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; }
#name ⇒ String?
Return the name associated with this NameError
exception.
# File 'error.c', line 1441
static VALUE name_err_name(VALUE self) { return rb_attr_get(self, id_name); }
#receiver ⇒ Object
Return the receiver associated with this NameError
exception.
# File 'error.c', line 1645
static VALUE name_err_receiver(VALUE self) { VALUE *ptr, recv, mesg; recv = rb_ivar_lookup(self, id_receiver, 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]; }