(Below is the blog post I wrote to accompany my CLI project for Flatiron - originally published Jan 31, 2019. Is it good? No. Is it helpful? Also no. But I'm trying to match up my DEV posts with the ones on my personal site so here we are.)
I've just finished the code for my very first Flatiron Project, which was creating a CLI Data Gem in Ruby. I started it on Sunday and y'all, it was a RIDE. Below is as much as I can manage to say about it with a brain that is completely DONE:
Things I have learned since Sunday:
How to create a github repository with the Learn IDE, the bundle gem, and github.
How to stage changes for submission to github, how to commit, what a commit message is, and how to push.
That if you don't save your files before trying to push, the terminal will warn you that "Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean."
When you create your repository, camel case is the way to go. At first I tried "Best-Books-of-2018" but I ended up with a lib folder that was several layers deep because a folder was created for each of those words. When I started over and used "best_books" my file structure was much cleaner, with just lib\best_books.
The file the user will run is called an executable and it goes in the bin directory.
Because the file was not created as a .rb file, I needed to add a line (#!/usr/bin/env ruby) so my computer would know to interpret the file as ruby.
Go one step at a time and make sure things are working the way you want.
In the Learn IDE files are created with a backslash ( \ ) but are run in the terminal with a forward slash. For example, my bin\best-books file did not work when I put "ruby bin\best_books" in the terminal but did work properly when I put "ruby bin/best_books"
ls -lah is a list of all the directory contents in human-readable format
cd [file name] will let me get into a different file, and cd .. will let me back out one level.
To change the syntax in a non .rb file to ruby (like my best_books file in bin) I can go to Edit > Select Grammar > Ruby.
At one point I ran into an error where even though the IDE had saved my cli.rb file, it was displaying that it had not and could not save, and it did not show under my "best_books" folder. I copied and pasted the text into a .rb file in Notepad++ as a backup, then pushed my changes to github and restarted the IDE. (My dad taught me the first step of engineering is to check whether the thing is plugged in and the next step is always just restart.) When I checked git, my cli.rb file was there, and when I cloned it to my IDE, it was there as well and was able to save changes just fine.
Saving my files locally in Notepad++ especially when it's something I don't want to lose is an A++ idea.
You need to tell your file to go into the lib directory and get those and load them too.
It's okay to try things and break your program. Nobody gets hurt, nothing terrible happens (if you save early and save often), and you can fix it. The worst thing that happens is you learn something new.
TYPOS CAUSE TERRORS.
What "here doc" is.
How to get rid of the whitespace at the front of a list from a here doc: "<<-DOC.gsub /^\s*/,''
At some point, you just have to start building stuff.
At some point, if you break everything, it's okay to take the good parts and start over.
Talking with someone is good. Saying things out loud helps you realize things. And they can help you see things you overlooked because of stress.
You don't have to be forever tied to something just because you got a part of it running. Near the end I realized I wanted to redo my entire CLI.rb file annnnd yolo I did and I feel better about my program and everything is working.
You should check for names of existing gems when you create yours - I happened to make one with the exact same name as an existing gem and I'm currently trying to figure out what I need to change so that the pkg folder contains a file with a different name than the existing gem.
PUPDATE: go into the gempsec and change the spec.name to something else. also update the github repo name. commit everything. In a clean IDE session, run rake build again and you should see the new file name. If it's your first time pushing to rubygems you need to use gem push not rake push, AND (here's a key point) when you try to enter your password: it won't show the characters. But you're totally doing it because if you do it right, when you hit enter it'll work. THE END.
GOOGLE IS YOUR FRIEND. I had to google (1) stuff I've learned but didn't remember, (2) stuff I didn't know, and (3) stuff I learned in the stuff I didn't know that I didn't understand. I opened and closed many. many. Chrome tabs.
Caffeine is da real MVP.
I had to choose a website. Which means it had to be something where I'd be able to more-or-less understand what I'm looking for in the HTML.
I also knew that it needed to be something I could figure out how to do, which meant I only wanted to get information from the first page and then maybe go one level deeper.
I wanted it to be something I found interesting since I'm going to have to spend hours with this project.
I settled on the Goodreads Best Books of 2018 page - the data set was limited (21 categories) and I only needed to get information about 1 book in each (the top book). I could also satisfy the project if I only completed the first level (letting the user ask about the category and then getting back the book).
I went ahead and inspected the pages and pulled the code I thought I'd need for my scraper, to at least get familiar with the page structure since there'd probably be lots of trial and error.
I then looked at some examples from the learn.co project page and others I found on google and started to think about how I'd want my project to be structured.
The first thing I did was sketch out my CLI - what I wanted it to say, what methods I wanted it to reference, and what I wanted it to return to the user. This helped me see what information I'm going to need to collect, what methods I want, and what classes I'll need.
I settled on attempting 3 classes - Author, Book, Category - since we previously worked on Has Many Through classes and this was an area I particularly struggled with. I figured this would be good practice and review.
I looked at the code in the World's Best Restaurants project referenced on the learn.co project page so I could get an idea of what a final project should look like and what kinds of methods I should think about making.
I went to bed.
I sat down to work and realized somehow I'd gotten myself all turned around with my repo and things weren't working the way they should and it was a mess, so I deleted it and started over (see above: What I Learned).
From THERE, I went back to the CLI Gem Walkthrough video on YouTube.
From here it's kind of a blur - somehow I got to where I was ready to scrape, and my first attempt was a hot mess. I had my 1:1 and learned how to get it to pull just what I needed and lolwelp it was a lot easier than I'd tried to make it.
From there, I had to do the second scraper method, which was supposed to scrape each category's URL to grab the author and description of the book. This process was also a mess. UNTIL:
I watched another example build and looked at the code. I had tried to pass in the object and pull its URL attribute before, but whatever I was doing was not it. Once I actually passed in the object I was getting the URLs back. PROGRESS.
And it was at this point I realized I only needed 1 class, not 2, and certainly not 3.
After I got everything tied to one class and the right objects in that class: ERMAGERD I HAD A PROGRAM.
The next day I took out the random comments, deleted things I wasn't using, and did a final commit.
From there it was just the fight to figure out how to push it to rubygems but I DID IT I AM DONE.
I am exhausted. My brain is toast. I would come home from work, eat a quick dinner, and go to my study spot where I'd work until midnight or so. This worked a lot better when I was 21 and full of YOUTH. Right now I am very excited to get a good night's sleep.
But, I'm really, really glad we did this. I learned so much so quickly, and to be honest I am still surprised by just how much information I've absorbed since we started at the end of November. THE BRAIN IS AMAZING.
Now that we've made one, I think I could figure out how to make another one. Maybe. But I am definitely (1) still excited about learning to code, (2) still having fun even when it's hard, and (3) so grateful for everything.