Ok, thanks Guy, I get it - internally it calls rb_num2long(), which in turn tries to call a to_int method. Since the Symbol class defines to_int, I get that result.
I guess it would be nice if we had a macro that raised a TypeError on anything except a Fixnum, Float or Bignum. Otherwise, aren't all C extension writers relegated to doing explicit checks against Symbols (the way rb_ary_ref() does in array.c) if we really want to make our code bulletproof? That, or lots of Check_Type() functions sprinkled throughout our code?
I suppose I just want a version of rb_num2long() that changes the default in the switch statement to something like this:
default:
rb_raise(rb_eTypeError, "not a Fixnum, Float or Bignum");
At Fri, 4 Nov 2005 02:02:35 +0900,
Daniel Berger wrote in [ruby-talk:163946]:
I guess it would be nice if we had a macro that raised a TypeError on anything
except a Fixnum, Float or Bignum. Otherwise, aren't all C extension writers
relegated to doing explicit checks against Symbols (the way rb_ary_ref() does
in array.c) if we really want to make our code bulletproof? That, or lots of
Check_Type() functions sprinkled throughout our code?
An object has :to_int method should be treated as an Integer.
It's duck-typing, isn't it? Symbol no longer has that method
in 1.9.
I understand the duck typing approach. However, I'm curious why
rb_ary_aref() explicitly forbids symbols as arguments. Can you tell me
why? Was there some unexpected side effect?
Thanks,
Dan
···
nobu.nokada@softhome.net wrote:
Hi,
At Fri, 4 Nov 2005 02:02:35 +0900,
Daniel Berger wrote in [ruby-talk:163946]:
> I guess it would be nice if we had a macro that raised a TypeError on anything
> except a Fixnum, Float or Bignum. Otherwise, aren't all C extension writers
> relegated to doing explicit checks against Symbols (the way rb_ary_ref() does
> in array.c) if we really want to make our code bulletproof? That, or lots of
> Check_Type() functions sprinkled throughout our code?
An object has :to_int method should be treated as an Integer.
It's duck-typing, isn't it? Symbol no longer has that method
in 1.9.
At Sat, 5 Nov 2005 09:57:09 +0900,
Daniel Berger wrote in [ruby-talk:164299]:
I understand the duck typing approach. However, I'm curious why
rb_ary_aref() explicitly forbids symbols as arguments. Can you tell me
why? Was there some unexpected side effect?
In older versions, symbols were just integers, so Symbol has
to_int method. But it was considered inappropriate for array
index which is successive. It is like the appendix, and no
longer in 1.9.