These methods are part of the Kernel module. Methods of the Kernel are available to all objects in Ruby. The print and puts methods produce textual output on the console. The difference between the two is that the latter adds a new line character.
The print method prints two consecutive "Apple" strings to the terminal. If we want to create a new line, we must explicitly include a newline character.
The puts method prints two strings to the console. Each is on its own line. The method includes automatically the newline character. In the example, we present the p , printf and putc methods. The p calls the inspect method upon the object being printed. The method is useful for debugging. The printf method is well known from the C programming language.
It enables string formatting. The putc method prints one character to the console. The second line prints a newline. The 0xA is a hexadecimal code for the newline character. Printing data to the console using the kernel methods is a shortcut: a convenient way to print data.
The following example shows a more formal way to print data to the terminal. The new method returns a stream to which we can write data. The method takes a numeric file descriptor. We could also simply write 2. By opening it and writing to it, we write to a console. The sysopen method opens the given path, returning the underlying file descriptor number.
It can be used to read input from the console. The read method reads data from the standard input until it reaches the end of the file. When we launch a program without a parameter, the script reads data from the user.
The gets method reads a line from the standard input. The data is assigned to the name variable. The data that we have read is printed to the console. We use interpolation to include the variable in the string. In the following two scripts, we discuss the chomp method. It is a string method which removes white spaces from the end of the string.
It is useful when doing input operations. The method name and usage comes from the Perl language. That would be fine as a comment, but hardly qualifies as an answer. I realise that the OP and the many readers who upvoted this answer disagree, and I frankly don't understand what they were thinking. The file must exist. In your case, w is preferable.
Community Bot 1 1 1 silver badge. CarySwoveland I actually agree with you. The real problem is that one of the two questions should have been marked as a duplicate a long time ago. I copied the answer because once I found the question zanbri had answered and the next few times when I needed the same info I came across this question first and had to figure out how to get to the other question. Eventually, I thought it would just be easier to have his answer here as well.
I linked to his answer so hopefully, people would click over and give him an upvote as well. For those of us that learn by example Write text to a file like this: IO. Read it back like this IO. Beware the IO. Append with IO. Not sure what you're talking about? The IO. To destroy the previous contents of the file, then write a new string to the file: open 'myfile.
Marko Tunjic Marko Tunjic 1, 1 1 gold badge 12 12 silver badges 14 14 bronze badges. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 1. See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
Accept all cookies Customize settings. Site Color. Ad Color. Sign Up to Save Your Colors. Privacy Terms. In this tutorial we are going to use the following JSON file.
0コメント