Override Time#to_s -- how?

B

Ben Winekur

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
 
J

Justin Collins

Ben said:
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
 
J

Justin Collins

Justin said:
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.
 
W

Wilson Bilkovich

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 :eek:riginal_to_s, :to_s
def to_s
"The old to_s method would have said: " + self.original_to_s
end
end
 
C

cies

I'd like to override the Time#to_s function to parse Time objects a
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.
 
E

Eero Saynatkari

--Q3iZjccpNGsIEgQL
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

class Time
alias_method :eek:riginal_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

--Q3iZjccpNGsIEgQL
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFPYoq7Nh7RM4TrhIRAu3kAJoCtsgR52ZsnGLDeTg1d8QG2yHsmQCdFjPz
zBL1QzoV9MbbDXAALPYPOVI=
=I02C
-----END PGP SIGNATURE-----

--Q3iZjccpNGsIEgQL--
 
B

Brian Neal

Ben said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top