开发者

how to analysis this excerpt of Ruby source code about === method

I found the example and metaphor of Object#=== operatorare too confusing, I'm now begin to read the source code, But I'm new to C, anyone could tell me how to analysis this code:

VALUE
rb_equal(VALUE obj1, VALUE obj2)
{
    VALUE result;

    if (obj1 == obj2)开发者_运维百科 return Qtrue;
    result = rb_funcall(obj1, id_eq, 1, obj2);
    if (RTEST(result)) return Qtrue;
    return Qfalse;
}


VALUE is the generic type of Ruby objects in C (as opposed to the C types like int). From this you can deduce that rb_equal is a function comparing two Ruby objects (obj1 and obj2). If the two objects are equal Qtrue (the represantion of Ruby's true in C) will get returned. If not rb_funcall will call the equality method (id_eq) on obj1. If the result is truthy (checked with RTEST(result)) Qtrue will be returned. If we hit the end of the function the 2 objects are obviously not the same, so we'll return false (Qfalse).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜