DEV Community

Cover image for Programming Basics for Grandmas (Ruby)
coding4grandmas
coding4grandmas

Posted on • Updated on

Programming Basics for Grandmas (Ruby)

Today is the big day; we finally get to explore the intricate world of programming (kind of). Hopefully, by the end of this blog, our Grandmas will be able to write the first lines of code for their Airbnb app clone.

A few points of clarification before we dwell on the blog. As you know, the world of programming is very diverse which covers many different fields. With diversity comes various programming languages. As a matter of fact, our trustworthy Wikipedia source indicates there are approximately 700 programming languages.

https://media.giphy.com/media/l41YtZOb9EUABnuqA/giphy.gif

Yes, we couldn't believe it as well. But you might ask, what is a programming language? A programming language aims to provide detailed instructions to the computer in a binary format (computers can only understand 0 and 1). In short, any instruction written in a programming language gets translated to binary format, allowing the computer to execute a specific set of instructions (computer language).

With the difficult part out of the way; let's cover what language we will be using. We feel most comfortable in Ruby, and we also believe Ruby is relatively easy to read and understand (very similar to English). One thing to mention more or less, every language is the same in terms of basics. The main difference is how it is written. For the programming savvy, the term is 'syntax'. The reason why we have so many languages is they are used for different purposes - each programming language is designed to make certain things easy. Another point is that once one language is learned, other languages can be picked up relatively quickly.

Now, let's get back to the real stuff - coding!

Today we are going to cover two concepts:

  1. Variables
  2. Data types

Variables

One of the first things we learned when we started coding was variables. Variables are an essential part of programming as it allows you to stores values. The best way to explain how a variable works is through the box example. Value(s) can be stored in the box which are then labelled. In this instance the variable name is Grandma, and the stored value is Barbara.

Alt Text

You might ask, what is so great about variables? The answer is very simple, they allow the programmer to store values that can be accessed at a later use.

The creation of a variable works by associating a Ruby object with a variable name this is called a variable assignment.

Grandma = "Barbara"
puts Grandma 

# => "Barbara"
Enter fullscreen mode Exit fullscreen mode

The best way to understand variable assignments is to read from right to left (we assigned "Barbara" to Grandma variable). Then we can use the Grandma variable when we require.

Data types

The different Ruby data types:

  • Strings
  • Numbers (integer & float)
  • Boolean (True, False & nil)
  • Arrays
  • Hashes
  • Symbols

Strings

A string is a text.

greeting = "Hello world!"
puts greeting.class

# => String 
Enter fullscreen mode Exit fullscreen mode

The simplest and most common way to create a string is to enclose the object in quotes.

You can do some nifty stuff with strings. For example, you can use concatenation. Which means you can combine various strings into a single string.

"Grand" + "ma"
# ⇒ "Grandma" 
Enter fullscreen mode Exit fullscreen mode

The other way of doing (better) is using interpolation

name = "Grandma"
puts "Hello, #{name}!"
# => "Hello Grandma!"                     
Enter fullscreen mode Exit fullscreen mode

Other interesting stuff that can be done is using Ruby's build in methods.

"Grandma".length
# => 7

"Grandma".upcase
# => "GRANDMA"

"Grandma".reverse
# => "amdnarG"
Enter fullscreen mode Exit fullscreen mode

Numbers

In Ruby there are two different types of numbers integers and floats. Integers are represented without a decimal places and floats have a decimal places.

Integer

2 + 8
# => 10
Enter fullscreen mode Exit fullscreen mode

Float

2.0 + 8
# => 10.0
Enter fullscreen mode Exit fullscreen mode

Once we introduce division, the difference become apparent - integers and floats will produce different results.

10 / 8 
# => 1
Enter fullscreen mode Exit fullscreen mode
10.0 / 8
# => 1.25
Enter fullscreen mode Exit fullscreen mode

You might ask, why is this happening? The answer is simple integer data type cuts off after the decimal places. In contrast, the float provides the numbers after the decimal places.

Booleans

The Boolean data type is very straightforward; it has three properties true, false and nil. The main purpose of using true or false is to evaluate a logical statement, to check whether it is true or false.

"Grandma" == "Grandma"
#= > true 

"Grandma" == "Barbara"
#= false 
Enter fullscreen mode Exit fullscreen mode

Lastly, the nil data type is somewhat unique - it represents nothing (yes, you've heard us right). Sometimes you might need to assign a nil value to an object (this will be discussed in the future).

Arrays

Arrays give the programmer a very useful tool at his or her disposal. It allows to store multiple values in a single variable. The stored values can be any data type from strings to numbers.

grandma_array = [ "grandma" ]
Enter fullscreen mode Exit fullscreen mode

Square brackets represent arrays. As mentioned previously, they can store more than one value.

number_array = [ 1, 2, 3, 4 ]
Enter fullscreen mode Exit fullscreen mode

To access the array is done by referring to the index.

For example:

grandma_items = [ "glasses", "walking stick", "medication" ] 
Enter fullscreen mode Exit fullscreen mode

In the array above, the first element would be glasses. Counter-intuitively the index starts at 0.

puts grandma_items[0]
# => "glasses"

puts grandma_items[2]
# => "medication"
Enter fullscreen mode Exit fullscreen mode

This is how indices work within an array. The counting always starts at 0.

grandma_items = [ "glasses", "walking stick", "medication" ] 
                       0             1              2
Enter fullscreen mode Exit fullscreen mode

Hashes and Symbols

We have decided to dedicate a separate blog to hashes and symbols as they are more complicated data types than what's been covered above.

Summary

We've only covered the basics here; in subsequent blogs, we will break down each concept in much greater detail. The reasoning behind this is to keep the posts short and sweet. It can be overwhelming with all the new information, especially with someone like our Grandmas who have no prior experience in coding. The idea is to make each post relatively easy to digest.

In summary, variables allow the programmer to store values (box example). The variables then can be accessed at a later use. Understanding various data types is important as well - if something is unclear, we would advise having another read. If it is still unclear, do not hesitate to get in touch with us. We are happy to help (can't be worse than explaining to our Grandmas).

Top comments (0)