# Extending the to\_s method

**URL:** <https://www.ruby-forum.com/t/extending-the-to-s-method/90583>\
**Category:** Rails\
**Created:** [March 30, 2007, 1:29am UTC](https://www.ruby-forum.com/t/extending-the-to-s-method/90583 "2007-03-30T01:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 30, 2007, 1:29am UTC](https://www.ruby-forum.com/t/extending-the-to-s-method/90583/1 "2007-03-30T01:29:29Z")

</div>

Hi!

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  
‘lalo’  
end  
end

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!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 30, 2007, 1:45am UTC](https://www.ruby-forum.com/t/extending-the-to-s-method/90583/2 "2007-03-30T01:45:30Z")

</div>

Hi,

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  
%\>
