Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixnum String conversion

Tags:

ruby

I want to get a string of the current time in Ruby:

"Time is " + Time.new.month + "/" + Time.new.day + "/" + Time.new.year

But it says "can't convert Fixnum into String". How can I fix this?

like image 284
John Avatar asked Dec 10 '25 13:12

John


2 Answers

Or you could just use right tool for the job: time formatting.

Time.new.strftime "Time is %m/%d/%Y" # => "Time is 11/13/2012"
like image 161
Sergio Tulentsev Avatar answered Dec 12 '25 08:12

Sergio Tulentsev


You could use to_s

"Time is " + Time.new.month.to_s + "/" + Time.new.day.to_s + "/" + Time.new.year.to_s

But event better is to use strftime

Time.new.strftime("Time is %-m/%e/%Y")
like image 34
Koen. Avatar answered Dec 12 '25 09:12

Koen.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!