Most beginner Ruby bugs come from a handful of habits, not from advanced syntax. Fix these seven and your programs become easier to read, test and change.
1. Using puts as a universal debugger
puts is fine for output, but it hides the difference between nil, "" and 0, and it returns nil. A beginner writes puts user and sees a blank line, then spends 10–15 minutes checking the wrong method. Use p or pp while debugging: p user prints nil clearly and keeps quotes around strings. Run a one-line check in irb before editing the file. In Ruby 3.2 and 3.3, p is available without a require. Keep puts for user-facing text; use p when you need to see the actual object, including its class and structure.
2. Treating symbols and strings as interchangeable
"ruby" == "ruby" is true, but :ruby == "ruby" is false. Strings are mutable objects; symbols are lightweight labels often used for hash keys. A beginner writes params["name"] when the hash uses :name, gets nil, and adds a fallback that hides the bug. Decide on one convention per hash: { name: "Ada" } uses :name. Also know that == compares values while equal? checks object identity; "ruby".equal?("ruby") is false. If you need identity, say so deliberately. If you need a string key, use "name" consistently and test the lookup with one small hash before wiring it into a larger program.
3. Mutating strings and arrays without expecting aliases
This is the classic two-line trap: name = "ruby", greeting = name, then name << "!". Both variables now point to "ruby!", because << changes the object in place. The same happens with a = [1, 2]; b = a; b << 3: a becomes [1, 2, 3]. Use + for strings and arrays when you want a new object, or call dup before changing a shared value. Add # frozen_string_literal: true at the top of files to turn accidental string mutation into an immediate error. That one line prevents a whole class of 20-minute debugging sessions, especially when a helper method quietly changes its argument.
4. Ignoring nil until it crashes
Ruby’s nil is an object, not an empty string. nil.to_s gives "", but nil.upcase raises NoMethodError: undefined method 'upcase' for nil:NilClass. Beginners often add a broad rescue instead of finding where nil entered. Use &. for expected absence (user&.name), fetch with a default for hashes (options.fetch(:limit, 10)), and Array() or to_a only when the conversion is meaningful. Check the edge cases that produce nil: missing hash keys, failed lookups, methods that return nil on purpose, and uninitialised instance variables. A 5-minute check at the boundary is cheaper than a rescue around the whole program.
5. Writing long methods instead of using enumerable methods
A beginner often writes a 15–20 line method with each, a temporary array and an if, when Ruby has map, select and reduce. each returns the original collection; map returns a new array of transformed values; select returns a new array of matches. If a method needs more than 10–15 lines or has three distinct jobs, split it. Name methods after one action, keep them under 20 lines where possible, and prefer a clear block over a nested loop. This is not about style: short methods make the next mistake visible, because you can test one behaviour instead of guessing which part of a 40-line method failed.
6. Waiting until the program is finished to write tests
A beginner writes 200 lines, runs the file once, and then cannot tell which change broke what. Write the first test within 20–30 minutes of starting a feature. With Minitest, one test file can check three cases: normal input, nil, and an empty value. Run the test file directly with ruby test.rb; a small suite should finish in under 1 second. Test the bug before fixing it: reproduce the failure, make the smallest change, then run the test again. Tests are not a separate subject; they are how you stop repeating the same Ruby mistake. Keep the first three tests small enough to read in under 2 minutes.
7. Installing gems globally and ignoring Bundler
A beginner installs 5–10 gems globally, then cannot reproduce the project on another machine. Keep a project to 2–3 gems while learning. Put them in a Gemfile, run bundle install, and run code with bundle exec ruby app.rb so the locked versions are used. A typical small gem install takes 5–10 minutes on a home connection; if it takes much longer, stop and read the error rather than installing more packages. Check ruby -v and bundle exec ruby -v when behaviour differs between terminals. The lockfile is part of the project; commit it so another beginner can run the same code with the same versions.
Diagnosis: why code works in irb but fails in a file
Symptoms: a line works when typed into irb, but the same file raises NameError, LoadError or prints nothing. Likely causes: a missing require or require_relative, a method called before its definition, a local variable used outside its scope, or a relative path that depends on the terminal’s current directory. The fix is to run ruby -w app.rb from the project root, read the first warning, and reduce the file to a 10-line reproduction. Check that every external file is loaded with require_relative, define methods before calling them, and print __FILE__ and Dir.pwd if a path is involved. If the 10-line script works, add the original code back in small pieces until it fails. Do not guess; bisect.
FAQ
Is it a mistake to use puts while learning Ruby?
No. puts is the right tool for user-facing output. The mistake is using it for inspection. Use p when you need to see nil, empty strings or quotes, and switch back to puts for the finished message.
Should beginners learn each before map?
Yes, learn each first so you understand iteration, then learn map and select because their return values differ. A 10-minute irb session with [1, 2, 3].each { |n| n * 2 } and [1, 2, 3].map { |n| n * 2 } makes the difference concrete.
How do I stop repeating the same Ruby mistakes?
Keep a one-page error log. For each bug, record the exact error message, a 10-line reproduction, and the fix. Review the log every 2 weeks. If the same mistake appears twice, add a test or a # frozen_string_literal: true header so the code catches it for you.