converting types

Discussion in 'Ruby' started by Charles Mills, Jul 15, 2004.

  1. 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
     
    Charles Mills, Jul 15, 2004
    #1
    1. Advertisements

  2. Charles Mills

    nobu.nokada Guest

    Hi,

    At Fri, 16 Jul 2004 06:37:09 +0900,
    Charles Mills wrote in [ruby-talk:106579]:
    There's StringValue() for String, and rb_convert_type() for
    generic types. Note that StringValue() requires lvalue as the
    argument.
     
    nobu.nokada, Jul 15, 2004
    #2
    1. Advertisements

  3. Thanks!
    -Charlie
     
    Charles Mills, Jul 15, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.