Bug #15596

Kernel.warn without arguments should do the same as Kernel.warn(nil)

Added by kke (Kimmo Lehto) over 1 year ago. Updated about 1 year ago.

Target version:
ruby -v:
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]


Kernel.warn without arguments does not print an empty line to $stderr.

This is inconsistent with Kernel.puts and it feels weird, because it does not act like a regular Ruby method would (if it was written in Ruby instead of C, it would probably be defined like def warn(msg = nil) and calling it with or without nil as argument would make no difference)

Expected behavior:

irb(main):001:0> warn nil

=> nil
irb(main):002:0> warn

=> nil

Actual behavior:

irb(main):001:0> warn nil

=> nil
irb(main):002:0> warn
=> nil

Updated by shevegen (Robert A. Heiler) over 1 year ago

This is indeed a (to me) somewhat surprising behaviour; not that I guess
many have encountered it (and it probably is quite minor, which may
explain why that has not been reported before? Unless that was a more
recent change perhaps).

The current documentation is at:

If the behaviour is not a bug then I think it should be mentioned
in the documentation at the least briefly to explain why it behaves
that way.

If the behaviour is a bug, though, then I think I concur with Kimmo,
but I really do not know any specifics. It did seem strange though,
since I was not able to determine as to why warn nil would be
treated differently than warn without arguments.

Updated by nobu (Nobuyoshi Nakada) over 1 year ago

It is a natural behavior.

This method prints multiple arguments to STDERR, per line.
warn nil passes one argument and prints one line, whereas warn passes no argument and prints zero line.

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Status changed from Open to Rejected

As nobu stated, the current behavior is expected in regards to not printing a newline if no arguments are given, and the documentation is accurate for the current behavior. It states converts each of the messages to strings, appends a newline character to the string if the string does not end in a newline, and calls Warning.warn with the string.. To me, that implies if there are no messages/arguments, it does not do anything.

It is true that warn is different than puts in how a zero argument call is handled. warn it is similar to p in this regard. puts is specifically documented as adding a newline for no arguments (If called without arguments, outputs a single newline), and warn is not.

Also available in: Atom PDF