Skip to content

Purogo ReducingRepetitionWithRuby

Thomas E. Enebo edited this page May 17, 2012 · 1 revision

After making our first drawing in Purogo-MyFirstDrawing we noticed there was some repetition. Purogo is really more of a Ruby library than it's own language. This means anything we can do in the Ruby programming language we can do ub Purogo too. So let's use the 'times' method to make a better version of the square program. Enter the new version as 'square2':

turtle("simpler square") do
  4.times do                
    forward 5
    turnright 90
  end
end

When you run this program (/draw square2) you will notice this generated the same square. '4.times' just ran the contents betwen 'do...end' 4 times. Simple huh? Let's now generate another polygon. How about a hexagon? A hexagon has 6 sides and turns 60 degrees (polygon angles always are 360 / # of sides [e.g. 360 / 6 = 60]) at a time. Entry this new version as 'hex1':

turtle("is this a hexagon") do
  6.times do
    forward 5
    turnright 60
  end
end

So when you run this program (/draw hex1) something looks a little odd right? Things are a little irregular. This is because we are making a reasonably small hexagon in comparison to the size of the blocks we are using. Perhaps you want to modify this to be bigger:

turtle("is this a hexagon") do
  6.times do
    forward 15
    turnright 60
  end
end

Still not perfect but much better. I mentioned that all polygons angles are a function of their sides. Perhaps we should consider making something a little more general to make any kind of polygon? Before we can do that let's learn about Purogo-UsingParameters.

Clone this wiki locally