Project

General

Profile

Feature #11848 ยป to_b_method.diff

View differences:

numeric.c
}
/*
* call-seq:
* num.to_b -> true or false
*
* Returns +false+ if +num+ is zero, +true+ otherwise.
*/
static VALUE
num_to_b(VALUE num)
{
if (rb_funcallv(num, rb_intern("zero?"), 0, 0)) {
return Qfalse;
}
return Qtrue;
}
/*
* call-seq:
* num.positive? -> true or false
*
......
rb_define_method(rb_cNumeric, "abs", num_abs, 0);
rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
rb_define_method(rb_cNumeric, "to_b", num_to_b, 0);
rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
object.c
return rb_usascii_str_new2("nil");
}
/*
* call-seq:
* nil.to_b -> false
*
* Always returns <code>false</false>.
*
* nil.to_b #=> false
*/
static VALUE
nil_to_b(VALUE obj)
{
return Qfalse;
}
/***********************************************************************
* Document-class: TrueClass
*
......
/*
* call-seq:
* true.to_b -> true
*
* Returns <code>true</code>.
*/
static VALUE
true_to_b(VALUE obj)
{
return Qtrue;
}
/*
* Document-class: FalseClass
*
* The global value <code>false</code> is the only instance of class
......
return RTEST(obj2)?Qtrue:Qfalse;
}
/*
* call-seq:
* false.to_b -> false
*
* Returns <code>false</code>.
*/
static VALUE
false_to_b(VALUE obj)
{
return Qfalse;
}
/*
* call-seq:
* nil.nil? -> true
......
rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
rb_define_method(rb_cNilClass, "to_b", nil_to_b, 0);
rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
rb_define_method(rb_cNilClass, "&", false_and, 1);
rb_define_method(rb_cNilClass, "|", false_or, 1);
......
rb_undef_alloc_func(rb_cData);
rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
rb_define_method(rb_cTrueClass, "to_b", true_to_b, 0);
rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
rb_define_alias(rb_cTrueClass, "inspect", "to_s");
rb_define_method(rb_cTrueClass, "&", true_and, 1);
......
rb_define_global_const("TRUE", Qtrue);
rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
rb_define_method(rb_cFalseClass, "to_b", false_to_b, 0);
rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
rb_define_alias(rb_cFalseClass, "inspect", "to_s");
rb_define_method(rb_cFalseClass, "&", false_and, 1);
string.c
return str;
}
/* "trues" array contains the valid values to convert String to TrueClass. */
#define STRING_TRUES_SIZE 6
static const char* trues[STRING_TRUES_SIZE] = {"t", "true", "on", "y", "yes", "1"};
/*
* call-seq:
* str.to_b -> true or false
*
* Returns <code>true<code> if <code>str</code> is one of "t", "true", "on", "y", "yes" or "1" values.
* Returns <code>false<code> otherwise.
* <code>to_b</code> method ignores trailing spaces and letter cases.
*/
static VALUE
rb_str_to_b(VALUE str)
{
int i;
for (i = 0; i < STRING_TRUES_SIZE; i++) {
str = rb_str_downcase(rb_str_strip(str));
if (strcmp(RSTRING_PTR(str), trues[i]) == 0) {
return Qtrue;
}
}
return Qfalse;
}
/**********************************************************************
* Document-class: Symbol
*
......
/*
* call-seq:
* sym.to_b -> true or false
*
* Same as <code>sym.to_s.to_b</code>.
*/
static VALUE
rb_sym_to_b(VALUE sym)
{
return rb_str_to_b(rb_sym2str(sym));
}
/*
* call-seq:
* sym.id2name -> string
* sym.to_s -> string
......
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
rb_define_method(rb_cString, "to_b", rb_str_to_b, 0);
rb_define_method(rb_cString, "to_s", rb_str_to_s, 0);
rb_define_method(rb_cString, "to_str", rb_str_to_s, 0);
rb_define_method(rb_cString, "inspect", rb_str_inspect, 0);
......
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
rb_define_method(rb_cSymbol, "to_b", rb_sym_to_b, 0);
rb_define_method(rb_cSymbol, "to_s", rb_sym_to_s, 0);
rb_define_method(rb_cSymbol, "id2name", rb_sym_to_s, 0);
rb_define_method(rb_cSymbol, "intern", sym_to_sym, 0);
test/ruby/test_bignum.rb
end
assert_equal(T1024 ^ 10, T1024 ^ obj)
end
def test_to_b
assert_predicate(1111_1111_1111_1111_1111_1111_1111_1111, :to_b)
assert_predicate(-1111_1111_1111_1111_1111_1111_1111_1111, :to_b)
end
end
test/ruby/test_float.rb
h = {0.0 => bug10979}
assert_equal(bug10979, h[-0.0])
end
def test_to_b
assert_not_predicate(0.0, :to_b)
assert_predicate(1.0, :to_b)
assert_predicate(2.0, :to_b)
assert_predicate(-1.0, :to_b)
assert_predicate(-2.0, :to_b)
end
end
test/ruby/test_integer.rb
assert_equal(i+1, (n+1).bit_length, "#{n+1}.bit_length")
}
end
def test_to_b
assert_not_predicate(0, :to_b)
assert_predicate(1, :to_b)
assert_predicate(2, :to_b)
assert_predicate(-1, :to_b)
assert_predicate(-2, :to_b)
end
end
test/ruby/test_integer_comb.rb
end
}
end
def test_to_b
VS.each do |a|
if a == 0
assert_equal(false, a.to_b, "(#{a}).to_b")
else
assert_equal(true, a.to_b, "(#{a}).to_b")
end
end
end
end
test/ruby/test_object.rb
num.times {a.clone.set}
end;
end
def test_nil_to_b
assert_not_predicate(nil, :to_b)
end
def test_true_to_b
assert_predicate(true, :to_b)
end
def test_false_to_b
assert_not_predicate(false, :to_b)
end
end
test/ruby/test_string.rb
assert_not_equal(str.object_id, (+str).object_id)
assert_equal(str.object_id, (-str).object_id)
end
def test_to_b
[ '1', '1 ', ' 1', ' 1 ',
't', 't ', ' t', ' t ',
'T', 'T ', ' T', ' T ',
'true', 'true ', ' true', ' true ',
'TRUE', 'TRUE ', ' TRUE', ' TRUE ',
'on', 'on ', ' on', ' on ',
'ON', 'ON ', ' ON ', ' ON ',
'y', 'y ', ' y', ' y ',
'Y', 'Y ', ' Y', ' Y ',
'yes', 'yes ', ' yes', ' yes ',
'YES', 'YES ', ' YES', ' YES '
].each do |value|
assert_predicate(value, :to_b)
end
[ '',
'0',
'2', '2 ', ' 2', ' 2 ',
'-1', '-1 ', ' -1', ' -1 ',
'-2', '-2 ', ' -2', ' -2 ',
'f', 'F',
'false', 'FALSE',
'off', 'OFF',
'n', 'N',
'no', 'NO',
'not', 'NOT',
'wherever'
].each do |value|
assert_not_predicate(value, :to_b)
end
end
end
class TestString2 < TestString
test/ruby/test_symbol.rb
assert_equal str, str.to_sym.to_s
assert_not_predicate(str, :frozen?, bug11721)
end
def test_to_b
[ :'1',
:t, :T,
:true, :TRUE,
:on, :ON,
:y, :Y,
:yes, :YES,
].each do |value|
assert_predicate(value, :to_b)
end
[ :'',
:'0', :'2', :'-1', :'-2',
:f, :F,
:false, :FALSE,
:off, :OFF,
:n, :N,
:no, :NO,
:not, :NOT,
:wherever
].each do |value|
assert_not_predicate(value, :to_b)
end
end
end
    (1-1/1)