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 (get SketchUp and the Ruby editor installed on your machine)
- say a few words to launch the adventure (‘hello world’)
- check what is in our saddle bags (objects)
- pack out the bandoleer (Arrays)
- exchange riddles with the cat (strings)
- Eat a bowl of fruit loops (flow control – looping)
- Conditional Love (flow control – conditional statements)
- And Finally (commenting)
- Homework
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
Everything in Ruby is an object. In the programming world that is a very profound statement, but it sounds pretty silly in the real world; all the things on my desk, in my bag, the bag itself, the desk, in fact, even the building I’m in is an object (well it is a collection of objects, but we’ll get to that later!) This kind of falls apart in the real world when you talk about intangible things, love isn’t an object, it’s a feeling, but in Ruby everything it an object, and that is important to remember!
One of the things that objects can do is store information by having a value, 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.
This puts the string the Ruby knight into the jar labled myName. This is called assignment; you assign the value to the object. This means that whenever 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, values 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 classes of object:
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.
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
It’s worth pointing out that you can declare as many arrays as you like in your code as long as they have different names. If they had the same name then the latter one would overwrite the one that had already been defined.
exchange riddles with the cat
Strings are words, they are the way that the computer remembers things that we say. The computer doesn’t interpret them because strings can contain anything. They are a bit like post it notes with messages on them.
In the examples below, the words after the . are methods. So we read "hello".reverse as ‘hello dot reverse’. Methods are words that the computer does interpret. They are the commands that actually do things.
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".include?('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' end
you can also say:
10.times{
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' end
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.
First of all i is the name of an object that is currently used as 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
You aren’t allowed to have spaces in object names, ruby uses spaces to tell the difference between one thing and the next.
There are two main ways of getting past this, one is camel case whereYouCapitaliseTheFirstLetterOfEachWord and the other uses an_underscore_to_do_it you can use either, but it seems that the Ruby world tends to do the latter, and the c-style world (java, c++, c# etc.) tend to use camel case.
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" end
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"
end
end
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"
end
else
puts 'slip slop slap'
end
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 # make some objects and set their values 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.
Homework
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.



Pingback: beginners guide to the apocalypse | Blog | First Ruby lesson
Pingback: notion parallax – Ruby Lessons
Pingback: beginners guide to the apocalypse | Blog | Tutorials on tuesday