Code a Ruby Rainbow 馃寛馃

February 6, 2018

in路ter路po路la路tion #锔忊儯



  1. the insertion of something of a different nature into something else.

Let鈥檚 program a rainbow! 馃寛 馃聽

So鈥β爓e need to talk to the machine by using the Ruby Programming Language. We have that already installed聽and setup and now we can open Mac Terminal and Xcode to聽get going.聽

But first, let鈥檚 ask ourselves some questions. We need step by step understanding on how to produce a coded rainbow. 聽 聽 聽 聽

  • What colors are in the rainbow?

  • In what format do we provide the color listing to the computer?聽

  • What method do we need to tell the machine to process (think)? 馃聽

  • How can the color listing interpolate in to the machines method (thought process)?

  • How can we tell the machine to show us what it鈥檚 thinking?聽

Let鈥檚 answer them in Xcode!聽馃寛 馃聽

The colors are:

red, orange, yellow, green, blue, indigo, and violet

The color listing format is an array set to equal a variable of 鈥渃olors鈥:

colors = ["red", "orange", "yellow" "green", "blue", "indigo", "violet"]

The defined method has an argument of the variable 鈥渃olors鈥:

def display_rainbow(colors)


What we are doing is saying: hey computer, the colors are equal to these and what your thinking about and going to process for us is called method display_rainbow.聽But how can it take the colors and interpolate them into the method and print out our 鈥渃olors鈥?

puts "R: #{colors[0]}, O: #{colors[1]}, Y: #{colors[2]}, G: #{colors[3]}, B: #{colors[4]}, I: #{colors[5]}, V: #{colors[6]}"

Woah right? The hash (鈥#鈥) means to take the data from the 鈥渃olors鈥 array and interpolate them into a hash. The numbers in a hash always begin with 鈥0鈥.聽

Now we have our ruby code in Xcode:

Let鈥檚 Play!聽Let鈥檚 program a rainbow! 馃寛 馃聽

On a Mac, open terminal & type:


Press Enter

Copy & Paste this code:

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

Press Enter

Copy & Paste this code:

def display_rainbow(colors)

聽聽 puts "R: #{colors[0]}, O: #{colors[1]}, Y: #{colors[2]}, G: #{colors[3]}, B: #{colors[4]}, I: #{colors[5]}, V: #{colors[6]}"


Press Enter

Now, Copy & Paste this code:


Press Enter

The ruby rainbow: R: red, O: orange, Y: yellow, G: green, B: blue, I: indigo, V: violet

There are no colors showing up but the program believes it鈥檚 displaying a rainbow the same way you believe you see one. 馃寛



Please reload

Recent Posts

August 4, 2020

Please reload


Please reload



  • github-153-675523
  • Black Instagram Icon
  • Black Twitter Icon