[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Override Time#to_s -- how?

Ben Winekur

10/24/2006 1:23:00 AM

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-....

7 Answers

Justin Collins

10/24/2006 1:44:00 AM

0

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

10/24/2006 1:46:00 AM

0

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

10/24/2006 1:48:00 AM

0

On 10/23/06, Ben Winekur <ben.vinegar@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

10/24/2006 2:20:00 AM

0

> > 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.


On 10/24/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> On 10/23/06, Ben Winekur <ben.vinegar@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
>
>


--
"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

10/24/2006 3:39:00 AM

0

On 2006.10.24 10:48, Wilson Bilkovich wrote:
> On 10/23/06, Ben Winekur <ben.vinegar@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

10/24/2006 3:45:00 AM

0

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


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

Paul Lutus

10/24/2006 8:15:00 AM

0

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.ara...