Project

General

Profile

Actions

Bug #12881

closed

Double-bang (!!) raises warning on string literal

Added by rrroybbbean (RRRoy BBBean) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[ruby-core:77801]

Description

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) bb.rb the code i use to produce the example rrroybbbean (RRRoy BBBean), 10/28/2016 05:33 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0