0

The code below is longer than the recommended 80 characters.

I've tried to break it up and concatenate it with + operator and << operator, but it doesn't seem to work. I've tried the .to_s method and moved the colons into their own string but to no avail.

What would be the correct way to split this up onto 2 or 3 lines without getting the no method "+@":String error?

string = "#{sprintf("%02i", hours)}:#{sprintf("%02i", minutes)}:#{sprintf("%02i", seconds)}"
3
  • If you would have a Time object instead of your hour, minute, second integers, you could use time.strftime(%H:%M:%S). Commented May 16, 2015 at 18:49
  • Please edit to show your desired output. Commented May 16, 2015 at 18:56
  • I tried this but I dont think time is part of ruby so it gave me an error saying time wasnt defined. Commented May 17, 2015 at 19:24

3 Answers 3

5

You can combine the three sprintf into one:

string = sprintf("%02i:%02i:%02i", hours, minutes, seconds)
Sign up to request clarification or add additional context in comments.

3 Comments

NOT the answer to question
@AndreyDeineko If the question is a general how to split a line that includes string interpolated variables into multiple lines, you are right. However, it's not, in this specific problem, the line is long because OP's code uses unnecessary string interpolation.
Okay thanks for this, I could learn from this because I forgot sprintf was essentially the same as printf in java
1

To split string into multiline in Ruby you use backslash:

string = "#{sprintf("%02i", hours)}: \
#{sprintf("%02i", minutes)}: \
#{sprintf("%02i", seconds)}"

Notice how there is only one opening and one closing ".

You can also consider moving three calls to sprintf method into one.

3 Comments

why minus? it is the answer to OP question. who did it? :)
what exactly happens to the space between that colon and the backslash? Is all the white space just removed due to the \?
@Jeff if you put backslash without a space after :, there will be no space
0

I'm guessing that your real question is that, given numbers of seconds or a time object, you want to construct a string with the desired format.

If you are given seconds:

def fmt(seconds)
  hours, minutes = seconds.divmod(60)
  "%02d, %02d, %02d" % [hours, *minutes.divmod(60)]
end

fmt(223)
  #=> "03, 00, 43" 

If you are given a Time object:

require 'time'

def fmt(time)
  time.strftime('%H hours, %M minutes, %S seconds')
end

time = Time.now
  #=> 2015-05-16 14:38:02 -0700 
fmt(time)
  #=> "14 hours, 38 minutes, 02 seconds"

In this second case I added the words "hours", "minutes" and "seconds" just to illustrate how the format string could be modified.

If you want to print this on multiple lines, simply insert newlines (\n) in the appropriate format string.

2 Comments

Just a quick question. Im not understanding why you do hours,minutes = seconds.divmod(6). The six there isnt making mathematical sense to me..
Thanks for spotting that, Jeff. It should have been 60.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.