Project

General

Profile

Feature #10327 ยป bool-falsy-truthy.patch

arimay (yasuhiro arima), 10/15/2014 02:31 PM

View differences:

include/ruby/ruby.h
1540 1540

  
1541 1541
VALUE rb_require(const char*);
1542 1542

  
1543
RUBY_EXTERN VALUE rb_mBool;
1543 1544
RUBY_EXTERN VALUE rb_mKernel;
1544 1545
RUBY_EXTERN VALUE rb_mComparable;
1545 1546
RUBY_EXTERN VALUE rb_mEnumerable;
1546 1547
RUBY_EXTERN VALUE rb_mErrno;
1548
RUBY_EXTERN VALUE rb_mFalsy;
1547 1549
RUBY_EXTERN VALUE rb_mFileTest;
1548 1550
RUBY_EXTERN VALUE rb_mGC;
1549 1551
RUBY_EXTERN VALUE rb_mMath;
1550 1552
RUBY_EXTERN VALUE rb_mProcess;
1553
RUBY_EXTERN VALUE rb_mTruthy;
1551 1554
RUBY_EXTERN VALUE rb_mWaitReadable;
1552 1555
RUBY_EXTERN VALUE rb_mWaitWritable;
1553 1556

  
object.c
33 33
VALUE rb_cData;
34 34

  
35 35
VALUE rb_cNilClass;
36
VALUE rb_mBool;
36 37
VALUE rb_cTrueClass;
37 38
VALUE rb_cFalseClass;
39
VALUE rb_mTruthy;
40
VALUE rb_mFalsy;
38 41

  
39 42
#define id_eq               idEq
40 43
#define id_eql              idEqlP
......
1386 1389
    return Qfalse;
1387 1390
}
1388 1391

  
1392
static VALUE
1393
rb_bool_append_features(VALUE module, VALUE includeto)
1394
{
1395
    if ((includeto != rb_cFalseClass) && (includeto != rb_cTrueClass)) {
1396
        rb_raise(rb_eTypeError, "'%s' isn't FalseClass nor TrueClass",
1397
                 rb_class2name(includeto));
1398
    }
1399

  
1400
    return module;
1401
}
1402

  
1403
/*
1404
 * call-seq:
1405
 *   Falsy === nil              -> true
1406
 *   Falsy === false            -> true
1407
 *   Falsy === true             -> false
1408
 *   Falsy === <anything_else>  -> false
1409
 *
1410
 * Only the objects <i>nil</i> and <i>false</i> respond <code>true</code>.
1411
 * Others respond <code>false</code>.
1412
 */
1413

  
1414
static VALUE
1415
rb_falsy_rtest(VALUE obj1, VALUE obj2)
1416
{
1417
    VALUE result;
1418

  
1419
    result = rb_funcall(obj2, id_eq, 1, Qnil);
1420
    if (RTEST(result)) return Qtrue;
1421

  
1422
    result = rb_funcall(obj2, id_eq, 1, Qfalse);
1423
    if (RTEST(result)) return Qtrue;
1424

  
1425
    return Qfalse;
1426
}
1427

  
1428
/*
1429
 * call-seq:
1430
 *   Truthy === nil              -> false
1431
 *   Truthy === false            -> false
1432
 *   Truthy === true             -> true
1433
 *   Truthy === <anything_else>  -> true
1434
 *
1435
 * Only the objects <i>nil</i> and <i>false</i> respond <code>false</code>.
1436
 * Others respond <code>true</code>.
1437
 */
1438

  
1439
static VALUE
1440
rb_truthy_rtest(VALUE obj1, VALUE obj2)
1441
{
1442
    VALUE result;
1443

  
1444
    result = rb_funcall(obj2, id_eq, 1, Qnil);
1445
    if (!RTEST(result)) {
1446
        result = rb_funcall(obj2, id_eq, 1, Qfalse);
1447
        if (!RTEST(result)) return Qtrue;
1448
    }
1449

  
1450
    return Qfalse;
1451
}
1389 1452

  
1390 1453
/*
1391 1454
 *  call-seq:
......
3445 3508
    rb_cData = rb_define_class("Data", rb_cObject);
3446 3509
    rb_undef_alloc_func(rb_cData);
3447 3510

  
3511
    rb_mBool = rb_define_module("Bool");
3512
    rb_define_singleton_method(rb_mBool, "append_features", rb_bool_append_features, 1);
3513

  
3448 3514
    rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
3515
    rb_include_module(rb_cTrueClass, rb_mBool);
3449 3516
    rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
3450 3517
    rb_define_alias(rb_cTrueClass, "inspect", "to_s");
3451 3518
    rb_define_method(rb_cTrueClass, "&", true_and, 1);
......
3459 3526
    rb_define_global_const("TRUE", Qtrue);
3460 3527

  
3461 3528
    rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
3529
    rb_include_module(rb_cFalseClass, rb_mBool);
3462 3530
    rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
3463 3531
    rb_define_alias(rb_cFalseClass, "inspect", "to_s");
3464 3532
    rb_define_method(rb_cFalseClass, "&", false_and, 1);
......
3470 3538
     * An alias of +false+
3471 3539
     */
3472 3540
    rb_define_global_const("FALSE", Qfalse);
3541

  
3542
    /*
3543
     * Falsy RTEST
3544
     */
3545
    rb_mFalsy = rb_define_module("Falsy");
3546
    rb_define_singleton_method(rb_mFalsy, "===", rb_falsy_rtest, 1);
3547

  
3548
    /*
3549
     * Truthy RTEST
3550
     */
3551
    rb_mTruthy = rb_define_module("Truthy");
3552
    rb_define_singleton_method(rb_mTruthy, "===", rb_truthy_rtest, 1);
3473 3553
}