Forum: Ruby on Rails Extending the to_s method

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-03-30 01:29
(Received via mailing list)

I'm developing an localization plugin (just for my own purposes) and I
wanted to extend the to_s method for the Time class.

What I want to do:
when someone does date.to_s(:short) that a localized version of
the :short date is displayed.
But I don't know how to accomplish this. I know have this:

class Time
  def to_s(option)
    print option.to_yaml

Jepp... it prints all the the times to 'lalo' :)

But how do I adjust this method so that only in the case of :short the
return value is modified?
Something like:

if option == :short {
  return self.strftime("%Y %D %E")

Or is there more to it?

Thanks in advance!
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-03-30 01:45
(Received via mailing list)

Maybe more concrete:

I have a model with an attribute called "ammount". It's a price stored
in an integer (in cents). Is it possible to extend the to_s method for
that attribute? So that I will show "€ 20,00" when I do: <%= ammount
This topic is locked and can not be replied to.