From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "mame (Yusuke Endoh)" <noreply@ruby-lang.org>
Subject: [ruby-core:120567] [Ruby master Feature#20987] Add dbg - minimal debugging helper
Date: Thu, 09 Jan 2025 11:26:57 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-111391.20250109112657.55446@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20987.20241226234002.55446@ruby-lang.org>
Issue #20987 has been updated by mame (Yusuke Endoh).
This proposal was discussed in the dev meeting.
@matz said that, while adding a new method or changing the default behavior of `Kernel#p` is unacceptable, some kind of switch could be considered to make `Kernel#p` output the filename.
Several ideas for the switch were raised.
* environment variable: `P=1`
* using the existing $DEBUG mode: `ruby -d`
* (ab)using a warning category: `ruby -W:p`
* `puby` (another command; joke)
Among these, the use of `-d` was favored.
However, the $DEBUG mode is impractical in modern times. Every time an exception occurs, an error message is output (even if it is rescue'd). Many libraries (including rubygems) output error messages under the $DEBUG mode.
@ko1 will do some research and see if it is practical by stopping the error message every time an exception occurs. He said he will make a separate proposal.
One more thing, the current proposal seems to output only one parent directory and file name, "dir/file.rb", but it is arguable whether this is enough or not. It would be annoying to always use absolute paths, but it might be better to use paths relative to the current directory.
----------------------------------------
Feature #20987: Add dbg - minimal debugging helper
https://bugs.ruby-lang.org/issues/20987#change-111391
* Author: pawurb (Pawel Urbanek)
* Status: Open
----------------------------------------
Hi. It's my first time contributing here, so I'm sorry in advance if I've mixed something up.
I’m author of https://github.com/pawurb/dbg-rb gem. `dbg` method is inspired by Rust where it's built-in into std-lib (https://doc.rust-lang.org/std/macro.dbg.html). AFAIK in Ruby there's no simple mechanism to puts debug values together with caller info without using external dependencies. What’s more frustrating is that while `p nil` outputs `nil` to the std, `puts nil` prints a blank line, sometimes making debugging sessions confusing.
I would like to propose adding a minimal `dbg` helper method to stdlib:
```
dbg("Hello world", [1, 2, 3])
# => [dir/file.rb:12] "Hello world"
# => [dir/file.rb:12] [1, 2, 3]
```
`dbg` will produce verbose output together with informative file name and LOC info. I think that such built-in feature would be useful for many Ruby devs.
My gem uses external dependencies, but I've came up with this barebones implementation:
```
def dbg(*msgs)
loc = caller_locations.first.to_s
matching_loc = loc.match(/.+(rb)\:\d+\:(in)\s/)
src = if matching_loc.nil?
loc
else
matching_loc[0][0..-5]
end
file, line = src.split(":")
file = file.split("/").last(2).join("/")
src = "[#{file}:#{line}]"
msgs.each do |msg|
puts "#{src} #{msg.inspect}"
end
nil
end
```
---Files--------------------------------
Screenshot 2024-12-27 at 00.00.23.png (81.5 KB)
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
prev parent reply other threads:[~2025-01-09 11:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-26 23:40 [ruby-core:120428] " pawurb (Pawel Urbanek) via ruby-core
2024-12-27 11:50 ` [ruby-core:120430] " pawurb (Pawel Urbanek) via ruby-core
2024-12-27 19:59 ` [ruby-core:120433] " mame (Yusuke Endoh) via ruby-core
2024-12-27 21:15 ` [ruby-core:120434] " pawurb (Pawel Urbanek) via ruby-core
2025-01-09 11:26 ` mame (Yusuke Endoh) via ruby-core [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-111391.20250109112657.55446@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).