Beginner Best Practices

Some helpful tips for approaching common situations with Ruby.

Scroll down...

Content

Resources

Comments

Ruby can be used to solve all sorts of interesting problems and it contains a whole lot of different helper methods to get you there. In fact, there are so many interesting methods for doing things like manipulating arrays or iterating that beginners often end up either confused or coming up with some odd ways of doing things.

In this quick lesson, we'll just cover a few very basic situations and how you should approach them with Ruby. This is intended to cut through the noise when you're approaching simple logical problems like the ones you'll see in the coming assignment and during the VCS application process.

Variable Naming

Name your variables descriptively. That should be the end of the discussion but sadly beginners seem to love to use names like i, counter, array, or num. What do these variables represent??? Every problem has some sort of real-world context -- your variables should be named to reflect it! It will help you enormously to keep from getting lost inside your problem. This comes from experience.

Don't worry about making your names too long when you're first starting out. It's better to be too descriptive and eventually pare it back than to use garbage like i unless there's absolutely no better way.

Indexing from Zero

Just about everything starts at 0 instead of 1. This is a common programming convention. The times iterator starts at 0. Array indexes start at 0. The each_with_index iterator starts at 0. Just assume something starts at 0 unless you're sure otherwise!

Indenting Code

Focus on indenting your code properly. Everything that opens up a new loop, block, or logic chain (ie. anything that finishes with an end) should be indented. It doesn't just look terribly amateurish to have poorly indented code but it also makes it MUCH more difficult to figure out why you're getting odd errors.

The bottom of your code will probably look like it's got a cascade of ends.

def some_method
some_array = []
4.times do |i|
if some_condition
# do something
else
# do something else
end
end
some_array
end

It's okay to start off using four spaces or a tab when indenting (as long as you're consistent). Try to start using 2 spaces instead when you get better at reading code (it's more conventional).

Iterating

"I need to do something some number of times, so I think I'll set up a triple nested looping structure using three different iterators all with the same variable i" -- we've seen this more than once

Beginners often find 1001 different ways to iterate. You really only need to know 3 or 4 for right now. And very few people use for loops in Ruby, so you can mostly forget about that. The key to iteration is to KEEP IT SIMPLE! Just use the most basic iterator that covers what you want to do.

When you know exactly how many times you want to iterate through something, just use the times method:

def three_cheers
3.times do
puts "Hip Hip, Hooray!"
end
end

A more common situation is iterating through each item in an array or hash (this comes up a lot when working with data in web applications) without actually modifying any of those items. Luckily, Ruby has the simple each method:

# Read an array that represents a list of groceries
def read_grocery_list(grocery_list)
grocery_list.each do |grocery_item|
puts "buy #{grocery_item}"
end
end

If you want to actually collect a new array filled with modified items of your original array, then you'll use the map method instead. For solving simple logical problems, this is again not terribly common (it's more often used with database data and things like that).

When looping as long as some condition is true, use a while loop. BUT -- you usually know exactly how many times you need to iterate, which would call for one of the methods above. while is most useful for more complex problems like games which want to continue looping until someone has lost:

def play_game
while player_1.lives > 0
# Do stuff to play game
end
end

String, Array and Hash Operations

There are a LOT of random string, array and hash methods out there. You of course need to be good at creating strings, arrays and hashes and accessing specific items inside of them, but there aren't a whole lot of other methods that you'll frequently encounter.

If you want to just look at or modify the last item, you can index backwards from the end of the array. -1 is the last item, -2 is the second to last, etc. If you actually want to remove the last item, just pop it off. The alternative, push, acts like the shovel operator above.

Paying Attention to Return Values

Make sure you know what your methods are returning. Just about everything in Ruby is a method and methods always return something (or nil). Some common things that trip up beginners:

A method automatically returns the result of its last line if there's no explicit return statement given. If your method is returning something weird, it's probably because you're not properly thinking about this.

each returns the original array. This is the most commonly forgotten fact.

puts returns nil. This is the second most commonly forgotten fact.

map returns the modified array.

Executing a Ruby Script

Instead of typing everything on the command line with IRB, try storing your ruby code in a .rb file. Put a line at the end which actually executes any methods you want to execute and then run it from the command line using the normal $ ruby your_file.rb.