Profile image of anthonygooran
Submitted by anthonygooran
about 11 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!"

Profile image of webWhiz67121
Submitted by webWhiz67121
almost 11 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.

Profile image of fanaugen
Submitted by fanaugen
about 11 years

8 comments

Profile image of anthonygooran
Submitted by anthonygooran
almost 11 years

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

Profile image of fanaugen
Submitted by fanaugen
almost 11 years

So, did you find out what \n is?

Profile image of anonymous
Submitted by anonymous
almost 11 years

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

Profile image of fanaugen
Submitted by fanaugen
almost 11 years

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

Profile image of anonymous
Submitted by anonymous
almost 11 years

What us the purpose of |x| ?

Profile image of anthonygooran
Submitted by anthonygooran
almost 11 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.

Profile image of anonymous
Submitted by anonymous
almost 11 years

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

Profile image of rubyNinja43179
Submitted by rubyNinja43179
almost 11 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?

Profile image of cardamomclouds
Submitted by cardamomclouds
over 10 years

1 comments

Profile image of msfrisby
Submitted by msfrisby
over 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

Profile image of elkommy
Submitted by elkommy
over 9 years