8000 there are bugs in if-statements using `and`. by mephistobooks · Pull Request #967 · eventmachine/eventmachine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

there are bugs in if-statements using and. #967

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mephistobooks
Copy link

the operators of and, or, and not in if-statements are too buggy.
(ex. p true and false =>true)
we should not use them. actually there are bugs in eventmachine.rb so i correct them.

please read this: "Ruby Style Guide", no and/or.

@sodabrew
Copy link
Member

Is there an example of a situation where one of these resulted in a wrong output?

@mephistobooks
Copy link
Author
mephistobooks commented Apr 18, 2022

For example,

if defined?(EventMachine.library_type) and EventMachine.library_type == :pure_ruby

this is always true if EventMachine.library_type is just defined. i.e., it becomes true also when EventMachine.library_type != :pure_ruby... I would think this is not expected behavior.

@sodabrew
Copy link
Member

That would be a surprising result, but I'm not able to reproduce that in a simple irb session. What am I missing?

@mephistobooks
Copy link
Author

Oh, really...? Simply we can check it as follows:

x = :pure_ruby
p true and x == :pure_ruby   # this becomes true. yeah, it's natural.
# =>true
p true and x != :pure_ruby    # but this becomes also true... 😭
# =>true

Both we get are true. So we must say that the codes using and/or in if-conditions have semantically (or potentially) bug. It is VERY dangerous...

@MSP-Greg
Copy link
Contributor

@mephistobooks

First, I would agree that using and/or can have unintended consequences.

Your example is incorrect, mostly because p/puts are method calls, and if is not. It also shows how problems can occur when not using parentheses with method calls. Try:

x = :pure_ruby
if true and x == :pure_ruby
  p 'true'
else
  p 'false'
end

if true and x != :pure_ruby
  p 'true'
else
  p 'false'
end

@sodabrew
Copy link
Member

Thanks @MSP-Greg, exactly the explanation for this situation of using puts and getting a different result.

@mephistobooks I would be glad to take the PR for modern Ruby style if you would clarify the commit message to say that this is a style change not a bug-fix.

8000
@mephistobooks mephistobooks force-pushed the patch-op-in-if-statements branch from a309b11 to a3fb762 Compare April 20, 2022 02:39
@mephistobooks
Copy link
Author
mephistobooks commented Apr 20, 2022

@sodabrew I'm sorry, @MSP-Greg is correct. I'm wrong (I misunderstood it with assignment expression).

I did git squashed two commits as one commit, and rename commit message as ba99be5.

@mephistobooks mephistobooks force-pushed the patch-op-in-if-statements branch from a3fb762 to ba99be5 Compare April 20, 2022 03:32
@nevans
Copy link
Contributor
nevans commented May 24, 2022

FWIW, although I agree with every change in this PR, I resent the ruby style guide total ban. And the justification confuses more than it clarifies (basically: it's FUD). So I'm just registering that complaint. 😜 Avdi Grimm has a good explanation (transcript available at the unlinkable "Video Script" tab). But, Avdi and I both learned Perl before ruby.

Fortunately, the rubocop default is Style/AndOr: {EnforcedStyle: conditionals}, which (mostly) agrees with Avdi and me, and not with the style guide. 😉

Contrary to what the style guide says: ruby's precedence makes more sense than Perl's (IMO), once you understand the design rationale. 🙂 It makes perfect sense for boolean algebra symbols to follow the standard math precedence rules (disjunctive normal form, or the sum of products) and the English control flow words to follow the most common linguistic approach (simple left-to-right).

If you want restrictions on the more arcane corners of ruby's grammar: using parens on most method calls is more helpful (and maybe less controversial?). The way "command_call" interact with "stmt", "expr", "arg", and "primary" isn't always obvious until you've spent time playing with the grammar. And to be fair, ruby's core syntax rdoc really should do a better job of explaining all of this.

Anyway, everyone's time might've been better served if, instead of writing this comment, I had submitted PRs to the ruby style guide and ruby's doc/syntax/{precedence,control_expressions}.rdoc!

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

Successfully merging this pull request may close these issues.

4 participants
0