Console Coding for beginners.

Justin Webb
5 min readSep 25, 2020
1999 ‘The Matrix’ data rain by the Wachowski Brothers

After my first week of the software engineering program by Flatiron, I sympathize entirely with the adage about a ‘frog stuck in a well.’ I once believed computer science’s challenge lay in syntax errors, not information; I was wrong. The number of labs and reading material on the first three days was staggering! Panicked, I thought the best method of keeping with the group was to fly through the labs by regurgitating techniques from the reading material. On the day of the first exam, I woke up feeling armed with all of the information crucial for passing, but I made an error again. Even with an arsenal of methods at my disposal, I didn’t note how to test the methods I made. After seemingly completing the assessment, I realized how vital the ‘annoying extra information’ in the reading material is. This post dates mere hours after completing the first coding assessment. Hence, the frustration and anxiety that comes with the inability to test your answers are still fresh in my mind. I now know that understanding how to deploy your code is as important as knowing how to write it. Turning in untested code feels like finding the best gun in the game spawned with no ammo, useless. Without the ability to test your code, nothing is certain. Even the cleanest and most succinct code can error due to typos. Having already experienced such defeat, I feel I must inform all who come after me. To that end, this blog will cover how to create local class instances that ‘belong-to’ or ‘has-many’ relationships and how to call class methods in your terminal for Ruby. Let’s ammo up!

taken from gifer.com by user Adorardana

Challenge:

We are tasked with creating three classes or models, ‘StruggleBus,’ ‘Driver,’ and ‘Passenger.’ For our purposes, a ‘Driver’ has many ‘Passenger’s, a ‘StruggleBus’ has many ‘Passenger’s and ‘Passenger’s belong to both ‘a Driver’ and ‘StruggleBus.’ ‘Driver’ and ‘StruggleBus’ is a many to many relationships. ‘Driver’ takes one argument in the form of a string; this will represent the driver’s name. ‘StruggleBus’ takes two arguments in the type of strings representing the bus’ name and struggle_level. ‘Passenger’ is initialized with a driver as a ‘Driver’ object, a struggle bus as a ‘StruggleBus’ object, and a string name as a string.

Bus Domain Model made by Aleksi Sjöberg

Wow, okay, that was a large amount of information! Don’t let it bother you if some of the technical words used seem foreign right now. Before we race ahead, let’s take a step back and cursorily review Ruby and classes.

As you may or may not know, Ruby is an object-oriented platform. This is clearest when working with classes in Ruby. Ruby classes act as object factories capable of ‘instantiating’ or creating new instances. Before classes can instantiate new objects, classes first have to be built. Ruby classes typically look something like this:

class StruggleBus #note the CamalCasing of the class variable

end #code after the ‘end’ will not be contained by the StruggleBus class

Classes can communicate even inherit information from one another when the ‘environment’ is conducive. You can check ‘required relatives’ and the conditions of the ‘environment’ in your console. The console is the same location that we will write out instances so that they persist within our local workspace. In our example, ‘Driver’ and ‘StruggleBus’ communicate through ‘Passenger.’ All three of our models have the same basic framework for creating a new instance. Something like:

instance_holder = ClassName.new(‘relevant information’)

An instance holder is a variable that holds the new instance we are creating. ‘ClassName’ should be replaced with the actual name of the class you want to make. The next part, ‘.new,’ uses (.)dot notation to tell the class exactly what we want it to do. In the ‘(),’ we want to add relevant information in the form of the data type specified when we initialized our class. Suppose there is more than one argument expected. In that case, we have to add the relevant information in the same order as our initialize method. Let’s try to make this tangible by utilizing the framework for our example. Let’s start with ‘Driver.’:

Sam = Driver.new(‘Sam’) #if we used a name like Lil’ bud, we’d need to use “” rather than ‘’

Above, we have created a new instance of the ‘Driver’ class named Sam. We input the argument as a string since the challenge directed us to do such. Using (.)dot, we could now, as our new instance Sam to do things in the terminal like return its name:

Sam.name → ‘Sam.’

Next, let’s create an instance with two arguments, bus name, and struggle level.

easy = StruggleBus.new(‘Ranger’, ‘Easy’)

medium = StruggleBus.new(‘Magic school bus’, ‘Medium’)

hard = StruggleBus.new(‘Partridge Family Bus’, ‘Hard’)

The Partridge Family first cast 1970

Above, our variable name is ‘easy,’ literally. As you can see, we use commas to separate our argument inputs. Take a second and try to hypothesize what:

easy.name

would output. Okay, now let’s see.

easy.name → ‘Ranger’

The reason we get ‘Ranger’ is not that ‘name’ is always the first argument, though it is conventional. The reason ‘easy.name’ outputs ‘Ranger’ is due to the ordering in our initialize method. To get ‘Easy’ we’d need to type :

easy.struggle_level — -> ‘Easy’’

into our terminal. To return both values, we only need to type ‘easy’ in our terminal. To return all three instances, we’d need to make a method that collected all cases and call the class name with that method in the terminal:

StruggleBus.all

In this example, ‘.all’ is a method in our class that holds each instance. Lastly, let’s look at how to create ‘Passenger’ instances that contain objects and a string.

At first, it is easy to overthink this, but we use the same framework as our other two models. Check it out:

jeff = Passenger.new(sam, medium, ‘Jeff’) #objects do not have ‘ ’ surrounding them

As the prompt specified, our ‘Passenger’ instance starts with the ‘Driver’ object, then the ‘StruggleBus’ object, and ends with a string representing the instance’s name. To return information specific to Jeff, we continue to use (.)dot notation:

jeff.driver → ‘Sam’

jeff.name → ‘Jeff’

jeff.strugglebus → ‘Partridge Family Bus’ ‘Medium’

jeff.strugglebus.name → ‘Partridge Family Bus’

It may feel strange initially, but I promise that the entire process will get easier as you practice the whole process. Suppose you are in a coding boot-camp like me or even teaching yourself. In that case, the ability to test methods is an invaluable skill. Starting now will only set yourself up for success in the future. At the very least, you can avoid the stress and anguish I experienced. I hope this helps!

--

--

Justin Webb

I am a graduated environmental studies researcher, who began a computer science boot-camp. I will be blogging solutions to what I struggle with most.