days.each{|day| day.me += day.better_me}

Hello, and welcome to my new blog! I have recently just begun a journey to become a full stack developer, and I will be periodically documenting my learnings here. Prepare to experience my triumphs, turmoils, and horrible code as I claw my way to a place of aptitude! For my first post, I will be taking a dive into some very basic code I wrote on my second day here at Flatiron School, using what I had learned in class earlier that day. I decided I wanted to practice what I had learned by building something stupid and fun when i got home, so i decided to create a “Star Wars Name Generator” that returned a users’ “Star Wars” name, which composed of two random names, and the planet they hail from. I’ll keep the code as is, so I can explain why I chose to do what I did, what I found was incorrect, and what I would do differently having learned so much in just a few short days. (At the time of writing this, it’s been just under one week!) This blog is all about growth, in every sense, so please expect this code to be buggy and full of total disregard to best practices…at least for a little while! Without further ado, let’s take a gander.

I decided to seperate my little program into three different files, mostly to practice creating different files via the terminal “touch” command, and to encapsulate each little bit of logic to it’s own place. I hadn’t very much experience using the terminal before, so this was a good oppurtunity to solidify what I had learned. The CLI file contains a little loop, that when run will prompt the user via the console to enter “start”, the “starwarstoy” file contains all of the main logic for accessing the star wars API, as well the methods that allow the program to function, and the “run” file was the file I ran to start the program. Let’s take a look at the meat of the program first.

YUCK!!!!

At the top of the file I “required” the pry gem, the JSON gem, and the rest-client gem, which allowed me to pry my program(allowing me to debug and see what values were getting passed around where) as well as make calls to the API and parse that returned info into a format that was workable. As you can see from the code, I only end up calling the first page of character hashes, a descision that was made by a constraint for time(I was getting sleepy!) and complete oversight.( I didn’t realize until I was so sleepy!). If I were to go back and refactor this program, I would definitely iterate over every page available, either by using string interpolation to append a number in the API url, or using the “next page” key that is each of the returned hashes to recieve the next page.

I initially create an empty array that is then filled with each characters name via an each loop. I decided I wanted to split each characters name, into seperate parts, so say a character had a first and last name, or a first middle and last name, I wanted my program to split each name and store them in my array. I used the solution as follows.

This allowed me to check each name that is in my array, and if they consisted of more than one word, it would split that word into multiple words as an array. This yielded a nested array, which was easily transformed into a one level array via ruby’s handy .flatten method.

This little method here stores a random name twice (stored in first_name and last_name) and creates a variable containing a string with those values using string interpolation. To choose a random index, I used the rand() method with the name array as the argument, -1 due to arrays 0 based indexing. I realized after the fact I could have probably just called .sample….live and learn!

Here is where things get really messy…when I first started this little project, I didn’t realize I wasn’t grabbing all of the pages, and I also didn’t realize the planets could be grabbed via the same method I grabbed the character hashes. (Hey! I told you I was very tired!) So to get around that, I ended up saving the character hash again, choosing a random character via the whole rand(yoda yoda yoda) nonsense, (See what I did there?), and accessing the home world key, to grab a home world page from that random character. I then parsed that page, and returned it’s name. This was a very round about solution, and while innefficient, would still work had my character sample size been larger, i.e., had I correctly iterated through ALLLLLL of the characters. However, even with this method, the random planet should still be pseudo random right? RIGHT?….NOPE. Of the character hashes that were contained on the front page, guess how many came from the same planet….All but 2! So there was significant weight towards the planet Tatooine! If I were to go back and change things, i would definitely remedy that by just taking the planet info directly from the various planets hashes instead of relying on the character hash.

Looking back on this code, at this point in time, I would probably create classes that created character objects, instanstialized with instance variables that relate to each key of the hash. I could possibly create a custom class method that would choose a random character page, and fill the instance variables with the correct data. This would allow me to generate a character object, with all the pertinent details that I could use to create a more robust version of this program, possibly generating a character object with a mish mash of data from every character, allowing the user to have more than just their “Star Wars” name and planet.

The rest of the code is very unremarkable. The CLI file contained a small loop that would check to see if the user typed “start” into the terminal, and chastised you if you entered anything but, while the run file contained calls to the above methods with a little puts statement using string interpolation.

While it was not pretty, It was definitely good practice building this little program. I was able to familiarize what was taught to us earlier that day in a fun and creative way, which I feel is a great way to learn a new topic. I look forward to building other little programs to test my knowledge over the course of my days here at Flatiron School.

Each day, try to become a better me! See you next time!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.