Did you ever wake up and thought “Today, I’m going to write the entire app in one attempt”? How did it go for you?
One of the hardest thing for me while learning coding was to understand what my lines of code actually do. I would write ten lines of code only to see it do nothing, and have no idea where to start debugging it. I would assume my entire logic was incorrect, erase it all only to find out later that I used .each instead of .map.
My biggest AHA moment was to discover tools available to test what my code does as I develop my functionalities. Let’s take a look at few examples I cannot imagine my day without.
Use Irb to Master Ruby
Run irb in your console to access Interactive Ruby console. Can’t figure out if you need to use .map or .each Test it.
Ohhh, now it makes sense. My .each returns the original value, and .map returns the outcome. Easy!
“Puts” in Ruby
Nokogiri scraper goes through the page and collects values that you can store in the database for later use in the application. Simplified version of my personal scraper project looked like this:
[brainpickings-scraper.rb] [lang: Ruby]
1234567891011121314151617181920212223242526
require'nokogiri'require'open-uri'require'sqlite3'require'FileUtils'FileUtils.rm("books.db")ifFile.exists?("books.db")db=SQLite3::Database.new"books.db"db.execute<<-SQL CREATE TABLE books ( id INTEGER PRIMARY KEY, title TEXT, );SQLbooks_url="http://bookpickings.brainpickings.org/"doc=Nokogiri::HTML(open(books_url))books=doc.css(".photo")books.eachdo|book|title=book.at_css(".post_title h1")db.execute("INSERT INTO books (title) VALUES (?)",title)end
It might or might NOT show lines that look like this:
[brainpickings-scraper.rb] [lang: Ruby]
123
/Users/jenya/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:41:in`bind_param': can't prepare Nokogiri::XML::Element (RuntimeError) 5/lib/nokogiri/xml/node_set.rb:238:in `each' from brainpickings-scraper.rb:25:in `<main>'
We got an error. (Surprised? Don’t be). Let’s start debugging.
I’m going to comment out the call to the database and ask ruby to print the value of title on the screen.
[brainpickings-scraper.rb] [lang: Ruby]
123456789
books_url="http://bookpickings.brainpickings.org/"doc=Nokogiri::HTML(open(books_url))putsbooks.eachdo|book|putstitle=book.at_css(".post_title h1")# db.execute("INSERT INTO books (title) # VALUES (?)", title)end
One will show you all of the repos available, the other will only show repos that do not already belong to the user. How can I make sure that this method works correctly? Fire rails c in your active application folder and test it.
Chrome offers several nifty features that make developer’s life easier. Right-click on the element => ‘Inspect Element’ or Command+Shift+c will bring up Developer Tools window on the bottom of the screen. Console Tab can be used to inspect the DOM, debug JavaScript or analyze HTML parse errors.
Remember the example I displayed at the beginning of this post in my Nokogiri project?
Looks familiar?
Be smart and seek out helper tools to do your work efficiently.
Test lines your don’t understand.
Test methods you just created.
PLEASE, Work off a separate branch.
Test your commits.
Enjoy it :)