Practical1
Practical1

Reputation: 678

What is the difference between "\n" (newline) and "\r" (carriage return) in Ruby?

y = "Ruby\riscool"
x = "Ruby\niscool"

if x == y
  puts x
  puts y
else
  print 'Not equal!'
end

The newline and carriage return character both give the same output. However, the code above is false, and triggers the else statement. I don't understand how I can get the same output, but for both to be unequal? Can someone give and example and explain the difference between the two whitespace characters? Thanks!

Upvotes: 4

Views: 5807

Answers (1)

THUNDER 07
THUNDER 07

Reputation: 559

\n and \r doesn't make a lot difference in output both are used to print a new line though they perform same action they are different it can be observed in case of input for example if we are getting a matrix as input like:

1 2 3 (\n)

4 5 6 (\n)

7 8 9 (\r)

to give new line as input we will use \n(enter) when the input end carriage return come to play which is (\r).

Upvotes: 1

Related Questions