If you studied (not just read once) the previous chapters of this tutorial now you have all the components you need to build Ruby programs.
With these 4 things, plus Ruby built-in commands (we call these methods) you have all the power of Ruby in your hands.
But how do you build your own programs?
Where do you start?
Start with a plan!
I want you to get an idea of what you want to build.
What will this program do?
Start with something simple.
When you know what you want to do write a list of steps in plain English.
Here’s an example:
Let’s say that I have a folder full of mp3 files & I want to print them sorted by file size.
These are the steps I’d write:
It’s like a recipe.
It might not be perfect the first time, that’s ok.
You’ll refine the steps as you put them into practice.
To turn these steps into code you’ll need to get creative & use everything you have learned about Ruby.
Go over every step & make it into a “how” question:
You can ask Mr. Google for some help.
Read these a few times until they sink in:
It’s completely normal to get error messages.
Error messages are there to help you.
Most of the time the error message has information to help you fix the problem.
There are two things you want to look for in an error message:
If you get an error like this:
exception.rb:7:in 'bar': undefined local variable or method 'bacon' from exception.rb:3:in 'foo' from exception.rb:10:in 'main'
This tells you that the error started at the
exception.rb file, on line
That’s a good place to start your investigation.
In this message you’ll also find the error type:
undefined local variable or method 'bacon'
This means you tried to use something with the name of
bacon, but Ruby can’t find anything with that name.
It’s possible that you made a typo, or maybe you forgot to create that variable.
Another error that’s very common looks like this:
undefined method `foo` for nil:NilClass (NoMethodError)
This happens because you are trying to call a method on
Many Ruby commands (methods is the more technical term) can return this value to you when they can’t find what you’re asking for.
Let’s say you have an array:
letters = ['a', 'b', 'c']
If you try to access it with an invalid index:
Ruby allows you to do that, but you get
Then if you try to do something with that value:
You get the undefined method error you saw above.
One solution is to check if you’re working with a non-nil value:
if letters letters.foo end
If you find another error that you don’t understand you can drop it into Google & you’ll find some hints on how to fix it.
When converting your steps into code it’s useful to know what you’re working with.
If you are starting with a string then string methods will be useful.
If you are starting with an array then look for an Array method that does what you want.
You can find all the available methods using the Ruby documentation.
If I have a string like “a,b,c,d” & I want to break it down into an array of characters without the commas.
I may notice the
split method in the String documentation:
A quick test in irb reveals that this is what we were looking for!
If you don’t know what kind of object you are working with you can use the
"apple".class # String
If you’re working with a string, but you want to do something that only an array can do, or you’re working with an integer but you want to work with the individual digits.
You can use these conversion methods:
|to_i||String -> Integer|
|to_s||Integer -> String|
|chars||String -> Array (individual characters)|
|split||String -> Array (split by spaces by default)|
|join||Array -> String (join without spaces)|
join take an optional parameter where you can specify the separator character.
"a-b-c".split("-") # ["a", "b", "c"]
Sometimes what you want to do is more complicated than this.
You may want to pull down data from a website & find images to download.
In that case what you’re looking for is a Ruby gem.
Ruby gems are small Ruby applications that you can add into your program & they’ll help you do something.
You don’t need the gems to do these things, but they can save you a lot of work.