Time.to_s

J

Jonas

I'm overriding Time.to_s to use a custom date format.
The method get's an argument, nothing in the documentation says that it
should. How do I find out what the argument does?
Where can I find the sourcecode for the original Time.to_s?
 
R

Robert Dober

I'm overriding Time.to_s to use a custom date format.
The method get's an argument, nothing in the documentation says that it
should.
Hmm that is strange, I cannot pass any argument to Time#to_s, can you
specify your version?
Robert
 
F

Florian Aßmann

Hi Jonas,

the Object.to_s method should not expect an argument since it is per
default an alias for Object.name.

If you want a custom time format you should overwrite the instance
method. There are several ways to accomplish this. For example you
derive from Time:

class CustomTime < Time
def to_s
strftime 'Simon says: Do your %d sit-ups on every %A.'
end
end

HF
Florian
 
J

John Joyce

Try Time#strftime
(string format time)
It is similar to the formatting found in other languages. You can =20
define your own method and simply use part of this internally in you =20
method:

def my_time_format(Time)
Time.strftim(%D%M)
...other code...
end

It is really that simple.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top