From: Daniel Berger Date: 2005-11-04T02:02:35+09:00 Subject: Re: Question about NUM2LONG, symbols ts wrote: >>>>>>"D" == Daniel Berger writes: > > > D> Foo.new.test("foo".to_sym) # NUM: 10201 - yikes! > > moulon% cat aa.c > #include "ruby.h" > #include > > static VALUE > aa_to_int(VALUE self) > { > return INT2FIX(12); > } > > static VALUE > aa_test(VALUE self) > { > printf("NUM: %ld\n", NUM2LONG(self)); > > return Qnil; > } > > void Init_aa(){ > VALUE cAa = rb_define_class("Aa", rb_cObject); > rb_define_method(cAa, "test", aa_test, 0); > rb_define_method(cAa, "to_int", aa_to_int, 0); > } > > moulon% > > moulon% ruby -raa -e 'Aa.new.test' > NUM: 12 > moulon% > > > Guy Decoux 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"); Thanks, Dan