your daily code for breakfast

Baking codepancakes with Ruby!


In this lesson, we’ll start creating a very simple program in Ruby. This program, how appropriate, will help us making pancakes!

We will use to write our code, so you don’t have to install everything.

The first version of the program will be very simple, and later we will add more features. It’s always a good idea to start simple to become more familiar with programming. And no worries if you make a mistake, nothing will go wrong, and in the worst case, you can always start over. Learn by repeating. Also, as a general tip, don’t copy and paste the code. Type it over, because your fingers will help you remember!

First, we want to display a small text to indicate the program has started. In Ruby, you can do this by using the puts command.

Write the following code in the editor on and press enter:

What happens here, is that we print “Let’s make pancakes!” to the screen!
Ruby uses puts (put string) for that. And a string is the text between quotes(“).
But, to make pancakes, we need some ingredients. In fact, we need to create a list of ingredients!

So, as you can see above, we have created a list of ingredients by putting the strings (the text of the ingredients) between square brackets, [ and ]. And all the ingredients in the list are separated by a comma (,). In total we should have 4 ingredients to make pancakes. Maybe we should check this?

We now have used our list, which we created earlier, to check the length. The word ingredients is actually called a variable, because we can change the contents of the ingredients. The length word is called a method, and the method is called on the ingredients list, to display the total number of ingredients. Running the above should show the number 4. But to start making pancakes, we need to see the ingredients if we run our program! So, what we need to do, is go through the list (this is called looping) and display each of the ingredients.

So, now we did the following: we say that for each ingredient in our list of ingredients, we print it to the screen. Now we have the list of ingredients printed to the screen. It’s time to start making pancakes, but how? We need instructions for this! So, what we need to do now is create  a list of instructions, just like we created the list of ingredients before.

These are the instructions for making pancakes:
– Put the flour, eggs, milk and a pinch of salt into a bowl, then whisk to a smooth batter.
– Set a medium frying pan over a medium heat and carefully wipe it with some oiled kitchen paper. When hot, cook your pancakes for 1 min on each side until golden.
– Serve with sugar or your favourite filling!

Now, it’s up to you: create a list of instructions and put the 3 instructions in the list!

If you have done that, we can now show the instructions on the screen, just like we did with the ingredients. Go, you can do it!

Congratulations! We have created our first program in Ruby. And while it might not be a complex program, we did learn:

  • How to display text on the screen
  • What a string is
  • How to create a variable
  • How to use a variable
  • How to make a list of texts
  • How to do looping!

For now, stop coding and start creating some pancakes! If you like this tutorial, please leave a comment below. We love to get feedback, and it really motivates us to write more tutorials!
Next time we will learn how to use input and how we can do simple calculations.

Eet smakelijk!

Picture from RubyRan