Exception: NameError
- Inherits:
-
StandardError
- Object
- Exception
- StandardError
- NameError
- 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
Direct Known Subclasses
Defined Under Namespace
Classes: message
Instance Method Summary collapse
-
#new(msg = nil, name = nil, receiver: nil) ⇒ Object
constructor
Construct a new NameError exception.
-
#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.
Methods inherited from Exception
#==, #backtrace, #backtrace_locations, #cause, #detailed_message, #exception, exception, #full_message, #inspect, #message, #set_backtrace, #to_s, to_tty?
Constructor Details
#new(msg = nil, name = nil, receiver: nil) ⇒ Object
Construct a new NameError exception. If given the name parameter may subsequently be examined using the NameError#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]
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 |
# File 'error.c', line 2400 static VALUE name_err_initialize(int argc, VALUE *argv, VALUE self) { ID keywords[1]; VALUE values[numberof(keywords)], name, ; argc = rb_scan_args(argc, argv, "*:", NULL, &); keywords[0] = id_receiver; rb_get_kwargs(, 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
#local_variables ⇒ Array
Return a list of the local variable names defined where this NameError exception was raised.
Internal use only.
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 |
# File 'error.c', line 2454 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.
2438 2439 2440 2441 2442 |
# File 'error.c', line 2438 static VALUE name_err_name(VALUE self) { return rb_attr_get(self, id_name); } |
#receiver ⇒ Object
Return the receiver associated with this NameError exception.
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 |
# File 'error.c', line 2739 static VALUE name_err_receiver(VALUE self) { VALUE recv = rb_ivar_lookup(self, id_recv, Qundef); if (!UNDEF_P(recv)) return recv; VALUE 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; TypedData_Get_Struct(mesg, , &name_err_mesg_data_type, ptr); return ptr->recv; } |