Override Time#to_s -- how?

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

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

Justin C. wrote:

Then:
Time.now.to_s => “Hi!”

I meant,

Time.now.to_s => “Hi Time!”

Obviously.

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

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

On 10/23/06, Ben W. [email protected] 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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs