Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$? and $CHILD_STATUS are not equivalent #1747

Closed
maasha opened this issue Mar 31, 2015 · 9 comments
Closed

$? and $CHILD_STATUS are not equivalent #1747

maasha opened this issue Mar 31, 2015 · 9 comments

Comments

@maasha
Copy link

maasha commented Mar 31, 2015

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?
@mattjmcnaughton
Copy link
Contributor

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?

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 31, 2015

It's even in the message:

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

@bbatsov bbatsov closed this as completed Mar 31, 2015
@arturopie
Copy link

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

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 14, 2015

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.

@matkoniecz
Copy link

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

@jonas054
Copy link
Collaborator

jonas054 commented Jun 6, 2016

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

@jaredbeck
Copy link
Contributor

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 $?.

@jonas054
Copy link
Collaborator

jonas054 commented Nov 5, 2016

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.

@jonas054 jonas054 reopened this Nov 5, 2016
@ivanovaleksey
Copy link
Contributor

By the way the message on current master branch is

C: Prefer $CHILD_STATUS from the stdlib 'English' module over $?.

DavidS added a commit to DavidS/jekyll-deploy that referenced this issue Aug 28, 2020
DavidS added a commit to DavidS/jekyll-deploy that referenced this issue Aug 28, 2020
DavidS added a commit to DavidS/jekyll-deploy that referenced this issue Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants