converting types

C

Charles Mills

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
 
N

nobu.nokada

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.
 
C

Charles Mills

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.
Thanks!
-Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top