Rubocop: $? and $CHILD_STATUS are not equivalent

Created on 31 Mar 2015  路  9Comments  路  Source: rubocop-hq/rubocop

Rubocop says:

rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:5:13: C: Prefer $CHILD_STATUS from the English library over $?.
fail unless $?.success?
            ^^

1 file inspected, 1 offense detected

However, $? is set for system call and $CHILD_STATUS is not:

./test.rb:6:in `<main>': undefined method `success?' for nil:NilClass (NoMethodError)

Code below:

#!/usr/bin/env ruby

system('pwd')

fail unless $?.success?
fail unless $CHILD_STATUS.success?

Most helpful comment

Instead of:

test.rb:5:13: C: Prefer $CHILD_STATUS from the English library over $?.

it might be more clear as:

test.rb:5:13: C: Prefer $CHILD_STATUS (must `require 'English'`) over $?.

All 9 comments

I think you need to require the "English" library. ruby-doc says the two should be interchangeable, if the library is required.

So for your test code to work it should be:

#!/usr/bin/env ruby

require 'English'

system('pwd')

fail unless $?.success?
fail unless $CHILD_STATUS.success?

It's even in the message:

test.rb:5:13: C: Prefer $CHILD_STATUS from the English library over $?.

I got the same error after using Rubocop auto-correct. Shouldn't the auto-correct add require "English"?

Normally you'd require it just once in your entire project, that's why auto-correct doesn't insert it. It's an easily fixable breakage, so I think most people would be fine with it.

Is there somewhere list of auto-correct rules that may break things, like this one?

@matkoniecz As far as I know, there is no such list.

Instead of:

test.rb:5:13: C: Prefer $CHILD_STATUS from the English library over $?.

it might be more clear as:

test.rb:5:13: C: Prefer $CHILD_STATUS (must `require 'English'`) over $?.

I'm re-opening the issue while we consider if we should change the message as per @jaredbeck's suggestion.

Even though it might be argued that requiring English in the offending file is unnecessary more often than not, I still think this message is an improvement and avoids some confusion, so my vote is yes.

By the way the message on current master branch is

C: Prefer $CHILD_STATUS from the stdlib 'English' module over $?.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tedPen picture tedPen  路  3Comments

bbatsov picture bbatsov  路  3Comments

NobodysNightmare picture NobodysNightmare  路  3Comments

benoittgt picture benoittgt  路  3Comments

david942j picture david942j  路  3Comments