Summary of Ruby Experience

Hello gals and guys!! Welcome back to the last post of my ruby blog. It has been a wonderful journey to discover and learn a completely new programming language all on my own. Even though it was not of my own accord to the learn a new language, but was a required assignment for my computer science class, I undoubtedly enjoy the whole process from the beginning to the end.

On the first day of class when my professor had told his students to choose a language to learn by themselves, I thought it was a completely insane idea. Learning a programming language from an instructor was already difficult so how would I be able to learn all on my own. As always, the cliche of not judging a book by its cover could be applied to my case. I looked at some blog posts on current popular programming languages and decided that my top three choices would be Python, Ruby or Pascal. Luckily, I was assigned Ruby, a language that I especially enjoy now because it is dynamically typed. I love the idea that a variable can become anything depending on its assignment.

During the first week of research on Ruby, I was actually enjoying the process because I was learning a language at my own pace. I could closely inspect and find what I needed and advance from there. There were so many cool features that I didn’t know about: the ternary operator, hashes, destructuring assignment, method missing, flip-flop operator and many more. Some of these features were especially helpful for my Final Project. I had decided to program a game using the Gosu library that is compatible with the Ruby language. A tough, but wonderful part of the experience.

My proudest achievement would be the post before this because I had completed a game (something that I personally picked and wanted to code). I was glad to accomplished something that I had set out to do. Learning the basics and more had given me the confidence to become a better programmer. Not only can I program in Java and C#, I am now familiar with Ruby and also PHP and Python (the blogs that I had to follow). In the end, all the research, effort, and time put into these blogs were well worth it.

I would like to end with a quote to summarize my whole experience:

“There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.” – Jiddu Krishnamurti

Thank you to everyone that has read and liked my posts. I appreciate you all for coming along with me on this wonderful journey 🙂


“Catch the Rainbows” Part 3

Welcome back gals and guys!! For my last portion of the program, I will need to find a game over image to display when the unicorn runs out of lives. For this particular program I have chosen the image below:


I will store the image along with the other images in the hash for organization purpose (Line 16). 

I have also found an angry fire image to act as a distraction obstacle that will randomly spawn throughout the game window to confuse the player. This will also be included in the hash (Line 17). 


Next, I added a trumpet.wav file into the hash even though it is not an image, therefore, the syntax will be different. Instead of Gosu:: Image, it has to be Gosu:: Sample (Line 15).

I will need a new instance variable called lives which will be set to 5 at the beginning of the game (Line 24). 

Then, three additional instances: firex, firey, and number are needed to spawn the fire icon. Initially, firex and firey will be randomized according to the game window size (Lines 27-29).

Screen Shot 2018-04-22 at 12.03.03 AM.png

I created a new method called distraction_object that will be called in the update method to randomize the fire icon. Everytime update runs, the number variable will be added by 1. When it reaches 30, firex and firey will be randomized again and the number variable will be reset to 0. This is to guarantee that the randomizing speed isn’t too fast. Remember, the update method is called once every update_interval milliseconds (Lines 56-63).

Refer to Documentation if needed: Gosu

Then, add the created distraction_object method to the update method (Line 66).

Screen Shot 2018-04-22 at 12.13.59 AM.png

Next, I have to add more code to the update method to manipulate the lives variable. Inside the else statement of the update method, I have to ensure that if the rainbowy variable surpasses 550 then the lives variable will be subtracted by one. Once the lives variable is zero  (compare lives == 0), all variables related to the rainbow and unicorn will be set to 0 to end the game (Lines 77-82).

The final step would be to draw the fire icon and the lives text to the game window. Inside the draw method, we add the fire image and assign firex and firey to its x and y location (Line 91).

Then add the lives text to the upper right-hand corner of the window (Line 93).

In order to draw the game over screen, I have included an if statement to compare the lives variable to zero and then I can draw the necessary game over screen and also the score text to the upper-left corner of the game window for the player to see (Lines 94-97).

Screen Shot 2018-04-22 at 12.21.51 AM.png

Test the code:

Game Output:

Screen Shot 2018-04-22 at 12.45.56 AM.png

Screen Shot 2018-04-22 at 12.43.42 AM.png

Screen Shot 2018-04-22 at 12.46.46 AM.png

Screen Shot 2018-04-22 at 12.49.36 AM.png

Screen Shot 2018-04-22 at 12.47.18 AM.png

That will be the wrap up of our Final Project!! Thank you, everyone, for tuning in to the completion of “Catch the Rainbows” 🙂

“Catch the Rainbows” Part 2

Hello gals and guys!!! Welcome back to the developing process of “Catch the Rainbows”. The next step in the game is to add a falling rainbow from the sky in random x-locations.

First, I would simply change the static location of the rainbowx variable to a random location within the width of the game window. I have decided that the range would be 100 to 800 (line 17). 

Next, I have added four new variables: y_velocity, change, score, and font. y_velocity will be the falling speed of the rainbow. The change variable will keep track of how fast the rainbow will fall. The score will keep track of the number of rainbows the unicorn catches.  The font is necessary to draw text on the game window (Lines 19-21). 

I created a velocity_rainbow method that can be called later in the update method to have the rainbow fall from the set position 0 (rainbowy variable). Interval is a local variable, that takes a default float of Gosu called update_interval (16.66666 – the interval between frames in milliseconds) divided by 1000. Then y_velocity will keep on adding change (which is set to 200) multiply by the interval. Lastly, the rainbowy variable will keep on adding y_velocity multiply by the interval. The calculations are done based on my preference. (Lines 33-37).

Screen Shot 2018-04-14 at 11.31.47 PM.png

Then, I added an update method (According to Gosu, “this method is called once every update_interval milliseconds while the window is being shown”). I then called the velocity_rainbow method in the update to have the rainbow move. An if condition is needed to have points added to the score. If the distance between the four parameters, the x and y locations of both objects: rainbow and unicorn are less than 30 then score will add 1 and the rainbow is reset to fall from the sky again. The change variable will add 10 to itself every time the if statement occurs in order to speed up the rainbow as more points are obtained. Else, I call the spawn_more method if the rainbow and unicorn are not in the proximity-indicated by the if statement. (Lines 49-60).

Screen Shot 2018-04-15 at 12.19.21 AM.png

Next, I created a spawn_more method if the rainbowy location is greater than 558 (the height of the game window). The rainbowy variable will be reset to -50 to give the player more time to react. y_velocity will also be reset to 0 and rainbowx variable will pick another random location from 100 to 800 (Lines 41-47).  

The final step would be to draw the text onto the game window. We call the font variable and assign the word: Score and the score variable. Twenty and twenty will be the x and y coordinates. Finally, we assign the color black to the font. (text, x, y, z, rel_x, rel_y, color) (Line 67). 

Parameters according to Gosu:

  • rel_x (Float) — the horizontal alignment.
  • rel_y (Float) — the vertical alignment.
  • text (String)
  • x (Number) — the X coordinate
  • y (Number) — the Y coordinate
  • z (Number) — the Z-order

Screen Shot 2018-04-14 at 11.43.04 PM.png

Test the code:

Screen Shot 2018-04-15 at 12.22.25 AM.png

Screen Shot 2018-04-15 at 12.20.07 AM.png

Screen Shot 2018-04-15 at 12.23.21 AM.png

Screen Shot 2018-04-15 at 12.24.25 AM.png

I tried to add a video of the recorded game, but it was not possible because I do not have the premium plan to support it 😦 That’s the end of part 2 of “Catch the Rainbows.” Tune in to next week’s completion of the game!!!!!!!




“Catch the Rainbows” Part 1

Welcome back gals and guys to my final project!!!! First of all, all images were found on google and edited using the Magic Wand Tool:







The Atom Editor has a plugin tool called atom-image-resize package. Images above were resized to my preference.

Next, I created a GameWindow, a subclass of the Gosu::Window in order to call other events: button_down, draw, etc (Line 6).

The initialize constructor can take any number of arguments and runs when is created (Line 7, 36).

Super is the default behavior of Gosu::Window (Line 8).

Images can be stored separately in a variable, but I currently have a hash to keep all the images organized. Create an instance hash called @images then add in the background, uni and rainbow. Each of the images is being overloaded with (window, source, tileable) parameters (Lines 11-13).

The self.caption allows for a title to appear on the title bar. Any name can be used, but since I have called the game, “Catch the Rainbows”, that is what I will be using (Line 9). 

I have created x and y instance variables for both the unicorn and rainbow images to manipulate later on (Lines 15-17).

Button down is a method that is called before the update method when a button is pressed (Line 20).

In my case, I have decided to use the case when statements when different keys are pressed. For the escape button, the window gets close. For the left arrow, the unicorn’s x position will be subtracted by 35. For the right arrow, the unicorn’s x position will be added by 35. Ternary operators are used to stop the unicorn once it reaches the edge of the GameWindow (Refresher on the ternary operator can be found on a previous blog). (Lines 21-24).

The draw method is necessary for the images to appear in the game window and gets called after every update (gets called 60 frames per second). Take a key in the hash, then add .draw and the dimensions (x, y, z). For the background, (0,0,0) is sufficient. For the unicorn, pass in its @x variable for movement and a static y location. For the rainbow, pass in its @rainbowx and @rainbowy variables. (Lines 28-32).

To get the window to appear. After the dimensions are set, is needed (Lines 36-37).

Screen Shot 2018-04-08 at 2.14.32 AM.png

The prerequisites for the last post is needed before the file can be run in the Terminal.

Code File:

Screen Shot 2018-04-08 at 3.30.49 AM.png

Output: GameWindow 

Screen Shot 2018-04-08 at 2.13.17 AM.png


That is all for part 1 of “Catch the Rainbows”!! Tune in to the next blog to see more changes 🙂

2D Game with Ruby and Gosu

Welcome back to the Ruby blog!!! From this point onward, I will begin a final project to showcase the Ruby skills that I have learned throughout the past weeks. I will be implementing a game using Ruby and Gosu (a game development library that is compatible with both Ruby and C++). Since I have never worked with Gosu before, it will be a challenging experience to create my first game using Ruby, but that is the fun part. Helpful information for Gosu is included below:


Description of the Game:

The game will be called “Catch the Rainbows.” There will be a unicorn that the player can move back and forth horizontally and the goal is to catch the falling rainbows from the sky. When the unicorn comes into contact with a rainbow, a point will be added to the score. The game is over when the unicorn misses a rainbow. I will be implementing the game on a MacOS using the Atom Editor and grabbing the Gosu library (or gem) to work with.

Prerequisite Steps To Implementation:

For MacOS

1.) Visit:

2.) Install Homebrew in Terminal:

3.) Type in: brew install sdl2 

Screen Shot 2018-04-03 at 10.54.40 PM.png

4.) Install the Gosu gem in Terminal (type the words): gem install gosu

Screen Shot 2018-04-03 at 10.55.25 PM.png

5.) Open Atom Text Editor, create a Gemfile,  type in: source ‘’ and gem ‘gosu’ and then bundle install into project’s root inside the Terminal.

(Note: to get to the project’s root, use the “cd /” command and to exit use “cd -“)

Screen Shot 2018-04-03 at 10.57.52 PM.png

Atom Editor:

Screen Shot 2018-04-03 at 10.46.14 PM.png


Screen Shot 2018-04-03 at 10.48.26 PM.png

6.) Create a game folder, add in a file called “unicorn.rb” or whatever name the programmer prefers.

7.) In the first line of “unicorn.rb” file, type in: require ‘Gosu’ and the setup is complete!!!!

Screen Shot 2018-04-03 at 10.50.46 PM.png

For Windows

1.) Visit:

2.) Install Gosu through RubyGems and type in: gem install gosu and the setup is complete!!

Game Plan: 

The game will be completed in the next 3 blogs, following the general outline below:

First blog: Find and edit all the images related to the game, implement these images into the game window, and finally allow the unicorn to move back and forth horizontally. Furthermore, pressing the escape button will exit the game regardless of the state the game is in (A shortcut button to exit).

Second blog: Randomize the rainbows that are falling from the top of the screen. Add a score tracker at the upper left-hand corner of the game screen. The score will initially be zero until the unicorn catches a rainbow. A point is added each time the unicorn comes into contact with the rainbow.

Third blog: The velocity of the rainbows will increase slowly as the score increases. There will be 5 lives added to the upper right-hand corner, a life will be subtracted if the player does not catch a falling rainbow.  Once lives reach 0, a ‘Game over’ screen with the total score will appear. Each time the unicorn catches a rainbow, a trumpet sound will be played. Furthermore, there will be a randomized angry fire icon that will spawn throughout the game window to confuse the player.

That will be the general approach. Let us meet again in the next blog!!!

Interesting Ruby Features Part 2

Welcome back gals and guys to the Ruby blog. We will be exploring more interesting features of our language!!!

Destructuring Assignment

A way to break down an array or hash into individual items.

“Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable” – Lisp 

Simple examples of destructuring in destruct.rb file:

1.) Obtain the first three items in the numbers array (Lines 4, 6).

2.) Obtain the first item, in-between elements, and last item in numbers array (Lines 9, 11-13).

3.) Ignore certain elements in the array (Lines 16, 18-20).

Screen Shot 2018-03-25 at 8.49.14 PM.png


Screen Shot 2018-03-25 at 8.49.28 PM.png

Test the code:

Splat Operator

Splat Operator has two functions depending on its assignment. The first is known as “slurp” or “collect”. This function takes a variable number of arguments and adds them into an array. The operator is usually used in methods that take a range of inputs for a parameter. The second function is known as “split”. A way to convert an array into a list of arguments. This function is used to call a method with the arguments of an array.

Destruct Block Assignment

A Ruby programmer can destruct inside a block argument. Anything inside an array [ ] can be destructed. Thus, one can obtain the first element in the 3 subarrays of the triples array (Lines 17-18).

Examples of splat operator and destructuring of a block in splat.rb file:

Screen Shot 2018-03-25 at 9.03.31 PM.png


Screen Shot 2018-03-25 at 9.03.38 PM.png

Test the code:

Destruct Hashes

We have explored ways to destruct an array, but hashes can be destructed too.

A Ruby Hash contains a method known as values_at. The method takes a list of keys and returns the values for the keys in the same order (Line 2).

Destruct of destructuring is also possible (Lines 11-13).

Splat operator will work with hashes (Lines 16-17).

Example of destructuring of hashes in hashes.rb file:

Screen Shot 2018-03-25 at 9.46.50 PM.png


Screen Shot 2018-03-25 at 9.46.59 PM.png

Test the code:


More destructuring examples for Ruby:


That’s the end of Ruby’s unique features!!!

Interesting Ruby Features Part 1

Hello gals and guys, welcome back to the weekly Ruby blog. We will be exploring interesting features or hidden gems in the Ruby language!!!

1.) Method Missing

An object can call a method by searching through the method directory (methods that were written) for the same name. Once the object cannot find a particular method, there will be a NoMethodError exception. Though a programmer in Ruby is able to provide a “method missing” to prevent such an error. This method (method_missing) will pass a variable for a non-existent method, an array of arguments that were initially passed, and a block that was initially passed to the missing method. The second and third parameter could be empty if a method was called without any arguments, but are there if they were to be used or passed to other methods.

Note: The * is the splat operator. In a method, it specifies a variable length argument list. All arguments passed to a method will be placed into an array called args.

A more complex example of method missing can be found here:–net-20924

Example of Method Missing in methodMissing.rb file:

Screen Shot 2018-03-17 at 11.50.28 PM.png


Screen Shot 2018-03-17 at 11.40.41 PM.png

Test the code:

2.) Flip Flop Operator

A way to execute a portion of a loop. The flip-flop range operator compares two conditions inside of a loop. Starting with the first condition, everything will be true until the second condition. Before the first condition or after the second condition, everything else will be false.

Note: << operator apprehends values to end of an array. In our case, we apprehend the value that is true in the colors array to the empty array called myColors.

Example of flip-flop operator in flipflop.rb file:

Screen Shot 2018-03-18 at 12.30.26 AM.png


Screen Shot 2018-03-18 at 12.30.34 AM.png

Test the code:


3.) Almost everything is an object

Classes, instances, and primitive types (fixnum) are objects. Examples of objects include number, string, array, hash, etc.  While methods, operators, control structures (conditionals and loops) and blocks are not objects but can be wrapped into objects, which is called Proc. Ruby can take a block of code, wrap it up in an object, then store it in a variable or pass it into a method, and finally run the code as many times as the programmer desire.

Example of objects and proc in object.rb file:

Screen Shot 2018-03-18 at 2.02.25 AM.png


Screen Shot 2018-03-18 at 1.51.55 AM.png

Test the code:


That’s the end of Ruby’s unique feature!!!


Binary Search

Hello, gals and guys!! Our next topic in Ruby is binary search 😉

Binary search = divide and find

What exactly is binary search?

Binary search is a process to locate an element or item within a sorted array by continually cutting the data in half to check whether the element is either equal to, larger, or smaller than the middle element in the array until a match is found or else the element is not found.

To accomplish such a task, we will first need to compare the element that we are searching for (the input value) to the middle element. Next, if the input and the middle element does not match then check to see whether the middle is larger or smaller than the input. Keep on cutting the array in half until it is not possible anymore.

Iterative Approach

We will approach the problem using an iterative implementation, which emphasizes repetition until a task is completed.

For arrays that are not sorted, .sort can sort an array before being used in the binary search. (Line 3-4)

First, we will need a method called binarySearch and two parameters: array (sorted array) and item (the element that we would like to find). (Line 9)

Then, we would like to obtain the middle element. Thus, we need a variable called first to obtain index 0 and a variable last to obtain the last index in the array (array.length – 1). (Line 10-11)

Then add first and last together and divide by 2 and store in mid variable (middle index). (Line 14)

Now, we can check to see if the item matches array[mid] (the value stored in middle index) then return the item and index location. (Line 16-17)

If the array[mid] is greater than the item, then last will equal to mid minus 1. This will obtain the lower portion of the array, to the left of the middle index. (Line 18-20)

If the array[mid] is less than the item, then first will equal to mid plus 1. This will obtain the upper portion of the array, to the right of the middle index. (21-23)

Repeat this process in a while loop that checks that first is always less or equal to last until the item is found. (Line 13)

Once while loop is completed and the item is not found then return the item cannot be found in this array. (Line 26)

Example of iterative binary search in binarysearch.rb file:

Screen Shot 2018-03-11 at 8.42.49 PM



Screen Shot 2018-03-11 at 8.43.09 PM

Test the code:

That’s the end of our binary search journey!!!

Methods and Encapsulation

Hello everyone, let’s get to know more about Ruby through methods and encapsulation!!!

1.) Methods

Instead of repeating the same codes over and over again throughout a program, methods allow for the same codes to be placed in one location and called in various places. Change can be done in the method instead of having to change every repeating line of code. To write a method, start with the keyword def followed by a name, parameters can be added after the name and at the end of the method, the keyword end is needed to signify completion. Note: Other programming languages can refer to methods as functions.

Parameter(s) are used to access data or variables outside of the scope of a method to be used within a method. If the method does not require any data outside then defining a parameter(s) would be unnecessary. (Line 5)

Arguments are data that is sent to a method to be changed or used to return some sort of result. They are passed when a method is being called. Arguments depend on the number of parameters. If there are no parameters, then no argument is needed. (Lines 8-9) 

Default values for parameters are allowed in case there are no arguments. (Line 18)

When calling a method, parentheses are optional around the argument(s). A matter of preference for the programmer. (Line 20)

Examples of methods in methods.rb file:

Screen Shot 2018-02-25 at 1.39.19 AM.png

Screen Shot 2018-02-25 at 1.17.20 AM.png


Test the codes:

More on Methods

In ruby, every method returns a value by default (last statement in method). (Line 5)

The keyword return allows for one or more items to be returned. (Line 12)

Instead of a definite number of parameters, methods can be declared with a variable number of parameters. The * signifies that the arguments passed into that method can be any number (can be zero, one, two, and so on). (Line 17, 23-24)

Methods outside of a class are known to be private. While inside a class, they are known to be public. This can change with the keywords public or private added in front of the methods. (Line 1)

A method declared with a class name does not require the creation of an object in order to access that method. (Line 31, 37)

Examples of more methods in methods2.rb file:

Screen Shot 2018-02-25 at 2.56.53 AM.png


Screen Shot 2018-02-25 at 2.57.09 AM.png

Test the code:

2.) Encapsulation

Encapsulation is a tool to restrict direct access to objects’ data and methods. In other words, it sets boundaries and is a form of data protection to prevent manipulation. Similarly to other object-oriented languages, Ruby accomplishes encapsulation by creating objects and revealing interfaces (methods) to interact with those objects.

“Encapsulation means that the internal representation of an object is hidden from the outside. Only the object can interact with its internal data. Public methods can be created to open a defined way to access the logic inside an object.” – devblast

Example of encapsulation in encapsulation.rb file:

Link to book: Beginning Ruby: From Novice to Professional

Screen Shot 2018-02-25 at 2.13.15 AM.png


Screen Shot 2018-02-25 at 2.14.22 AM.png

Test the code:


That’s the end of our adventure!!! 😉



Loops, Iterators, Recursion

Hello everyone, the focus of this blog will be on repeated actions: loops, iterators, and recursion 🙂

1.) Loops

In Ruby, there are while loops, until loops, do/while loops, and for loops.

While loops

The condition or expression of a while loop will evaluate to a boolean (True will continue the loop and False will stop the loop). May cause an infinite loop if the programmer is not careful.

Until Loops

Until loops are the opposite of while loops and will continue to loop until the condition is evaluated to be True (False will continue the loop). A different way of thinking.

Do/While loops

A do/while loop is similar to a while loop, but the difference is that the code within the do/while loop gets executed once before checking the conditional to see whether it should be executed. The conditional will always be placed at the end of the loop instead of at the beginning. The syntax is code(s) then conditional.

For loops

Unlike other languages, the for loops in Ruby is used to loop over a collection of elements. Similar to a for each loop that loops over the values in an array or hash.   (Hash- collection of unique keys and their values). For loops have a definite end.

Ex: Ages = {“Elephant” => 10, “Donkey” => 2, “Penguin” => 4}

Loop Control Keywords

The loop control constructs: break, redo, and next allows for the alteration of a normal flow in a loop or iterator.

Break terminates the internal loop; control resumes at the statement following the block. Redo repeats the loop from the start, but without checking the condition or obtaining the next element in an iterator. Next skips to the end of the loop and starts the next iteration.

Examples of loops in loops.rb file:

Screen Shot 2018-02-17 at 9.50.42 PM.png


Screen Shot 2018-02-17 at 9.45.04 PM.png

Test the code:

2.) Iterators

Iterators are methods that loop over a given set of data (arrays and hashes) and allows the programmer to manipulate each element in the collection.

By convention, curly braces ({}) for codes that are one-liners. Do and end for multi-line of codes.

Call iterator methods by using dot (.) and the name of the iterators (each, collect, select, etc.) Next place the code(s) either in curly braces ({})  or Do and end . Each time the programmer iterates over the array assign the value of the element to a variable name and place it between two lines |. Finally, write the logic for the execution.

Ruby iterators are “chainable”, stacking functionality on each other. If an iterator method (each, collect, select) is not passed to a block then an iterator object is returned. It is possible to call more methods on these iterator objects and finally pass a block. (Lines 19-23)

More iterator methods can be found here: iterator methods: Times, Upto and Step, etc.

Examples of iterators in iterators.rb file:

Screen Shot 2018-02-17 at 10.22.30 PM.png


Screen Shot 2018-02-17 at 10.22.46 PM.png

Test the Code:

3.) Recursion

In Ruby, another way to create a loop is by using recursion. Recursion calls a method from within itself. For a recursion to terminate, a condition where the function does not call itself is needed.

Note: Recursion is not as preferable as iterators.

Reasons why recursion shouldn’t be used: Lists of Reasons

Example of recursion in recursion.rb file:

Screen Shot 2018-02-17 at 10.55.54 PM.png


Screen Shot 2018-02-17 at 10.56.02 PM.png

Test the code:

That’s the end of the various ways to repeat actions!!!