This forum is now read-only. Please use our new forums! Go to forums
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
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!"
Answer 52f4f16c80ff33af9a001b49
Answer 54385c5952f8638484000120
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?
1 comments
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
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
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
8 comments
I did in fact experiment. Removing the
\n
produced the same output. Hence the reason I asked the question.So, did you find out what \n is?
I’m guessing, it doesnt mean anything. Cause nothing changed in my console when removed/inserted “\n”
Then you haven’t tested it thoroughly. Try comparing puts “#{x}\n” with puts “#{x}\n\n\n”
What us the purpose of |x| ?
x is just an arbitrary place holder. You could call it peach if you wanted. It basically acts a placeholder for the
.each
method.tell me ur cell,no or an email or social network (fb only) i want help! m new on it! i’ll appriciate
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?