Are there any Ruby tools to help you step through the logic of your problem?

I am working on an app that involves some complex relationships between nodes (a family tree app).

I am working through each step, but because I am doing some automagic translation of relations between two people, it can easily get confusing.

It would be nice if there were something that assisted me with stepping through the logic of my problem, to prevent me creating any logic bombs.

Is there any such tool/gem? Even a website similar to Rubular or anything else like that would go a long way to helping me.

Thanks.

Pry is probably the best debugging tool currently available for ruby applications, and when used in conjunction with good old rails c, debugging Rails is pretty straightforward.

I recommend adding the gem jazz_hands to your Gemfile. It includes pry along with bunch of other little goodies for sprucing up your console.

From there itā€™s simply of matter of dropping binding.pry somewhere in your code and getting the interpreter to eventually evaluate it. E.g.,

class MyObj
  def do_stuff
    binding.pry
    ....
   end
end

And then in your ruby console you can start your pry session via:

Ā»  MyObj.new.do_stuff

From: /home/cyle/development/ruby/stuff.rb @ line 3 MyObj#do_stuff:

     2: def do_stuff
     3:   binding.pry
 =>  4:   ...

Ā»  self
=> #<MyObj:0x0000000fbb7d30> { ... }

It essentially grants you break point capabilities for your ruby projects, extremely useful for debugging object state during runtime. I wish I could speak more intelligently on how it can be used, but so far Iā€™ve only ever really needed to use it for basic things and itā€™s done a hell of a job at it. I know thereā€™s other gems like pry-byebug that extend this even further if you really want to pull your code a part and see what happens at each level of the call stack.

If you just want a step up from the poor manā€™s style of debugging (good old puts) another thing I find myself doing is simply calling raise somewhere in my application during development. Since Iā€™m using the better_errors gem with binding_of_caller I get a spiffy error panel with a REPL prompt that allows me to see the value of local variables. It isnā€™t elegant, but it works, and it sure beats the hell out of calling puts everywhere. :smiley:

1 Like

@Cyle_Hunter you are right, I love PRY too. And I also use Better Errors. They are both quite beautiful. I just realized recently that with Better Errors, you actually get a REPL for every step of the stack, where you can access local variables and settings within each step. I always just used to use the top of the stack.

That being said, I am now working with much more complicated data relationships and behavior, and I have to be holding everything in my head - to get all the relationships right. It has gotten to a place where on one operation there are 5+ halts, because I have binding.pry everywhere. That has gotten to be too much.

So I would love if I could basically dump the mental model I have in my mind, into some tool, that will help me visualize everything I am trying to build.

I am not sure if this exists, but just figured I would ask :smile:

In fact, I have something in mind that I want built, but I figure I would need hardcore JS skills to be able to build the web version. So if anyone that is particularly strong on the front-end with JS would like to build something like that, then let me know. It would be OSS obviously.

Try drawing it out with pencil and paper.

Iā€™m serious.

@benorenstein You are dead right. This works. This is actually what I do in the most complex of cases.

But what I want is an automated version of this. Meaning, the weakness with Pen and Paper is that values arenā€™t as easy to change and cascade.

So if I could get the benefits of pen and paper and of machine, thatā€™s the perfect combination of what I am looking for actually.

Thereā€™s also a bunch of free UML diagramming tools available on all the major operating systems that should have some of the bells and whistles that youā€™re looking for.

If you would just like to get a visual of your existing controllers and models you can use RailRaody to dump some diagrams. Hell, if youā€™re using guard with guard-rake you can even run the rake task for generating the diagrams in an automated fashion if you really wanted.

1 Like

I joined a really complicated project a few months ago. Rails erd really helped me to understand the logic and I still use the diagrams as reference especially when debugging or working with the console.