This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by agooran
about 10 years

What is the function of "\n"?

Im not understanding why \n is in the code (seems redundant), let alone the fact what it even does…? Can someone help…

multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

multi_d_array.each { |x| puts "#{x}\n" }

Answer 53335afd52f863787c000206

6 votes

Permalink

Although experimentation could have revealed this answer, the underlying function of \n is a very good question to have asked.

What you are seeing is common in all programming languages and is called an Escape Sequence. When you are printing strings you may oftentimes want to manipulate the way the text displays, or more crucially, include characters that would normally break the code (including quotation marks within the string for instance).

To accomplish these tasks with little hassle you begin with a backslash ( ** ) within the string, which in essence tells the compiler “Stop! The upcoming text is special.”

In the example provided we use the newline escape sequence (\n) which, as you probably have long discovered, creates a new line for the forthcoming text. Here is a list of other escape sequences you can use:

 \" – double quote
 \\ – single backslash
 \a – bell/alert
 \b – backspace
 \r – carriage return
 \n – newline
 \s – space
 \t – tab

So if I wanted to print the string This is a “string” with troublesome\reactive characters! I could use the code puts "This is a \"string\" with troublesome\\reactive characters!"

points
Submitted by Matthew A
about 10 years

Answer 52f4f16c80ff33af9a001b49

0 votes

Permalink

Be curious. Experiment. Try removing the \n or adding another one. I’m sure you can figure out what it is in less than a minute. Less than it took you to post this question, anyway.

points
Submitted by Alex J
about 10 years

8 comments

agooran about 10 years

I did in fact experiment. Removing the \n produced the same output. Hence the reason I asked the question.

Alex J about 10 years

So, did you find out what \n is?

Dimitri about 10 years

I’m guessing, it doesnt mean anything. Cause nothing changed in my console when removed/inserted “\n”

Alex J about 10 years

Then you haven’t tested it thoroughly. Try comparing puts “#{x}\n” with puts “#{x}\n\n\n”

Hassan Bajwa about 10 years

What us the purpose of |x| ?

agooran about 10 years

x is just an arbitrary place holder. You could call it peach if you wanted. It basically acts a placeholder for the .each method.

Hassan Bajwa about 10 years

tell me ur cell,no or an email or social network (fb only) i want help! m new on it! i’ll appriciate

Bart Fiten about 10 years

Correct me of I’m wrong, but is the use of \n really necessary in this code? Since you are already using puts to make sure each element gets printed on a new line?

Answer 54385c5952f8638484000120

0 votes

Permalink

i understand the concept, now (thanks Matthew), but removing the n (and in fact, entering in a number of these codes after /)doesnt make any difference in how it displays. could it be my browser?

points
Submitted by cardamomclouds
over 9 years

1 comments

msfrisby almost 9 years

I see here you’ve typed \ instead of /. Which one did you use in your code? It should be . Most likely you’ve resolved the answers your questions by now, but hopefully this will at least be of help to one of my fellow nubs passing by –frisby

Answer 5603d05d3e0ec85ed3000544

0 votes

Permalink

as it seems /n adds a new line as Mathew explained above, for instance you can replace #puts with #print by adding \n after the thing you want to #print

points
Submitted by Hossam L Kommy
over 8 years