Project

General

Profile

Actions

Feature #16779

closed

Add sprintf %q format option

Feature #16779: Add sprintf %q format option

Added by gearnode (Bryan Frimin) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:97833]

Description

It would be great to have format option for quoted string for sprintf (like golang sprintf),
it's particularly useful to format log message for example.

Currently we have to do this:

sprintf("%s", some_value.inspect.dump)

I propose to introduce the %q format option which allow to do this:

sprintf("%q", some_value)

Files

add-quoted-string-sprintf-format.patch (1.52 KB) add-quoted-string-sprintf-format.patch gearnode (Bryan Frimin), 04/11/2020 06:00 PM

Updated by gearnode (Bryan Frimin) over 5 years ago Actions #1

  • Description updated (diff)

Updated by gearnode (Bryan Frimin) over 5 years ago Actions #2

  • Description updated (diff)

Updated by Eregon (Benoit Daloze) over 5 years ago Actions #4 [ruby-core:97852]

Isn't %p enough (which uses #inspect)?

Updated by Eregon (Benoit Daloze) over 5 years ago Actions #5 [ruby-core:97853]

.inspect.dump double quotes, that doesn't seem readable/useful to me:

puts sprintf("%s", "abc".inspect.dump)
# "\"abc\""

Updated by gearnode (Bryan Frimin) over 5 years ago Actions #6 [ruby-core:97856]

I am not aware of %p format option, this option exactly did what i try to solve.
Thanks.

Someone can close this issue?

Updated by Eregon (Benoit Daloze) over 5 years ago Actions #7

  • Status changed from Open to Rejected

Updated by Eregon (Benoit Daloze) over 5 years ago Actions #8 [ruby-core:97857]

%p is part of the documentation of sprintf but maybe it's not so clear.
PR welcome to improve that documentation.

Actions

Also available in: PDF Atom