News for hackers. Stuff that matters.
Re:I Love Ruby by nickysielicki on Monday December 18, 2023 @05:58PM (#38689585)
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.
Re:I Love Ruby by jameskilton on Monday December 18, 2023 @06:12PM (#38689711)
You want to use "||" like most languages, not "or". They have different precedence rules.
Re:I Love Ruby by sinkwool on Monday December 18, 2023 @09:04PM (#38690940)
`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-langu...

Re:I Love Ruby by djur on Tuesday December 19, 2023 @01:38AM (#38692573)
Rubocop 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).
Re:I Love Ruby by mberning on Monday December 18, 2023 @09:33PM (#38691101)
If that is enough to turn you off it’s probably for the best.
Re:I Love Ruby by matthewowen on Monday December 18, 2023 @09:34PM (#38691106)
In 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.

Re:I Love Ruby by jes5199 on Monday December 18, 2023 @10:22PM (#38691408)
that’s not the bug here. they got caught by precedence rules
Re:I Love Ruby by matthewowen on Tuesday December 19, 2023 @08:19AM (#38695160)
I 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
Re:I Love Ruby by sinkwool on Tuesday December 19, 2023 @12:42PM (#38698755)
I 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?

Re:I Love Ruby by avg_dev on Tuesday December 19, 2023 @10:06AM (#38696426)
yes, 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 (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