Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Override Time#to_s -- how?

5 views
Skip to first unread message

Ben Winekur

unread,
Oct 23, 2006, 9:22:48 PM10/23/06
to
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

--
Posted via http://www.ruby-forum.com/.

Justin Collins

unread,
Oct 23, 2006, 9:44:18 PM10/23/06
to
Ben Winekur 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

Justin Collins

unread,
Oct 23, 2006, 9:46:19 PM10/23/06
to
Justin Collins wrote:
> Ben Winekur 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 meant,

Time.now.to_s => "Hi Time!"

Obviously.

Wilson Bilkovich

unread,
Oct 23, 2006, 9:48:07 PM10/23/06
to
On 10/23/06, Ben Winekur <ben.v...@gmail.com> 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

cies

unread,
Oct 23, 2006, 10:20:27 PM10/23/06
to
> > 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.


--
"Computer games don't affect kids; I mean if Pac-Man affected us as
kids, we'd all be running around in darkened rooms, munching magic
pills and listening to repetitive electronic music." -- Kristian
Wilson (Nintendo, Inc), 1989

Eero Saynatkari

unread,
Oct 23, 2006, 11:38:39 PM10/23/06
to
On 2006.10.24 10:48, Wilson Bilkovich wrote:
> On 10/23/06, Ben Winekur <ben.v...@gmail.com> 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)
> >
> 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

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

Brian Neal

unread,
Oct 23, 2006, 11:45:01 PM10/23/06
to
Ben Winekur 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

Paul Lutus

unread,
Oct 24, 2006, 4:14:59 AM10/24/06
to
Ben Winekur 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.

--
Paul Lutus
http://www.arachnoid.com

0 new messages