Lesson 1

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

make sure that your armour fits

(get ruby and shoes installed on your machine)

Ruby can be downloaded from here.

And Shoes comes from here.

say a few words to launch the adventure

(‘hello world’)

Royce helpfuly pointed out that to get ruby running on a mac all you need to do is type irb into the console, and you are running.

launch “Interactive ruby” and you’ll be greeted with a very programmery interface:

type puts 'hello world' and press enter.

check what is in our saddle bags

(variables)

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.

then type puts 'hello ' + myName and press enter.

the second put looks in the jar to find out what our name is.

lift the visor, take in the view and talk about it

Code is supposed to be interactive and reusable, it’d be good to be able to make the same bit of code do different things. lets get an input

myName = gets this grabs a user input, so it’ll leave a blinking line on the irb (tech speak for Intereactive RuBy interface)

then type puts 'hello ' + myName again and press enter.

pack out the bandoleer

Bandolier_of_Carrots

Imagine that we have a lot of carrots, we’ll need a way of keeping them in order.
fruits = ['apple', 'orange', 'dragon fruit'] is an array
so 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']
but then 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)] + gets

exchange riddles with the cat

(strings)

This would be a good place to introduce some help. The rubydocs to be precise. 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')

Gaze into a crystal ball

(the shoes graphics interface)

we’ll just smell the shoes, and wear them for even less time than Cinderella.

Make a new text document and save it as newShoes.rb, then open it and paste in:

Shoes.app :width => 300, :height => 200 do
   button("Click me!") { alert("Good job.") }
end

and save it…

the shoes window

open shoes, then open an app, then pick your newShoes.rb file.. jobs-a-good-un

Homework

play with the string library and make up some complicated wordplay. Points for humor and slyness!

Advertisements