Question about #coerce

Discussion in 'Ruby' started by Jim Cain, Jul 9, 2003.

  1. Jim Cain

    Jim Cain Guest

    This is my understanding of the customary use of coerce: it is a request
    to convert the receiver into the same type as the argument:

    x.coerce(y)

    is asking x to return a copy of itself that is the same class as y. I
    understand that the return value is an array: [y1, x1].

    What if x cannot be represented in the same class as y, but both could
    be represented in a third class? Is it acceptable to return [y1, x1]
    such that both objects have been rendered in a third class?

    What if nothing can be done? Is it customary to raise an exception, or
    return something like [y, nil]?

    I'm working on implementing #coerce in my Oracle library, and I want to
    make sure I do it the Ruby Way as much as possible.
     
    Jim Cain, Jul 9, 2003
    #1
    1. Advertising

  2. Hi,

    In message "Question about #coerce"
    on 03/07/09, Jim Cain <> writes:

    |What if x cannot be represented in the same class as y, but both could
    |be represented in a third class? Is it acceptable to return [y1, x1]
    |such that both objects have been rendered in a third class?

    Acceptable. It's only reason to return converted y1 *and* x1 from
    coerce, although no known class does that yet.

    |What if nothing can be done? Is it customary to raise an exception, or
    |return something like [y, nil]?

    Raise TypeError exception.

    matz.
     
    Yukihiro Matsumoto, Jul 9, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Harris
    Replies:
    4
    Views:
    1,459
    James Harris
    Oct 9, 2003
  2. Matthew Wilson
    Replies:
    8
    Views:
    302
    vbgunz
    Oct 2, 2006
  3. Alan Isaac

    disappearing documentation of `coerce`

    Alan Isaac, Jul 5, 2007, in forum: Python
    Replies:
    2
    Views:
    281
    Alan Isaac
    Jul 5, 2007
  4. Mark Tarver

    how to coerce to a string in Python?

    Mark Tarver, Apr 28, 2009, in forum: Python
    Replies:
    3
    Views:
    1,408
    Paul Rubin
    Apr 29, 2009
  5. John Fletcher

    Problem with coerce and SWIG

    John Fletcher, Jul 7, 2003, in forum: Ruby
    Replies:
    1
    Views:
    109
    John Fletcher
    Jul 8, 2003
Loading...

Share This Page