Ruby 2.4参考手册
错误 | Errors
NameError
Parent:StandardError
当给定名称无效或未定义时引发。
puts foo
引发了一个例外:
NameError: undefined local variable or method `foo' for main:Object
由于常量名称必须以大写字母开头:
Integer.const_set :answer, 42
引发了一个例外:
NameError: wrong constant name answer
公共类方法
new(msg, *, name) → name_error Show source
构造一个新的NameError异常。如果给定名称参数可能随后使用该NameError.name
方法进行检查。
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);
{
rb_thread_t *th = GET_THREAD();
rb_control_frame_t *cfp =
rb_vm_get_ruby_level_next_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp));
if (cfp) iseqw = rb_iseqw_new(cfp->iseq);
}
rb_ivar_set(self, id_iseq, iseqw);
return self;
}
公共实例方法
local_variables → array Show source
返回在引发此NameError异常的情况下定义的本地变量名称的列表。
仅供内部使用。
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 or nil Show source
返回与此NameError异常关联的名称。
static VALUE
name_err_name(VALUE self)
{
return rb_attr_get(self, id_name);
}
receiver → object Show source
返回与此NameError异常关联的接收者。
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];
}
错误 | Errors相关
Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。
主页 | https://www.ruby-lang.org/ |
源码 | https://github.com/ruby/ruby |
版本 | 2.4 |
发布版本 | 2.4.1 |