Issue #12881 has been updated by Tomoyuki Chikanaga.

Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: REQUIRED

----------------------------------------
Bug #12881: Double-bang (!!) raises warning on string literal
https://bugs.ruby-lang.org/issues/12881#change-61117

* Author: RRRoy BBBean
* Status: Closed
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: REQUIRED
----------------------------------------
I use !!x or !!(x) to force x to true or false.
I just noticed this trick raises a warning with string literals.
It would be nice if this warning were not raised, since it is sometimes convenient to force some object to true or false.
I first saw this in irb, but it also happens when running a script from the command line.
Caveat: Although the output of my sample program suggests that double-quoted strings avoid this warning, it actually happens with !!"hi" as well as !!'hi'

CODE: bb.rb
  1 p [ '!!false', !!false ]
  2 p [ '!!(false)', !!(false) ]
  3 p [ '!!nil', !!nil ]
  4 p [ '!!(nil)', !!(nil) ]
  5 p [ '!!true', !!true ]
  6 p [ '!!(true)', !!(true) ]
  7 p [ '!![]', !![] ]
  8 p [ '!!([])', !!([]) ]
  9 p [ '!!{}', !!{} ]
 10 p [ '!!Object.new', !!Object.new ]
 11 p [ '!!(Object.new)', !!(Object.new) ]
 12 p [ '!!1', !!1 ]
 13 p [ '!!(1)', !!(1) ]
 14 p [ '!!0.1', !!0.1 ]
 15 p [ '!!(0.1)', !!(0.1) ]
 16 p [ '!!:a', !!:a ]
 17 p [ '!!(:a)', !!(:a) ]
 18 p [ '!!({})', !!({}) ]
 19 p [ '!!String.new', !!String.new ]
 20 p [ '!!(String.new)', !!(String.new) ]
 21 p [ '!!""', !!"" ]
 22 p [ '!!("")', !!("") ]
 23 p [ '!!"b"', !!"b" ]
 24 p [ '!!("b")', !!("b") ]
 25 p [ "!!''", !!'' ]
 26 p [ "!!('')", !!('') ]
 27 p [ "!!'b'", !!'b' ]
 28 p [ "!!('b')", !!('b') ]

OUTPUT
bb.rb:25: warning: string literal in condition
bb.rb:26: warning: string literal in condition
bb.rb:27: warning: string literal in condition
bb.rb:28: warning: string literal in condition
["!!false", false]
["!!(false)", false]
["!!nil", false]
["!!(nil)", false]
["!!true", true]
["!!(true)", true]
["!![]", true]
["!!([])", true]
["!!{}", true]
["!!Object.new", true]
["!!(Object.new)", true]
["!!1", true]
["!!(1)", true]
["!!0.1", true]
["!!(0.1)", true]
["!!:a", true]
["!!(:a)", true]
["!!({})", true]
["!!String.new", true]
["!!(String.new)", true]
["!!\"\"", true]
["!!(\"\")", true]
["!!\"b\"", true]
["!!(\"b\")", true]
["!!''", true]
["!!('')", true]
["!!'b'", true]
["!!('b')", true]

ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
I compiled it myself from sources, running on Fedora24 amd64 workstation




---Files--------------------------------
bb.rb (685 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>