0

I'm trying to debug a ruby application where I loop through a bunch of days and check against my employees to see whether they worked that day. The code I am working on looks like the following:

def search_for_worked(day)
  # debugger
  workers = employees.select do |employee|
    employee.worked?(day)
  end
  if workers.length < 1
    raise Error

end

I know my employee.worked? method is doing something it shouldn't be and I would like to step into it with the params that are causing the problem. However the search_for_worked method is being called ~100 times before it is getting to the day where it is failing.

I can call catch Error but it halts after the method so I can't step into it.

Whats the best way to debug logic errors if I only know its doing something wrong after I've already called it?

4
  • Can you post the worked? method? Commented Feb 2, 2014 at 4:36
  • 1
    Have you considered Pry? github.com/pry/pry It will let you step into an irb session at your if workers.length < 1. You can then exit irb and it will pick up the program where it left off. Commented Feb 2, 2014 at 4:49
  • @Beartech yeah I wanted to use pry but inside of the employee.worked?(day) when it was messing up Commented Feb 2, 2014 at 6:22
  • Darcy, you might consider writing the body of this method as employees.any? {|employee| employee.worked?(day)}. That way, the method would return true if any employees worked; else nil. If you have if(search_for_worked(day))..., returning nil has the same effect as returning false. Commented Feb 2, 2014 at 8:34

1 Answer 1

1

I often use something like this:

debugger if <condition I didn't expect>

or

debugger if $foo.nil?

The second will be hit the first time through the loop, then I set $foo (a global) to something and continue.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah ended up doing that. Except I rewrote the select block afterwards so I could step into it :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.