0% found this document useful (0 votes)
7K views9 pages

Thinking Like Programmer - RubyGuides

The document discusses how to think like a programmer by breaking problems down into steps, finding relevant Ruby methods, and handling errors. It provides tips for planning a Ruby project, such as writing steps in plain English and refining them as code is implemented. Error messages are described as helpful for locating and identifying issues. Methods are resources for accomplishing tasks with different data types.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views9 pages

Thinking Like Programmer - RubyGuides

The document discusses how to think like a programmer by breaking problems down into steps, finding relevant Ruby methods, and handling errors. It provides tips for planning a Ruby project, such as writing steps in plain English and refining them as code is implemented. Error messages are described as helpful for locating and identifying issues. Methods are resources for accomplishing tasks with different data types.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

1 Learning Ruby

2 Understanding Variables

3 Working With Collections

4 If / Else Conditional Statements

5 Ruby Loops: Repeating


Something Many Times

6 Thinking Like A Programmer

7 Object-Oriented Programming

 Download eBook

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.

https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 3/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

To review:

Variables
Strings, Arrays & Hashes
If statements
Loops

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
https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 4/12
Let s say that I have a folder fullThinking
03/04/2024, 12:16
of mp3 files & I want to print them
Like a Programmer - RubyGuides

sorted by file size.

These are the steps I’d write:

1. Read list of mp3 into array


2. Sort list by file size
3. 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.

Now:

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-


S l i
https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 5/12
Solving
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

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
j b f
https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ f i d i 6/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

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:

1. Where the error is located


2. What kind of error you’re dealing with

If you get an error like this:

1. exception.rb:7:in 'bar': undefined local variable or


method 'bacon'
2. from exception.rb:3:in 'foo'
3. from exception.rb:10:in 'main'

This tells you that the error started at the exception.rb file, on line 7 .

That’s a good place to start your investigation.

https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 7/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

In this message you’ll also find the error type:

1. 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:

1. undefined method `foo` for nil:NilClass


(NoMethodError)

This happens because you are trying to call a method on nil .

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:

1. letters = ['a', 'b', 'c']

If you try to access it with an invalid index:

1. letters[5]

Ruby allows you to do that, but you get nil .


https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 8/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

Then if you try to do something with that value:

1. letters[5].foo

You get the undefined method error you saw above.

One solution is to check if you’re working with a non-nil value:

1. if letters[5]
2. letters[5].foo
3. 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.

For example:
https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 9/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

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 class method.

Example:

1. "apple".class
2.
3. # 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:

ME T H O D CO NV E RSI O N
https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 10/12
03/04/2024, 12:16 Thinking Like a Programmer - RubyGuides

to_i String -> Integer

to_s Integer -> String

String -> Array (individual


chars
characters)

String -> Array (split by spaces by


split
default)

Array -> String (join without


join
spaces)

Both split & join take an optional parameter where you can specify
the separator character.

Example:

1. "a-b-c".split("-")
2.
3. # ["a", "b", "c"]

Ruby Gems
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.


https://www.rubyguides.com/ruby-tutorial/thinking-like-programmer/ 11/12

You might also like