APIdock / Ruby
/
method

singleton_method

ruby latest stable - Class: Object
singleton_method(p1)
public

Similar to method, searches singleton method only.

class Demo
 def initialize(n)
 @iv = n
 end
 def hello()
 "Hello, @iv = #{@iv}"
 end
end
k = Demo.new (99)
def k.hi
 "Hi, @iv = #{@iv}"
end
m = k.singleton_method (:hi)
m.call #=> "Hi, @iv = 99"
m = k.singleton_method (:hello) #=> NameError
VALUE
rb_obj_singleton_method(VALUE obj, VALUE vid)
{
 const rb_method_entry_t *me;
 VALUE klass = rb_singleton_class_get(obj);
 ID id = rb_check_id(&vid);
 if (NIL_P(klass) || NIL_P(klass = RCLASS_ORIGIN(klass))) {
 undef:
 rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'",
 obj, vid);
 }
 if (!id) {
 if (respond_to_missing_p(klass, obj, vid, FALSE)) {
 id = rb_intern_str(vid);
 return mnew_missing(klass, obj, id, rb_cMethod);
 }
 goto undef;
 }
 me = rb_method_entry_at(klass, id);
 if (UNDEFINED_METHOD_ENTRY_P(me) ||
 UNDEFINED_REFINED_METHOD_P(me->def)) {
 vid = ID2SYM(id);
 goto undef;
 }
 return mnew_from_me(me, klass, klass, obj, id, rb_cMethod, FALSE);
}

AltStyle によって変換されたページ (->オリジナル) /