More Ruby newbie time: The importance of 'making the invisible visible' as a beginner (featuring the Date class)
Tags: beginners, programming, ruby, learning
Published: 2023-11-14 04:08:47


Today, a lesson that I've learned throughout my journey as an educator, has cropped up again. That's the importance of breaking things down into smaller, easier-to-grasp parts. One way to do this in programming was introduced in the online class I'm taking. They introduced the concept of 'making the invisible visible' when programming.

How do you do this? By printing as much as you can when you can. 

This probably seems obvious after you've had years of experience, or maybe you're already a programming wizard who doesn't even need to use this as a crutch anymore. But for the beginner it's so essential to building a good foundation in programming. 

So let's take this example from a little beginner coding challenge:


---
**THE DATE CLASS**

Write a program to identify different parts of today's date and print a String result. For any given date, the output should be:

"The year is: XXXX, the calendar day is: X, and the month is: X."

Use the exact same copy ("The year is..."), but replace "X" with your calculated date. Don't forget to ``require "date"``

---


So where do we start? 

Well thanks to some context clues, we know we're going to be working with the **Date class**. 

I think here is the first small part we have to make sure we understand. What is a class and why do we have to use ``require "date"``?

So **Date** is a class. As I understand it, this means it is a pre-built piece of code that's already been written to make it easier for us so we **don't** have to create a program that works with dates ourselves. 

**Phew! Thanks to whoever made the Date class!**

Now another important thing to note is that if we want to use this super handy class someone made for us already, it's not already pre-loaded in a default Ruby program, so you have to always start with ``require "date"`` to tell Ruby to get access to that pre-made class.

Great so we can write the first part of our program:
```ruby
require "date"
```

So what's next? 

Well we have access to the Date class now, so let's use it! Erm...How do we use it? Well the simple answer is by combining it with methods. Methods are also prebuilt pieces of code that make our lives easier. So how can we use them?

Well in this example, the method we'll need to use is ``.today``. To use this method, you have to "plug it into" something. In this case, we'll "plug it into" the Date class. I kind of think of the period before the method name as a little plug that connects it to whatever it comes behind. By using this method, we will have access to all the info the program needs to know the current date. 

So we can guess that we'll need to plug our ``.today`` method into the Date class. Let's say something like this

```ruby
Date.today
```

Nice! But we can't really do anything with that. It's kind of just information right now sitting there chilling. 

How can we use it? Enter the variable

So I think of variables as boxes. They are boxes we can hold information in. But what if we have tons of boxes? How are we going to remember which box holds what? That's the beauty of variables in our programs. We can label these boxes anything we want to make them easier to keep track of. I think of it like writing in a big permanent marker, a label on a cardboard box. 

So how do we box and label. Here's how I would do it:

```ruby
require "date"
date = Date.today
```

So I just put all the information that is included in our ``Date.today`` method into my box which I've labeled with my big permanent marker 'date'. It's important to note that I could have called it anything really though. I went with 'date' to remember what's in that box more easily. 

Ok now enter the whole concept of making the invisible visible. Let's take a peek inside the 'date' box and see what's inside!

The best way to do this is by adding some kind of print function in front of our variable so we can see what's inside. For example:

```ruby
require "date"
pp date = Date.today
```

This will allow us to run our program so far, and see exactly what we're working with. After you run your program, the result would be something like this

```
#<Date: 2023-11-14 ((2460263j,0s,0n),+0s,2299161j)>
```

Cool! That's all the information that's stored in our 'date' box or variable. We have plenty to work with to complete the date class challenge above! Let's revisit the challenge briefly:

---
Write a program to identify different parts of today's date and print a String result. For any given date, the output should be:

"The year is: XXXX, the calendar day is: X, and the month is: X."

---

So we need the four-digit year, day, and month! All of that's already in our 'date' box!

**Now we just gotta get it outta there somehow 🤔.**

Let's make more boxes!

We need a year, month, and day box. Let's do that first:
```ruby
require "date"
date = Date.today

#define variables
year =
month = 
day = 
```

Ok we have boxes, but now how do we pull that information out of the bigger date box and put it in the smaller boxes?

The answer is methods! It's going to kind of be like a method-ception idea here, but that's why the box imagery works so well I think. Remember we can plug methods into things with a '.'

How do we do this for the first box or variable called 'year'?

First we have to refer to the box we want to take the information out of. That would be the date box. Then we have to tell it exactly what information to pull out of that box using a method. In this case if we use ``.year`` we can pull the year out of the 'date' box!

```ruby
require "date"
date = Date.today

#define variables
year = date.year
month = 
day = 
```

Now is a great time to apply the 'making the invisible visible' concept we've been talking about. Let's see what's in that new box we made by printing it!
```ruby
require "date"
date = Date.today

#define variables
pp year = date.year
month = 
day = 
```

The output should be something like this (assuming you're reading this in the year it was written. If not it will be your current year.):
```
2023
```

Nice! Now we can just apply the same idea to the remaining two boxes or variables. Here's what we're left with:
```ruby
require "date"
date = Date.today

#define variables
year = date.year
month = date.month
day = date.year
```

You can similarly print all the variables to see what they're holding. 

Then you just have to cook up your string to print! If you use string interpolation, it's pretty straightforward:

```ruby
require "date"
date = Date.today

# define variables
year = date.year
month = date.month
day = date.year

# print the string
pp "The year is: #{year}, the calendar day is: #{day}, and the month is: #{month}"
```

This should complete the challenge! I hope this could help illustrate the concept of 'making the invisible visible' when building your foundations as a programmer. At least it's been helpful to me so far!

Thanks for reading and happy coding! 😀