by vidarh · 2y ago · 0 comments
Overally I just don't get this difficulty people have debugging Ruby, and to me it feels like there's just some fundamental disconnect where people insist on working with Ruby as if it is statically typed, and then get confused when they find it hard, instead of embracing the dynamism and message passing nature of it.
In Ruby I usually work with a "dev mode" in my code that makes it trivial to drop to a repl (there's also ruby-debug which allows dropping to a repl "anywhere" but since you then run in a trap context it limits what you can do) and optionally reload code, which means I can poke and prod at every aspect of the runtime system whenever and wherever there's a problem, and can often write a fix, load it into the running system and retry the basic block that triggered the error to see if it fixed it.
My editor is written in Ruby, and if something breaks, I get thrown into a pry prompt and can fix it that way and continue. Or I can exit, because since every method call is a message, DrB lets me trivially forward it over a socket, and so the actual text buffers sits safely in a different process (and if that throws an exception, I can debug that too, via the same mechanism, from the client, while it is still running)
It might well be Elixir would be the better choice for how you think - I'm not trying to convince you Ruby works better for you. But I think a lot of people would be a lot happier with their Ruby experience if they actually explored what a more dynamic system makes possible. If you don't take advantage, there's really no point - in that case you're paying a performance cost for no gain and would be better off sticking to a static language.
(Regarding refinements, in the years since I've found exactly one use for it - enabling torturously invasive monkey patching that'd be entirely unacceptable system-wide, and keep that firmly confined to inside a DSL - but that use is very much worthwhile)
For example, yesterday I ran into an issue with bundler where a gem was failing to install and needed to step through the process. I set a binding.b in the gem’s extconf.rb, but when that breakpoint got hit the program just stopped without allowing input, I.e. there was no debug prompt presented.
Is bundler running inside a trap context similar to what you mention above?
Bundler shouldn't be running inside a trap context, but you might be running into a situation where standard input/output from the actual process triggering your breakpoint has been redirected. In that case, ruby-debug[1] is a good option, as you attach to it from outside[2]. Basically, run "rdbg --open yourscript.rb" and then use rdbg -A from another terminal.
You use Pry remotely too[3] if you prefer.
[1] https://github.com/ruby/debug
[2] https://github.com/ruby/debug?tab=readme-ov-file#remote-debu...
* Add a "reload!" method that will reload the code.
* Add a repl.rb file that loads the main app and throws you in a suitably configured irb or pry console, with a "reload!" method that'll reload the files you're running in (remember, `require`'s are only loaded once, so this can get tricky for projects that require's lots of things - you can pick a gem to handle this for you, but frankly I tend to find that I usually work on a small set of code at a time, and so I usually prefer to adjust what I want to reload semi-manually).
* Be set up to do remote debugging if your code takes over stdin/stdout, but try to ensure the majority of your code doesn't, so you can play with it from the debugger before you relinquish control at the top level. Or add a way to have your app temporarily surrender control. E.g. my editor will give up control of the terminal to pry when I press ctrl+x, ctrl+p, and once on the Pry prompt I can access the entire editor state via $editor.
* Add some gems to make formatting data from your objects nicer, like e.g. by current side-project brings in sequel_pretty_print (because I use Sequel rather than ActiveRecord), awesome_print and hirb-unicode.
* While it's nice to be able to run throwaway lines in the REPL, I tend to copy things into methods very quickly, so I build up an array of tests and helpers *based directly on what I've done in the REPL.
Overall most of the "magic" will come from learning the full set of capabilities pry (and irb is finally starting to catch up) and ruby-debug provides you.
My goal tends to be that I shouldn't need to exit the REPL very often while debugging, so I can e.g. keep old objects around and compare state before/after a change, and not have to worry about the complexity.
That's because `method_missing` isn't the subject of the post. Are you also going to point out that the blog post doesn't mention Eigenclasses or Jim Weirch?
> and in general method_missing is rarely used much in modern Ruby meta programming, for good reason.
You're just extremely wrong.
> I'm not trying to convince you Ruby works better for you
I've been doing Ruby for 15 years, I don't think you could.
I am not, but given the extremely combative attitude of your comment while you've not provided any actual arguments, it's clear you're not interested in actually discussing the subject, but rather getting out aggression, so take care - I won't waste my time on any further conversations with you.
Which editor is that?
Sometimes it breaks. That's fine - I can fix it trivially. But because it's my own, I also put up with things in it I wouldn't otherwise, because I know how to work around it.
I also get a level of pleasure from gradually taking more and more control of my own tools that I'd never want to give up.
'snark'.tr('nr', 'lc')The "problem" mainly that it works well enough for me, and I've had little incentive to make it work at all for anyone else.... Though now that it runs in my own terminal, I'm thinking of unifying some of the buffer handling for both the editor and terminal and splitting that out as a gem, and once I've done that and rewritten some of the (currently rather inefficient) rendering and syntax highlighting code (the syntax highlighting uses Rouge, which is fine, but I have a mountain of things on top that I'm slowly cleaning up so they're reusable and somewhat performant) it'll start to approach usable for others...