Lesson 1

Today’s is the first of the ruby lessons. As stated we’ll be going very gently through this exciting realm, the thorny wilderness that surrounds the tower of the philosopher programmer kings.

In this class we’ll:

make sure that your armour fits

SketchUp can be downloaded from here,

and the Ruby console for SketchUp from here.

Once you have installed SketchUp you just need to unzip the console into your SketchUp plugins folder. You can find that by going to C:\Program Files (x86)\Google\Google SketchUp 7\Plugins.

say a few words to launch the adventure

To start your Ruby writing career you’ll need a place to write. You can write on a piece of paper on a tram, but it won’t compile, you’ll need to send it down to the typing pool first. The best place to write your code is into an editor. To launch the editor you go to your plugins menu and click on it, simple as that!

The editor comes with a few lines of ready written code, but essentially it is an empty place for you to jot down your ideas.

There are lots of ways that the computer can talk to you, text, sounds, pictures, moving a robot arm to pinch your bum. We are going to lean on SketchUp to do our pictures, but to see any text output from Ruby we also need to see the Ruby console. That lives in the ‘window’ menu. The console is really useful to look at, but pretty dull.

You should have three windows on your screen, arrange them so that you can see them all at the same time.

Now for your very first bit of programming! Type puts 'hello world' and press the play button in the editor.

By now you should be getting comfortable with the editor. We are going to do some text based experiments, so don’t worry too much about the SketchUp graphics window for the moment, we’ll get to that in lesson 2.

Lets learn about some of the fundamental things that we’ll need on our quest.

check what is in our saddle bags

Variables are places to store information, a bit like a jar. Lets make a jar to put our name in.

Type myName = 'the Ruby knight' or whatever your name really is, and press enter.

a picture of some marmalade from wikipedia.This puts the string the Ruby knight into the jar labled myName. This means that whenver we ask for that jar we get the words that are inside. This might seem a bit odd, but at breakfast when you ask for the marmalade, you aren't interested in the marmalade jar, but the marmalade in the jar. As long as you remember the marmalade, veriables will be simple.

On the next line type puts 'hello ' + myName and press the play button – look in the console window for the output. Notice the space after hello that's there so that when you put the two words toghther they don't bump into each other.

There are lots of different types of variables:

numbers: 1, 5000, 20, or like 4.5, -700.45

strings: 'hello world', "my style is the best"

Booleans: true, false

and lots of others, there is a bit more info here.

pack out the bandoleer


Imagine that we have a lot of carrots, we’ll need a way of keeping them in order; we can do this with an array. I like more variety than just carrots, so try:

fruits = ['apple', 'orange', 'dragon fruit'] ← this is an array

If we want a way of saying hi in different ways we could build this array:

helloWords = ['hello', 'hi', 'wotcha', 'Greetings', 'Hey', 'Salutations', 'Hi', 'Hiya', 'Yo', 'Pip pip cheerio', 'Top o the mornin to ya', 'Hola', 'Howdy', 'Howzit']

When we need to get into the bandoleer and pull out the specific carrot that we want. We do this with an index.

puts fruits[0] will give us ‘apple

WOAH, hang on a second, index 0? WTF? Well programming languages count from 0, so 0, 1, 2, 3… get used to it.

There are 14 things in the helloWords array, so because we index from 0, the highest index is 13, and because the random number generator gives us numbers under the number we give it, we need to give it 14. It's easy once you know how, but gettign there will make your brain hurt!

Lets do a little trick. To get a random number from ruby we say rand(a number) and that gives you a random number between 0 and 1 less than that number. We can use a random number to index an array – so, putting it all together, try puts helloWords[rand(14)] + myName

At this point your code will look something like this:

puts 'hello world'
myName = 'the Ruby knight'puts 'hello ' + myName
fruits = ['apple', 'orange', 'dragon fruit']
helloWords = ['hello', 'hi', 'wotcha', 'Greetings', 'Hey', 'Salutations', 'Hi', 'Hiya', 'Yo', 'Pip pip cheerio', 'Top o the mornin to ya', 'Hola', 'Howdy', 'Howzit']
puts fruits[0]
puts helloWords[rand(14)] + myName

a postit with the word 'strings' on itexchange riddles with the cat

Strings are words, they are the way that the computer interprets things that we say and remembers it. They are a bit like post it notes with messages on them This would be a good place to introduce some help. The rubydocs to be precise. They are a repository of all there is to know about the core of Ruby. Here’s the string help. This explains all the methods that ruby uses. lets play some word games.

puts "repaid deliver".reverse
puts "restaurant".includes('aura')
puts 'mieman'.insert(2, 'nut')

By using the docs try and work out what each of these statements will do before you actually run them.

The eagle eyed amongst you might have noticed that sometimes I use ” ” and other times I use ‘ ‘ to enclose the strings. In Ruby they are more or less interchangeable, but ‘ ‘ is a but faster. There are times that you’ll need to use ” but they are rare (regular expressions).

Eat a bowl of fruit loops

The thing that makes computers worth using is that they do simple things very fast. Usually it is the same thing that they do each time, and the thing that does this is called a loop.

There are a few ways to make loops happen. The most common is the for loop.

for i in (1..10)
  puts 'kick'
  puts 'high hat'
  puts 'kick'
  puts 'snare'

you can also say:

  puts 'kick'
  puts 'high hat'
  puts 'kick'
  puts 'snare'

There are a load of other possibilities here but lets try to use this in a useful way,

for i in (1..10)
  puts (10 - i).to_s + ' green bottles - hanging on the wall'

this will write out:

9 green bottles – hanging on the wall
8 green bottles – hanging on the wall
7 green bottles – hanging on the wall
6 green bottles – hanging on the wall
5 green bottles – hanging on the wall
4 green bottles – hanging on the wall
3 green bottles – hanging on the wall
2 green bottles – hanging on the wall
1 green bottles – hanging on the wall
0 green bottles – hanging on the wall

There are a couple of tricky looking things there, so lets clear them up.

1..10 gives you all the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 whereas 1…10 with three dots gives you 1, 2, 3, 4, 5, 6, 7, 8, 9.

First of all i is a variable name that is currently a counter. It needn't be i, but convention sayst that it should be; it could be a or bobby, but people that i counts and that mack has a knife. So i counts from 1 to 10. What is happening here is that the loop processes for each value of i. It doesn’t take a genius to work out what (10 – i) gives you, when i = 1 then (10 – i) = 9 see, I told you that there was no hard maths in programming!  Remember that we could add strings to eah other – 'hello' + ' it's me again' = 'hello it's me again' but you can’t mix apples and oranges. The problem is that we can't add a number and a string.What we can do is convince an apple that it really is an orange, so by using the .to_s method converts a number into a string (to_s is short for to string).

That’s looping covered, two more things and then we’ll have programming all sewn up.

Conditional Love

If it is raining then I’ll get wet, unless I’m inside.

This is one of the places that Ruby is particularly lovely. It allows for (and you should strive for) your code to read out loud like a sentence.

it_is_raining = true

if it_is_raining
  puts "remember your umbrella"

We can make that a bit more complicated, and a little more useful

it_is_raining = true
i_am_inside = false

if it_is_raining
  unless i_am_inside
    puts "remember your umbrella"

How cool is that? Read it out loud, for real – don't just pretend to, and it’ll all make sense. Try swapping the true and false values about to see what happens.

it_is_raining = true
i_am_inside = false

if it_is_raining
  unless i_am_inside
    puts "remember your umbrella"
  puts 'slip slap slop'

That last snippet of code is important, it introduces the idea of what to do if it’s all going differently. The else clause tells you what to do if the first question comes up false.

There is a lot more to think about when writing conditional statements, but we’ll cover that in another lesson. This article is a pretty good way to understand conditionals.

And Finaly

There is only one last thing you need to know how to do, and you already know how to do it! When you write computer code, you need to spare a thought to your poor future brain, it needs to know what you were thinking, so you need to write comment on what your code actually does!

# this program was written on the 9/2/2011 by Ben Doherty
# it prints sartorial advice dependent on precipitation and location

# declare some variables
it_is_raining = true
i_am_inside = false

# get moving on the real meat of it all
if it_is_raining                   # if it_is_raining holds the value true then enter this block
  unless i_am_inside               # if i_am_inside holds the value false  then enter this block
    puts "remember your umbrella"  # write remember your umbrella to the console
  end                              # end the unless block
else                               # if it_is_raining holds the value false then enter this block
  puts 'slip slap slop'            # write slip slap slop to the console
end                                # end the if block

The # is the key to all of this. The compiler ignores everything from # to the end of the line. The way I like to do things is to have a header that says who and when, and then what, then following that, revision descriptions. There more you comment your code the better your life will be in the future.


Have a play with loops, conditionals and the string library and make up some complicated wordplay. Points for humor and slyness! Remember to comment extensively! Post your results in the comments below.


One thought on “Lesson 1

  1. Pingback: The gist of things | BVN ruby lessons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s