S
simonh
Hello all. I am learning to program with ruby but I'm struggling to
find a really good tutorial or book aimed at beginners. The pickaxe 2nd
ed. is an incredible book but can be a little intimidating for
beginners. Many of the other books cover ruby 1.6. The available
tutorials I've found either don't go far enough or spend too long
telling stories (sorry Why)! Anyway, I think it would be beneficial to
have a set of tutorials in the form of dialogues similar to the
artcompsci project:
link: http://www.artcompsci.org/kali/development.html
but aimed at non mathematical problems. Here is a sample of what I am
proposing: (bear in mind I am a newby too). I think it would be better
for a ruby guru to write the dialogues.
-----------------------------------------------------------------------------------------------------------------------------------
T- Teacher
S- Student
T: Ok lets get started. Here is a 'hello world' program. This is
usually the first program a student writes in any programming language.
All it does is print the phrase Hello World onto the screen.
puts "Hello World"
T: How do you think this program works?
S: It looks to me like the word puts is a command. The phrase Hello
World is the text that the command should print out. I'm not sure why
the phrase is quoted though. The quotes don't appear on the screen.
T: Well spotted. The phrase Hello World is what we call a String. We
identify Strings by enclosing text within quotation marks. We use
single ( ' ) and double ( " ) quotes in Ruby. We'll come to the
differences between using single or double quotes later on. You
suggested that puts was a command. In fact it's a method. Methods are a
core concept of Ruby so fix that in your mind. Here is a description of
what the program is doing: The method puts receives the String Hello
World as its argument. puts is a method defined in the Kernel module.
We'll come to that later as well.
S: Does it matter if puts is upper or lowercase?
T: Try it out
S: Puts "Hello World"
S: Obviously it doesn't work. An error message has been printed.
T: You should get used to reading error messages straight away. Ruby is
telling us that the error occurred in line 1 beginner.rb:1 it then
tells us the error. We have not defined a method 'Puts' for main:Object
In Ruby methods should always be lowercase. Classes, Modules and
Constants are uppercase. For now don't worry about any of these new
terms. We will cover them before long.
S: It seems there is a lot of stuff we will be covering later on!
T: Yes I know. Its quite hard to know where to start with someone who
has no programming experience. My plan is to introduce you to some
basic Ruby and then explain things at the right time. I promise you'll
understand all of these concepts before long.
S: Ok, I'll take your word for it. What's next?
T: Rather that use the method puts we can use print instead
print "Hello World"
T: The difference is that puts will print the string and insert a
newline character where as print will not. See the cursor is now on the
same line? Change print to puts and see where the cursor is then.
S: Oh right. Yep I see what you mean.
T: Try this
puts "Hello World"
puts "I am learning Ruby!"
T: Now do the same thing with print
print "Hello World"
print "I am learning Ruby!"
find a really good tutorial or book aimed at beginners. The pickaxe 2nd
ed. is an incredible book but can be a little intimidating for
beginners. Many of the other books cover ruby 1.6. The available
tutorials I've found either don't go far enough or spend too long
telling stories (sorry Why)! Anyway, I think it would be beneficial to
have a set of tutorials in the form of dialogues similar to the
artcompsci project:
link: http://www.artcompsci.org/kali/development.html
but aimed at non mathematical problems. Here is a sample of what I am
proposing: (bear in mind I am a newby too). I think it would be better
for a ruby guru to write the dialogues.
-----------------------------------------------------------------------------------------------------------------------------------
T- Teacher
S- Student
T: Ok lets get started. Here is a 'hello world' program. This is
usually the first program a student writes in any programming language.
All it does is print the phrase Hello World onto the screen.
puts "Hello World"
Hello world
T: How do you think this program works?
S: It looks to me like the word puts is a command. The phrase Hello
World is the text that the command should print out. I'm not sure why
the phrase is quoted though. The quotes don't appear on the screen.
T: Well spotted. The phrase Hello World is what we call a String. We
identify Strings by enclosing text within quotation marks. We use
single ( ' ) and double ( " ) quotes in Ruby. We'll come to the
differences between using single or double quotes later on. You
suggested that puts was a command. In fact it's a method. Methods are a
core concept of Ruby so fix that in your mind. Here is a description of
what the program is doing: The method puts receives the String Hello
World as its argument. puts is a method defined in the Kernel module.
We'll come to that later as well.
S: Does it matter if puts is upper or lowercase?
T: Try it out
S: Puts "Hello World"
beginner.rb:1: undefined method `Puts' for main:Object (NoMethodError)
Exit code: 1
S: Obviously it doesn't work. An error message has been printed.
T: You should get used to reading error messages straight away. Ruby is
telling us that the error occurred in line 1 beginner.rb:1 it then
tells us the error. We have not defined a method 'Puts' for main:Object
In Ruby methods should always be lowercase. Classes, Modules and
Constants are uppercase. For now don't worry about any of these new
terms. We will cover them before long.
S: It seems there is a lot of stuff we will be covering later on!
T: Yes I know. Its quite hard to know where to start with someone who
has no programming experience. My plan is to introduce you to some
basic Ruby and then explain things at the right time. I promise you'll
understand all of these concepts before long.
S: Ok, I'll take your word for it. What's next?
T: Rather that use the method puts we can use print instead
print "Hello World"
Hello World
T: The difference is that puts will print the string and insert a
newline character where as print will not. See the cursor is now on the
same line? Change print to puts and see where the cursor is then.
S: Oh right. Yep I see what you mean.
T: Try this
puts "Hello World"
puts "I am learning Ruby!"
Hello World
I am learning Ruby!
T: Now do the same thing with print
print "Hello World"
print "I am learning Ruby!"