Next installment! On to a bit about handling text and some types of commands named "methods"
Ok, now we already talked about how to do simple math in Ruby. And you also know how to display something on the screen using puts.
How about working with text? That 's important for things like dialogs between characters and the NPC's, high score lists, etc.
So, let's see how to write the words The dragon eats the goat to the output window. You just do
puts "The dragon eats the goat"
Why is the text between double quotes? Because that's how the programming language can see that it's a text, and not soe insruction for what it should do. In the jargon of computer programmers, such text is called a "string" like a bunch of letters stringed together on a wire. In Ruby (but not in some other programming languages), you can also put text between single quotes, like
puts 'The dragon eats the goat'
And of course, with text strings you can do several things, a bit like how you can calculate with numbers. For example
s = "Bob's" + " goat"
puts s
will output Bob's goat when you run it as a program. The + can glue two text strings to each other, in the order given. And of course,
you can store a string in a variable, and fetch it again later, like , when you want to display it in the output window.
Now , let's see how numbers and text strings work together.
Let's try this:
s = "The length of Bob's name is " + 3
puts s
Well, you'll get an error like this:
TypeError: can't convert Fixnum into String
Why? Because numbers and letters aren't directly compatible with each other, at least not in Ruby. You'll have to change the 3 from a number to a text string like this:
s = "The length of Bob's name is " + 3.to_s
puts s
That's something new for you there. What's up with the period . and the to_s after the 3?
Well, in Ruby, you can use a command like puts, and the programing language will look it up in it's currently open libraries to find what it has to do. However, you can also tell Ruby that it has to do something with an individual number or string. To do that, you put a period (.) behind the thing you want to work with, followed by the name of the command that tells what you want Ruby to do with that object. In this case , you're telling Ruby, hey, i want you to take number 3, and convert it to a text String. "to_s" is short for String. In Ruby, it's common to see many short names for commands, since they're more quick to type, and also less work to remember in the long run.
There are many more things that you can ask Ruby to do to text, numbers or values. Also, even when you tore the values into variables, you can still ask Ruby to perform the commands on them that it could do when dealing with the value direcly. Another example:
b = "Bob"
l = b.length
puts "Bob's name is " + l.to_s + " characters long"
will write "Bob's name is 3 characters long" to output. You're storing the string "Bob" in a variable called b. then You ask Ruby to take the length of that text string, that is, the amount of characters in it, and store it into a variable named l
Finally, you let it change the length, which is a number, to text, and then glue all the parts of the text together, and then write it to output.
Commands like puts are called "functions" or "procedures", in the programmer's jargon. They're just instructions that the programming language looks up in the currently active libraries of the programing language, and then performs. Commands like .to_s or .length are called "methods". In Ruby, there's not too much difference between the two but it will help if you know the programmer's jargon a bit.
How to find out what "methods" a number or string or a value in a variable has? In other words, how to find out what ruby can do with them? Well, in Ruby that's simple because there's also a method to get the methods, conveniently caled .methods. Just try this:
puts 3.methods
puts "hello".methods
You'll get oodles of text in your output window, all of these are the names of the methods for the number 3 or for the text "hello" that Ruby knows.
To find out what all these commands do, you can look it up in the documentation of Ruby, like here:
http://www.ruby-doc.org/core-1.8.6/index.htmlBut for now, we don't need all those commands, but I'lll leave it up to you to explore a bit. Don't wory about using a method (command) that Ruby doesn't know, it will simply give an error message and stop there.