3

Defining to_s on a class works as I expect:

class Foo
  def to_s
    "Joe"
  end 
end

f = Foo.new
"hello #{f}" # => "hello Joe"

I attempt to utilize to_i and expect it to work in a similar fashion. I expect that, by defining to_i to be a number, then anywhere a number is expected, the object will return that number in the place of the object; in a situation like the following, I expect it to return the integer 5. Instead, it raises an error:

class Foo
  def to_i
    0
  end
end

f = Foo.new
5 + f # => TypeError: Foo can't be coerced into Fixnum

What does defining to_i enable? How do you utilize it? Can I implicitly represent this object as an integer and return 0 just like the object implicitly returns the string "Joe"?

4
  • 2
    This will work if you implement to_int, see stackoverflow.com/questions/11182052/…
    – matt
    Oct 12, 2015 at 1:57
  • @matt would you mind elaborating a bit more? I did this: def to_int; 0; end and it errored out just the same as the to_i definition above.
    – Neil
    Oct 12, 2015 at 2:04
  • 1
    @matt: No, it's coerce: class Foo; def coerce(n) [0, n] end end.
    – cremno
    Oct 12, 2015 at 3:48
  • @cremno Yes, you’re right, sorry. to_int will work for other things like using it as an array index, but you need coerce for arithmetic.
    – matt
    Oct 12, 2015 at 12:55

1 Answer 1

2

The crucial difference between the two cases is that string interpolation "#{}" calls to_s implicitly while Fixnum#+ does not call to_i implicitly on its argument.

Defining to_i only lets you call it (explicitly). Whether or not it is called implicitly depends on where that is used. There is nothing you can do to make to_i be called implicitly just by defining to_i. If you really want to do it, you have to modify Fixnum#+.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.