Learn Ruby The Hard Way Rapidshare Files
We’re getting to about where I was in LRTHW before I decided to go back and write everything down. Here’s my work for. Exercise 15 Code Exercise 15 Output As far as I can tell, my code matches Zed’s exactly. But for some reason, my “Type the filename again: ” string doesn’t start on a newline.
As a temporary fix, I added n into my code. This is something I’ll revisit later.
Learn Ruby the Hard Way rapidshare, megaupload ebook search. Learn Python the Hard Way: A Very Simple Introduction, 3rd Edition. Book Description. Learn Ruby The Hard Way Rapidshare Download. Your AV is probably flagging SWF files or ZIP files without. Download fresh hidden object, dash, match3. Zed Shaw's Learn Code the Hard Way, an extension of his wildly popular Learn Python the Hard Way into Ruby. Search and download in the smartest way. Black Knights Tango Pdf Reader. More than 20 million files available for free download and all you need is to click a 'Download' button to save them to. 10 Programming Languages You Should Learn Right Now.
Exercise 15 Revised Code In the meantime, I wrote this down in my notebook so I can search for an answer later. Study Drills 1. Above each line, write out in English what that line does. Exercise 15 Code Comments 2. If you’re 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”. Documentation for and. When we use the open method, we’re not actually returning the contents of the file.
Instead, we get the file object. Zed himself explains that the difference between the file object and the file contents is kind of like the difference between a DVD player and a DVD. If our file has contents inside, the read method will return those contents as a string. I used the word “commands” here, but commands are also called “functions” and “methods.” You will learn about functions and methods later in this book.
I’ve seen them described as “functions” and “methods” in other tutorials, but hey! “Commands” works too, and probably makes a little more sense to someone who’s never used Ruby before. Get rid of the lines 8-13 where you use gets.chomp and run the script again. Drill 4 Code and Output Our program doesn’t prompt the user to enter a second filename, and it only reads the file’s contents one time. Use only gets.chomp and try the script that way.
Why is one way of getting the filename better than the other? Using gets.chomp instead of ARGV The answer to this question might be more obvious if we were working with multiple variables. ARGV basically assembles all of your arguments into an array. As far as I know, gets.chomp can’t do this.
Instead, gets.chomp merely accepts a string from the user and assigns it to a variable. Also, the gets.chomp input method adds an extra step to your script. You could argue that it makes for cleaner code to use ARGV in cases where the user can just enter arguments before running the program. Start irb 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? Opening a file from irb Okay, this took me an embarrassingly long time to figure out LOL. At first, I couldn’t figure out why I was getting that “undefined local variable or method”.
Turns out I needed to enter the filename as a string. Apparently, irb needs filename parameters to be given as strings. That’s why I’ve got the single-quotes on my second go-around. Have your script also call close() on the txt and txt_again variables.
It’s important to close files when you are done with them. Closing my file objects Summary This exercise shows how Ruby reads from a file. First, we created a.txt file and wrote a few sentences inside. Because we want to avoid “hard coding”, we let the user input whatever filename they need through either ARGV or gets.chomp. Then, we used the “open” command to make what’s called a “file object”.
We can play around with the file object using different commands, but the file object is not the file’s actual contents. In this example, we called the.read method on both the txt and txt_again variables. We did this in order to read the.txt file we created at the very beginning.