Ruby File Gets Chomp
Exercise 15: Reading Files You know how to get input from a user with gets.chomp or ARGV. Now you will learn about reading from a file. You may have to play with this exercise the most to understand what's going on, so do the exercise carefully and remember your checks. Working with files is an easy way to erase your work if you are not careful. This exercise involves writing two files.
One is the usual ex15.rb file that you will run, but the other is named ex15sample.txt. This second file isn't a script but a plain text file we'll be reading in our script.
Here are the contents of that file: This is stuff I typed into a file. It is really cool stuff.
The Ruby chomp is used with functions such as the ‘gets’ function to remove the ending ‘n’ character from the string obtained via the gets function.
Lots and lots of fun to have in here. What we want to do is 'open' that file in our script and print it out. However, we do not want to just 'hard code' the name ex15sample.txt into our script.
Satori File Gets Deleted
- The Ruby chomp is used with functions such as the ‘gets’ function to remove the ending ‘n’ character from the string obtained via the gets function.
- There are two methods happening with gets.chomp (gets and chomp). If you call gets, ruby will wait for the user to input text in our case via the keyboard. When you press enter, this is returned. In your code when your enter your name, it sets the variable name to whatever your type in. When you use just 'gets' and press enter/return.
'Hard coding' means putting some bit of information that should come from the user as a string directly in our source code. That's bad because we want it to load other files later. The solution is to use ARGV or gets.chomp to ask the user what file to open instead of 'hard coding' the file's name. Study Drills This is a big jump, so be sure you do this Study Drill as best you can before moving on.
Above each line, write out in English what that line does. If you are not sure, ask someone for help or search online. Many times searching for 'ruby THING' will find answers to what that THING does in Ruby. Try searching for 'ruby open.' . I used the word 'commands' here, but commands are also called 'functions' and 'methods.'
You will learn about functions and methods later in the book. Get rid of the lines 8-13 where you use gets.chomp and run the script again. Use only gets.chomp and try the script that way. Why is one way of getting the filename better than another?. Start irb to start the irb shell, and use open from the prompt just like in this program.
Notice how you can open files and run read on them from within irb?. Have your script also call close on the txt and txtagain variables. Easy recovery essentials for windows 7. It's important to close files when you are done with them. Common Student Questions Does txt = open(filename) return the contents of the file? No, it doesn't. It actually makes something called a 'file object.' You can think of a file like an old tape drive that you saw on mainframe computers in the 1950s or even like a DVD player from today.
You can move around inside them and then 'read' them, but the DVD player is not the DVD the same way the file object is not the file's contents. I can't type code into Terminal/PowerShell like you say in Study Drill 7. First thing, from the command line just type irb and press Enter. Now you are in irb as we've done a few other times. Then you can type in code and Ruby will run it in little pieces. Play with that. To get out of it type quit and hit Enter.
See More On Stackoverflow
Why is there no error when we open the file twice? Ruby will not restrict you from opening a file more than once, and sometimes this is necessary.
Vister Gade wrote: heloo. I m a beginner of ruby. so i have a problem about gets function or gets.chomp.
when i run at SciTE. It will not do anything. Just a command blank. if i run it from file to ruby interpreter. The question will appear.
but when i insert input and enter. It will disappear. what are the problem is had. And what are the solution. please help.
The Windows command prompt closes after the program is done running unless the program was run by an already existing command prompt IIRC. My advice would be to opening a command prompt, and running it from an already opened command prompt. This should allow you to see the output of your program. Kyle Hunter wrote: Vister Gade wrote: heloo. I m a beginner of ruby.
so i have a problem about gets function or gets.chomp. when i run at SciTE. It will not do anything.
Just a command blank. if i run it from file to ruby interpreter. The question will appear. but when i insert input and enter. It will disappear. what are the problem is had. And what are the solution.
please help. thanks. The Windows command prompt closes after the program is done running unless the program was run by an already existing command prompt IIRC. My advice would be to opening a command prompt, and running it from an already opened command prompt. This should allow you to see the output of your program. Can u teach me how to run in step by step.
Sorry but i really needing u help. BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Vister Gade wrote: The Windows command prompt closes after the program is done running unless the program was run by an already existing command prompt IIRC.
My advice would be to opening a command prompt, and running it from an already opened command prompt. This should allow you to see the output of your program. Thanks. Can u teach me how to run in step by step. Sorry but i really needing u help.
Start - Run. cmd - help Navigate to the directory you saved the Ruby script, and type 'ruby nameofyourscript.rb'. This assumes that Ruby is in your path. Otherwise, you'll have to type in the path to ruby.exe, too (For example: 'c: ruby bin ruby.exe yourscrupt.rb'). More help can be found here: / - - Phillip Gawlowski Twitter: twitter.com/cynicalryan - You know you've been hacking too long when.you've been low-level debugging ethernets for a week and you see two people at a table trying to pick up the same jar of butter and you directly wonder if they are using the correct CSMA/CD algorithm to avoid a re-collision.BEGIN PGP SIGNATURE- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - iEYEARECAAYFAkgC6fsACgkQbtAgaoJTgL8K2wCgp8bne4hva7aPHeF88QCSmtXS HsQAn1AaZVHSKHb1oKwYaJ+r8BaWagXx =Uu7T -END PGP SIGNATURE.