Troubleshooting - Ruby Wizardry: An Introduction to Programming for Kids (2014)

Ruby Wizardry: An Introduction to Programming for Kids (2014)

Appendix B. Troubleshooting

When running your Ruby scripts or using IRB, you may run into some common errors. I’ve listed a few of them here, along with tips for fixing them!

Errors Running Ruby Scripts

There are two common errors that you might see when running Ruby scripts from the command line: Command Not Found and “no such file or directory.” Here are some suggestions for how to resolve them.

Command Not Found

If you’re running a Ruby script and you get some output that looks like this:

$: command not found

it probably means you accidentally typed a $ before your ruby command. I use the $ symbol to show you that you’re running a Ruby script from the command line with a filename (like ruby my_fancy_script.rb); you shouldn’t type the $ itself!

No Such File or Directory

If you get an error that looks like this:

No such file or directory -- some_filename.rb (LoadError)

it means you tried to run ruby some_filename.rb, but that file didn’t exist in the folder you’re currently in.

To fix this, first make sure you’re in the folder where you saved your Ruby script. You can change from one folder to another using the cd command (for “change directory”). See Creating Your First Script for help using the cd command.

If you’re in the correct folder and your command still gives you an error, double-check the spelling of your file! (I mistype the names of Ruby files all the time.)

Errors Using IRB

There are a few common errors that you might see when using IRB. Here’s how to fix them, along with some other handy tips for fixing typos and mistakes.

Undefined Local Variable or Method

If you try to call a method in IRB and get something like this:

NameError: undefined local variable or method `some_method_name' for

main:Object

it means you tried to use a method that Ruby doesn’t know about. When you exit and restart IRB, Ruby forgets everything you were previously doing—so if you defined a method, exited IRB, and started it again, you’ll need to redefine that method to keep using it. (See Defining Your Own Methods if you need a refresher on how to define methods.) If your method is from a file, make sure you load that file using the command load 'your_file.rb', and if all else fails, double-check that you’ve spelled your method name correctly.

Syntax Error

If you get an error that looks like this:

SyntaxError: (irb):1: syntax error, unexpected 'something_here'

it means you wrote Ruby code that’s not quite right, and IRB doesn’t know what to do with it. Double-check your code for tiny errors, like typos, missing commas between elements in arrays, or missing hash rockets (=>) or colons in hashes.

Can’t Convert nil into String

If you get an error like this:

TypeError: can't convert nil into String

it means you tried to do something with one Ruby type (like a string, integer, or nil), but Ruby expected a different type. This often happens when something is nil and you don’t know it; if you see this error, try putsing out the values of all your variables to make sure each one is the type of thing (string, integer, array, and so on) that you expect! (See Getting to Know IRB for help with the puts command and A Bit More About Variables for a refresher on the types of variables.)

You Were Saying . . . ?

From time to time, you might see Ruby print something like this:

...?

This means that Ruby expects you to “finish your thought.” Usually it means you pressed ENTER without closing a string, or maybe the last thing you typed was a + or - sign. All you need to do is finish that thought—complete the expression you started to type, close the string or array you opened, or whatever it is Ruby is waiting for—and you’ll be all set. For example:

>> 1 +

...? 2

=> 3

If you have no idea what Ruby is waiting for, or you simply mistyped and want to start over, you can press CTRL-C to tell IRB not to wait for you. You’ll get your regular IRB prompt back and can continue from there. (For more about CTRL-C, see Investigating the Kingdom’s Web Server.)

Clear the Screen

Sometimes you’ll type a whole bunch in IRB and will want to clear the screen. You can do this in several ways, depending on which operating system you’re using. On a Mac, you can press ⌘-K or CTRL-L, or you can type system 'clear' into IRB and then press ENTER. If you’re using Linux, typing CTRL-L or entering system 'clear' should work. If you’re using Windows, typing CTRL-L or entering system 'cls' (not 'clear'!) should do the trick.

Go Back to a Previous Command

If at any point you want to go back to a previous command you typed into IRB, just hit the up arrow on your keyboard! This is great if you just cleared the screen and then realize you need to retype a command, or if you mistyped a command and want to try again without retyping everything you just did.

Look It Up!

Finally, if you ever see an error that you don’t know how to handle, go ahead and search for it on the Internet (after you get your local adult’s permission!). Everyone gets errors, so it’s likely that someone else has already figured out how to handle any error you might run into. Even the best programmers look up things they don’t know on a daily basis. The more comfortable you get hunting for answers when you’re stuck, the happier and more productive you’ll be when writing Ruby.

Updates

Visit http://nostarch.com/rubywizardry/ for updates, errata, and other information.