Thinking Like a Programmer
How To Convert Human Logic Into Computer Logic
If you’ve studied (not just read once) the previous chapters of this tutorial now you have all the components you need to build Ruby programs.
- Strings, Arrays & Hashes
- If statements
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!
Planning Your Ruby Project
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 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:
- Read list of mp3 into array
- Sort list by file size
- Print sorted list
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:
- “How do I get a list of files in Ruby?”
- “How do I sort an array?”
- “How do I find the file size?”
You can ask Mr. Google for some help.
Tips For Effective Problem-Solving
Read these a few times until they sink in:
- Start with a plan
- Make sure you understand the problem you’re trying to solve
- If something doesn’t work look for error messages, they often have hints that help you find out what’s wrong
- You always want to build towards a solution, one step at a time.
- You can store temporary data in an array, string or hash to build this solution.
- Think like a car factory, every step the cars gets closer to completion. Every step has one job.
- Break down big steps into smaller steps.
- Remember previous exercises & try to draw parallels from that experience.
- Use irb to your advantage & test things in isolation.
- Work “inside out”. Figure out how to make something work for one case then generalize with a loop.
- If you are writing code directly in a file run your code often so you can find errors early.
- You’re not supposed to know everything. It’s ok to use Google & the Ruby documentation. This will be part of your job even after many years of experience, get used to it.
- Details matter! A missing parenthesis, a typo, or not having the right data type (Ruby class) can cause your code to not work as you expect.
How to Handle Error Messages
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:
- Where the error is located
- What kind of error you’re dealing with
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.
Finding Ruby Methods
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:
|String -> Integer
|Integer -> String
|String -> Array (individual characters)
|String -> Array (split by spaces by default)
|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.
- Nokogiri helps you read HTML code & extract information from it.
- Thor helps you write command-line applications.
- RSpec & Minitest help you write tests to check if your code is working correctly.
You don’t need the gems to do these things, but they can save you a lot of work.