bind(p1)
public
Bind umeth to obj. If Klass was the class from which
umeth was obtained, obj.kind_of?(Klass) must be true.
class A
def test
puts "In test, class = #{self.class}"
end
end
class B < A
end
class C < B
end
um = B.instance_method(:test)
bm = um.bind(C.new)
bm.call
bm = um.bind(B.new)
bm.call
bm = um.bind(A.new)
bm.call
produces:
In test, class = C
In test, class = B
prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
from prog.rb:16
Show source
static VALUE
umethod_bind(VALUE method, VALUE recv)
{
struct METHOD *data, *bound;
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
if (data->rclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, data->rclass)) {
if (FL_TEST(data->rclass, FL_SINGLETON)) {
rb_raise(rb_eTypeError,
"singleton method called for a different object");
}
else {
rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
rb_class2name(data->rclass));
}
}
method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
*bound = *data;
if (bound->me.def) bound->me.def->alias_count++;
bound->recv = recv;
bound->rclass = CLASS_OF(recv);
return method;
}