Converting types

I am working on a Ruby extension and want to support "duck typing".
I have read through the source code for rb_Integer(), rb_Float(), rb_String(), and rb_Array() a few times now and am wondering if there are stricter versions of these functions.
For example I want to use a type as an integer only if it has a "to_int" method or is already a Fixnum or Bignum, otherwise raise an error. I was hoping to do the same thing using "to_str" for Strings and "to_ary" for Arrays. The rb_String() function will turn most objects into Strings (Files for example) and in those cases I want my code to raise an error.
Looking at the innards of the conversion functions it looks like all the machinery for creating your own conversion functions is there, but if they already exist even better.
-Charlie

Hi,

At Fri, 16 Jul 2004 06:37:09 +0900,
Charles Mills wrote in [ruby-talk:106579]:

For example I want to use a type as an integer only if it has a
"to_int" method or is already a Fixnum or Bignum, otherwise raise an
error. I was hoping to do the same thing using "to_str" for Strings
and "to_ary" for Arrays. The rb_String() function will turn most
objects into Strings (Files for example) and in those cases I want my
code to raise an error.

There's StringValue() for String, and rb_convert_type() for
generic types. Note that StringValue() requires lvalue as the
argument.

···

--
Nobu Nakada

Thanks!
-Charlie

···

On Jul 15, 2004, at 2:49 PM, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 16 Jul 2004 06:37:09 +0900,
Charles Mills wrote in [ruby-talk:106579]:

For example I want to use a type as an integer only if it has a
"to_int" method or is already a Fixnum or Bignum, otherwise raise an
error. I was hoping to do the same thing using "to_str" for Strings
and "to_ary" for Arrays. The rb_String() function will turn most
objects into Strings (Files for example) and in those cases I want my
code to raise an error.

There's StringValue() for String, and rb_convert_type() for
generic types. Note that StringValue() requires lvalue as the
argument.

--
Nobu Nakada