5 ms·
I had to write some ruby at work recently and wasted a few hours on a bug caused by the following footgun: irb(main):028:0> value = false or true => tr
by nickysielicki 3y ago
I had to write some ruby at work recently and wasted a few hours on a bug caused by the following footgun:
irb(main):028:0> value = false or true
=> true
irb(main):029:0> value
=> false
That really makes me want to write it off entirely. It’s hard to think of a situation where this is the right behavior.
- jameskilton 3y agoYou want to use "||" like most languages, not "or". They have different precedence rules.
- sinkwool 3y ago`and`, `or`, `not` have much lower precedence than `&&`, `||` and `!` . I would just avoid `and` and `or` in general. For some more explanation see https://graceful.dev/courses/the-freebies/modules/ruby-language/topic/episode-125-and-or/ https://graceful.dev/courses/the-freebies/modules/ruby-langu...
- djur 3y agoRubocop forbids the use of `and` and `or` in a value expression for this reason, and I believe several style guides used with Rubocop ban them entirely. I like them but have come to accept that they're more trouble than they're worth (in a shared codebase with lots of contributors, at least).
- mberning 3y agoIf that is enough to turn you off it’s probably for the best.
- matthewowen 3y agoIn python empty string is false. In ruby 0 is true. And yes, ruby has both or and || but they mean different things. I’m not saying that it isnt valid to criticize these things: first time use of a language matters, people work in multiple languages, it is good to be intuitive. But in practice, these things aren’t problems for people who work regularly in these languages, so I personally find them to be quite low salience. I recognize that this is sort of similar to the claims the hypothetical user of php makes in “php is a fractal of bad design”, but personally I find think the issues are of a different nature.
- jes5199 3y agothat’s not the bug here. they got caught by precedence rules
- matthewowen 3y agoI suppose “|| and or” mean different things was a little too loose or broad a way of expressing that, but I agree what the big is and you see how it fits in with my overall point I’m sure
- sinkwool 3y agoI believe what GP is saying is: coming from python, which uses `and` and `not` and `or` you expect those to be the binary operators in ruby as well. You are then surprised when they behave unexpectedly. Who is then to blame for your frustration? Is ruby a bad language for having both `and` and `&&`, or are you at fault for not doing enough research? (I don't see why someone coming from python would look any further after trying `and` and seeing that it seems to work just fine). I think this is an unfortunate situation, but one that doesn't really happen to someone that does ruby day-to-day; so it shouldn't really factor in as an argument to write ruby off. But then is being surprised by `["0","1","10"].map(parseInt) // => [0, NaN, 2]` in JavaScript a sign of bad language design or does it just mean that you don't have enough experience with the language yet?
- avg_dev 3y agoyes, that does seem pretty annoying. however, irb(main):001:0> value = false or true => true irb(main):002:0> value => false irb(main):003:0> value = false || true => true irb(main):004:0> value => true irb(main):005:0> I believe if you use the `||` operator instead of `or`, then things just work out fine. I agree it is really annoying. But I am pretty sure if you use a tool like RuboCop https://github.com/rubocop/rubocop https://github.com/rubocop/rubocop (a static code analysis tool) then it will catch bugs like this. Note that I am not recommending Ruby. But in my experience if I want to work with a language and it has a community style guide and a linter that enforces it, it will save me some heartache. Just editing to add this reference: https://rubystyle.guide/#and-or-flow https://rubystyle.guide/#and-or-flow