Project

General

Profile

Actions

Feature #15149

open

extend string format to nil safe

Added by foonlyboy (Eike Dierks) about 6 years ago. Updated about 6 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:89132]

Description

I'd like to suggest a new modifier for the string format
(aka the printf-style format string)

Examples

Current

'%s' % nil => ''
'%d' % nil => Exception: TypeError: can't convert nil into Integer

Suggestion

'%&s' % nil => 'nil'
'%&d' % nil => 'nil'

Explanation

I suggest to introduce a new modifier for the format strings.
(in the examples above I used the ampersand char: &)

That modifier should change the behaviour of string formatting in two ways:

  • accept nil as an argument for all conversion formats
  • display explicit 'nil' when nil is given as an argument

Rationale

This feature would be most helpful for log messages and printf style debugging

  • currently only the %s format accepts nil, but returns an empty string.

  • all other formats raise on nil

  • when displaying strings, nil args show up indiscernible from empty strings

  • when displaying anything else (like %d, %f etc) things just break

Compatibility

I believe that this new feature would be fully compatible with any existing code.
Up to now the ampersand was not allowed as a modifier and results in an Exception:

'%&d' % nil => ArgumentError: malformed format string - %&

As far as I know, the ampersand has not been used in other variations of printf, yet.

Design

I suggest to use the & (ampersand) because that would be in line with the ruby nil safe operator.

I opt for explicitly showing 'nil' for nil args (instead of the empty string)

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

Rather it feels the role of inspect, that is used by %p.
Regarding the compatibility, that new format string can't work with earlier versions.
That means libraries which want to support earlier versions can't use it.

Updated by foonlyboy (Eike Dierks) about 6 years ago

Hi nobu,
thanks for your reply, let's talk about this

I believe that %p is for pointers.
Actually %p already behaves like this:

'%p' % nil #=> "nil"

(I was not aware of that)

Please explain your complaints about compatibility.

This is a new feature
Obviously libraries which want to support earlier versions can not use that new feature.

But adding this new feature would not break any existing code.
(show me code that breaks on this)

I believe this is ruby style:
make things easy


We could go down the road of printf formats,
and a lot of programs failed on NULL in printf

Once we have this in ruby,
it might even spread out to C++

I still do opt for displaying nil as 'nil' (not the empty string)
I should look this up in the standards for the %p conversion

~eike

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

foonlyboy (Eike Dierks) wrote:

I believe that %p is for pointers.

It's true in C, but Ruby does not show bare pointers.
%p calls inspect instead.

Please explain your complaints about compatibility.

This is a new feature
Obviously libraries which want to support earlier versions can not use that new feature.

But adding this new feature would not break any existing code.
(show me code that breaks on this)

I meant that such library would need to stick the old behavior or
branch by the version or runtime checking.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0