# Override Time#to\_s -- how?

**URL:** <https://www.ruby-forum.com/t/override-time-to-s-how/73361>\
**Category:** Ruby\
**Created:** [October 24, 2006, 3:22am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361 "2006-10-24T03:22:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 3:22am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/1 "2006-10-24T03:22:45Z")

</div>

Hey everyone,

I’d like to override the Time#to\_s function to parse Time objects a  
specific way – how can I do this from within my script? (Without  
modifying the Time class itself)

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 4:17am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/2 "2006-10-24T04:17:49Z")

</div>

Ben W. wrote:

> Hey everyone,
> 
> I’d like to override the Time#to\_s function to parse Time objects a  
> specific way – how can I do this from within my script? (Without  
> modifying the Time class itself)
> 
> Thanks,  
> Ben

You mean output time objects a certain way?  
Here’s one:

class Time  
def to\_s  
puts “Hi Time!”  
end  
end

Then:  
Time.now.to\_s =\> “Hi!”

I’m not sure if that counts as modifying the Time class or not. You kind  
of have to do that to override the function, don’t you?

-Justin

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 4:19am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/3 "2006-10-24T04:19:50Z")

</div>

Justin C. wrote:

> > 
> 
> Then:  
> Time.now.to\_s =\> “Hi!”

I meant,

Time.now.to\_s =\> “Hi Time!”

Obviously.

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 5:40am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/4 "2006-10-24T05:40:08Z")

</div>

On 2006.10.24 10:48, Wilson B. wrote:

> ```
> "The old to_s method would have said: " + self.original_to_s
> 
> ```
> 
> end  
> end

Time#to\_s is one of the core methods I have absolutely no qualms  
overriding but if you would rather not, there is always #strftime.

ri Time#strftime

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 5:44am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/5 "2006-10-24T05:44:59Z")

</div>

Ben W. wrote:

> Hey everyone,
> 
> I’d like to override the Time#to\_s function to parse Time objects a  
> specific way – how can I do this from within my script? (Without  
> modifying the Time class itself)
> 
> Thanks,  
> Ben

Perhaps you mean something like this?

#!/usr/bin/env ruby

t = Time.new #or populate from some variable in your script  
def t.to\_s  
“Two minutes to midnight!” #your time parsing here  
end

puts t.to\_s  
puts Time.new\_to\_s

Produces:

Two minutes to midnight  
Mon Oct 23 23:42:49 -0400 2006

cheers,

Brian

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 5:08am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/6 "2006-10-24T05:08:07Z")

</div>

On 10/23/06, Ben W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey everyone,
> 
> I’d like to override the Time#to\_s function to parse Time objects a  
> specific way – how can I do this from within my script? (Without  
> modifying the Time class itself)
> 
> Thanks,  
> Ben

What do you mean by ‘without modifying the Time class itself’? Isn’t  
that precisely what you want to do?

All classes are ‘open’ in Ruby, so you can simply do:

class Time  
def to\_s  
“blah”  
end  
end

If you need access to the original method as well, you can do this:

class Time  
alias\_method :original\_to\_s, :to\_s  
def to\_s  
"The old to\_s method would have said: " + self.original\_to\_s  
end  
end

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 29, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/7 "2006-10-29T15:28:46Z")

</div>

> > I’d like to override the Time#to\_s function to parse Time objects a  
> > specific way – how can I do this from within my script? (Without  
> > modifying the Time class itself)

or maybe you want:

irb(main):001:0\> class MyTime \< Time  
irb(main):002:1\> def to\_s  
irb(main):003:2\> “time from MyTime”  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> MyTime.new.to\_s

then the Time class is guaranteedly not changed in behavior by the above  
code…

cies breijs.

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 24, 2006, 10:17am UTC](https://www.ruby-forum.com/t/override-time-to-s-how/73361/8 "2006-10-24T10:17:00Z")

</div>

Ben W. wrote:

> Hey everyone,
> 
> I’d like to override the Time#to\_s function to parse Time objects a  
> specific way – how can I do this from within my script? (Without  
> modifying the Time class itself)

Please state the goal, not the imagined solution. There are any number  
of  
ways to solve a problem using Ruby, but only if the problem is revealed.

If you really don’t want to modify the Time class, why not subclass Time  
and  
put your special handling in the derviced class? This is the classical  
approach to the goal you have stated.
