在线文档教程
Ruby 2.4

Method

类方法

Parent:Object

Method

公共实例方法

meth == other_meth → true or false Show source

如果两个方法对象绑定到同一个对象并引用相同的方法定义,并且它们的所有者是相同的类或模块,则它们是相等的。

static VALUE method_eq(VALUE method, VALUE other) { struct METHOD *m1, *m2; VALUE klass1, klass2; if (!rb_obj_is_method(other)) return Qfalse; if (CLASS_OF(method) != CLASS_OF(other)) return Qfalse; Check_TypedStruct(method, &method_data_type m1 = (struct METHOD *)DATA_PTR(method m2 = (struct METHOD *)DATA_PTR(other klass1 = method_entry_defined_class(m1->me klass2 = method_entry_defined_class(m2->me if (!rb_method_entry_eq(m1->me, m2->me) || klass1 != klass2 || m1->klass != m2->klass || m1->recv != m2->recv) { return Qfalse; } return Qtrue; }

prcparams,... → obj Show source

调用该块,使用接近方法调用语义的东西将块的参数设置为params中的值。返回块中最后一个表达式的值。

a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } } a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc.(9, 1, 2, 3) #=> [9, 18, 27] a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]

请注意,prc。()使用给定的参数调用prc.call()。 它是隐藏“呼叫”的语法糖。

对于使用lambda或 - >()创建的procs,如果将错误的参数数量传递给proc,则会生成一个错误。 对于使用Proc.new或Kernel.proc创建的procs,额外的参数将被静默丢弃,缺少的参数将被设置为nil。

a_proc = proc {|a,b| [a,b] } a_proc.call(1) #=> [1, nil] a_proc = lambda {|a,b| [a,b] } a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)

另请参阅Proc#lambda ?.

VALUE rb_method_call(int argc, const VALUE *argv, VALUE method) { VALUE procval = rb_block_given_p() ? rb_block_proc() : Qnil; return rb_method_call_with_block(argc, argv, method, procval }

arity → integer Show source

返回一个方法接受的参数个数的指示。返回具有固定数量参数的方法的非负整数。对于采用可变数量参数的Ruby方法,返回-n-1,其中n是所需参数的数量。对于用C编写的方法,如果调用需要可变数量的参数,则返回-1。

class C def one; end def two(a end def three(*a end def four(a, b end def five(a, b, *c end def six(a, b, *c, &d end end c = C.new c.method(:one).arity #=> 0 c.method(:two).arity #=> 1 c.method(:three).arity #=> -1 c.method(:four).arity #=> 2 c.method(:five).arity #=> -3 c.method(:six).arity #=> -3 "cat".method(:size).arity #=> 0 "cat".method(:replace).arity #=> 1 "cat".method(:squeeze).arity #=> -1 "cat".method(:count).arity #=> -1

static VALUE method_arity_m(VALUE method) { int n = method_arity(method return INT2FIX(n }

call(params,...) → obj Show source

调用该块,使用接近方法调用语义的东西将块的参数设置为params中的值。返回块中最后一个表达式的值。

a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } } a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc.(9, 1, 2, 3) #=> [9, 18, 27] a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]

请注意,prc。()使用给定的参数调用prc.call()。 它是隐藏“call”的语法糖。

对于使用lambda或 - >()创建的procs,如果将错误的参数数量传递给proc,则会生成一个错误。 对于使用Proc.new或Kernel.proc创建的procs,额外的参数将被静默丢弃,缺少的参数将被设置为nil。

a_proc = proc {|a,b| [a,b] } a_proc.call(1) #=> [1, nil] a_proc = lambda {|a,b| [a,b] } a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)

另请参阅Proc#lambda ?.

VALUE rb_method_call(int argc, const VALUE *argv, VALUE method) { VALUE procval = rb_block_given_p() ? rb_block_proc() : Qnil; return rb_method_call_with_block(argc, argv, method, procval }

clone → new_method Show source

返回此方法的克隆方法。

class A def foo return "bar" end end m = A.new.method(:foo) m.call # => "bar" n = m.clone.call # => "bar"

static VALUE method_clone(VALUE self) { VALUE clone; struct METHOD *orig, *data; TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data CLONESETUP(clone, self RB_OBJ_WRITE(clone, &data->recv, orig->recv RB_OBJ_WRITE(clone, &data->klass, orig->klass RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me) return clone; }

curry → proc Show source

curry(arity) → proc

基于该方法返回一个curried过程。当proc被调用的参数数量低于方法的参数时,则返回另一个curried proc。只有当提供足够的参数来满足方法签记时,方法才会被调用。

当使用可变参数来压缩方法以确定在调用方法之前需要多少个参数时,应该提供可选的arity参数。

def foo(a,b,c) [a, b, c] end proc = self.method(:foo).curry proc2 = proc.call(1, 2) #=> #<Proc> proc2.call(3) #=> [1,2,3] def vararg(*args) args end proc = self.method(:vararg).curry(4) proc2 = proc.call(:x) #=> #<Proc> proc3 = proc2.call(:y, :z) #=> #<Proc> proc3.call(:a) #=> [:x, :y, :z, :a]

static VALUE rb_method_curry(int argc, const VALUE *argv, VALUE self) { VALUE proc = method_to_proc(self return proc_curry(argc, argv, proc }

eql?(other_meth) → true or false Show source

如果两个方法对象绑定到同一个对象并引用相同的方法定义,并且它们的所有者是相同的类或模块,则它们是相等的。

static VALUE method_eq(VALUE method, VALUE other) { struct METHOD *m1, *m2; VALUE klass1, klass2; if (!rb_obj_is_method(other)) return Qfalse; if (CLASS_OF(method) != CLASS_OF(other)) return Qfalse; Check_TypedStruct(method, &method_data_type m1 = (struct METHOD *)DATA_PTR(method m2 = (struct METHOD *)DATA_PTR(other klass1 = method_entry_defined_class(m1->me klass2 = method_entry_defined_class(m2->me if (!rb_method_entry_eq(m1->me, m2->me) || klass1 != klass2 || m1->klass != m2->klass || m1->recv != m2->recv) { return Qfalse; } return Qtrue; }

hash → integer Show source

返回与方法对象相对应的哈希值。

另请参阅 Object#hash。

static VALUE method_hash(VALUE method) { struct METHOD *m; st_index_t hash; TypedData_Get_Struct(method, struct METHOD, &method_data_type, m hash = rb_hash_start((st_index_t)m->recv hash = rb_hash_method_entry(hash, m->me hash = rb_hash_end(hash return INT2FIX(hash }

inspect → string Show source

返回基础方法的名称。

"cat".method(:count).inspect #=> "#<Method: String#count>"

static VALUE method_inspect(VALUE method) { struct METHOD *data; VALUE str; const char *s; const char *sharp = "#"; VALUE mklass; VALUE defined_class; TypedData_Get_Struct(method, struct METHOD, &method_data_type, data str = rb_str_buf_new2("#<" s = rb_obj_classname(method rb_str_buf_cat2(str, s rb_str_buf_cat2(str, ": " mklass = data->klass; if (data->me->def->type == VM_METHOD_TYPE_ALIAS) { defined_class = data->me->def->body.alias.original_me->owner; } else { defined_class = method_entry_defined_class(data->me } if (RB_TYPE_P(defined_class, T_ICLASS)) { defined_class = RBASIC_CLASS(defined_class } if (FL_TEST(mklass, FL_SINGLETON)) { VALUE v = rb_ivar_get(mklass, attached if (data->recv == Qundef) { rb_str_buf_append(str, rb_inspect(mklass) } else if (data->recv == v) { rb_str_buf_append(str, rb_inspect(v) sharp = "."; } else { rb_str_buf_append(str, rb_inspect(data->recv) rb_str_buf_cat2(str, "(" rb_str_buf_append(str, rb_inspect(v) rb_str_buf_cat2(str, ")" sharp = "."; } } else { rb_str_buf_append(str, rb_class_name(mklass) if (defined_class != mklass) { rb_str_buf_cat2(str, "(" rb_str_buf_append(str, rb_class_name(defined_class) rb_str_buf_cat2(str, ")" } } rb_str_buf_cat2(str, sharp rb_str_append(str, rb_id2str(data->me->called_id) if (data->me->called_id != data->me->def->original_id) { rb_str_catf(str, "(%"PRIsVALUE")", rb_id2str(data->me->def->original_id) } if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) { rb_str_buf_cat2(str, " (not-implemented)" } rb_str_buf_cat2(str, ">" return str; }

name → symbol Show source

返回方法的名称。

static VALUE method_name(VALUE obj) { struct METHOD *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data return ID2SYM(data->me->called_id }

original_name → symbol Show source

返回方法的原始名称。

static VALUE method_original_name(VALUE obj) { struct METHOD *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data return ID2SYM(data->me->def->original_id }

owner → class_or_module Show source

返回定义该方法的类或模块。

static VALUE method_owner(VALUE obj) { struct METHOD *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data return data->me->owner; }

parameters → array Show source

返回此方法的参数信息。

def foo(bar end method(:foo).parameters #=> [[:req, :bar]] def foo(bar, baz, bat, &blk end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]] def foo(bar, *args end method(:foo).parameters #=> [[:req, :bar], [:rest, :args]] def foo(bar, baz, *args, &blk end method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]

static VALUE rb_method_parameters(VALUE method) { const rb_iseq_t *iseq = rb_method_iseq(method if (!iseq) { return unnamed_parameters(method_arity(method) } return rb_iseq_parameters(iseq, 0 }

receiver → object Show source

返回方法对象的绑定接收者。

static VALUE method_receiver(VALUE obj) { struct METHOD *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data return data->recv; }

source_location → String, Integer()

返回包含此方法的Ruby源文件名和行号,如果此方法未在Ruby中定义(即本机),则返回nil。

VALUE rb_method_location(VALUE method) { return method_def_location(method_def(method) }

super_method → method Show source

返回当使用super时会调用的超类的方法,或者如果超类没有方法,则返回nil。

static VALUE method_super_method(VALUE method) { const struct METHOD *data; VALUE super_class; const rb_method_entry_t *me; TypedData_Get_Struct(method, struct METHOD, &method_data_type, data super_class = RCLASS_SUPER(method_entry_defined_class(data->me) if (!super_class) return Qnil; me = (rb_method_entry_t *)rb_callable_method_entry_without_refinements(super_class, data->me->called_id if (!me) return Qnil; return mnew_internal(me, super_class, data->recv, data->me->called_id, rb_obj_class(method), FALSE, FALSE }

to_proc → proc Show source

返回与此方法相对应的Proc对象。

static VALUE method_to_proc(VALUE method) { VALUE procval; rb_proc_t *proc; /* * class Method * def to_proc * lambda{|*args| * self.call(*args) * } * end * end */ procval = rb_iterate(mlambda, 0, bmcall, method GetProcPtr(procval, proc proc->is_from_method = 1; return procval; }

to_s → string Show source

返回基础方法的名称。

"cat".method(:count).inspect #=> "#<Method: String#count>"

static VALUE method_inspect(VALUE method) { struct METHOD *data; VALUE str; const char *s; const char *sharp = "#"; VALUE mklass; VALUE defined_class; TypedData_Get_Struct(method, struct METHOD, &method_data_type, data str = rb_str_buf_new2("#<" s = rb_obj_classname(method rb_str_buf_cat2(str, s rb_str_buf_cat2(str, ": " mklass = data->klass; if (data->me->def->type == VM_METHOD_TYPE_ALIAS) { defined_class = data->me->def->body.alias.original_me->owner; } else { defined_class = method_entry_defined_class(data->me } if (RB_TYPE_P(defined_class, T_ICLASS)) { defined_class = RBASIC_CLASS(defined_class } if (FL_TEST(mklass, FL_SINGLETON)) { VALUE v = rb_ivar_get(mklass, attached if (data->recv == Qundef) { rb_str_buf_append(str, rb_inspect(mklass) } else if (data->recv == v) { rb_str_buf_append(str, rb_inspect(v) sharp = "."; } else { rb_str_buf_append(str, rb_inspect(data->recv) rb_str_buf_cat2(str, "(" rb_str_buf_append(str, rb_inspect(v) rb_str_buf_cat2(str, ")" sharp = "."; } } else { rb_str_buf_append(str, rb_class_name(mklass) if (defined_class != mklass) { rb_str_buf_cat2(str, "(" rb_str_buf_append(str, rb_class_name(defined_class) rb_str_buf_cat2(str, ")" } } rb_str_buf_cat2(str, sharp rb_str_append(str, rb_id2str(data->me->called_id) if (data->me->called_id != data->me->def->original_id) { rb_str_catf(str, "(%"PRIsVALUE")", rb_id2str(data->me->def->original_id) } if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) { rb_str_buf_cat2(str, " (not-implemented)" } rb_str_buf_cat2(str, ">" return str; }

unbind → unbound_method Show source

从目前的接收器中分离meth。 随后可以将得到的UnboundMethod绑定到同一个类的新对象(请参阅UnboundMethod)。

static VALUE method_unbind(VALUE obj) { VALUE method; struct METHOD *orig, *data; TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig method = TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD, &method_data_type, data RB_OBJ_WRITE(method, &data->recv, Qundef RB_OBJ_WRITE(method, &data->klass, orig->klass RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me) OBJ_INFECT(method, obj return method; }