extend string format to nil safe
I'd like to suggest a new modifier for the string format
(aka the printf-style format string)
'%s' % nil => '' '%d' % nil => Exception: TypeError: can't convert nil into Integer
'%&s' % nil => 'nil' '%&d' % nil => 'nil'
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
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
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.
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)