There appears to be a "feature" in ruby (1.8) in String#oct. Namely,
it'll actually parse ANY base. It seems that the base passed to
rb_str_to_inum should be 8, not -8:
static VALUE
rb_str_oct(str)
VALUE str;
{
return rb_str_to_inum(str, -8, Qfalse);
}
static VALUE
rb_str_hex(str)
VALUE str;
{
return rb_str_to_inum(str, 16, Qfalse);
}
% irb
>> 0b101010 # baseline
=> 42
>> "0b101010".hex
=> 185602064 # as expected
>> 0x0b101010
=> 185602064
>> "0b101010".oct
=> 42 # binary parser?
By switching from -8 to 8, we get what I'd expect to get:
% ./ruby -e 'p "0b101010".oct'
0
UPDATE: apparently this was discussed in July and it IS intentional.
Why? I would think this is good behavior for String.to_i, but I don't
understand it at all in #oct. If it doesn't belong in #hex, why does
it belong in #oct? Also, why unmentioned in rdoc if intentional?