Crystal: Ternary If Bug

Created on 3 Nov 2020  路  3Comments  路  Source: crystal-lang/crystal

I'm working with directories. And, i faced with this problem:

SETTINGS_DIR = "/etc/something_else" working_dir = Dir.exists? SETTINGS_DIR ? SETTINGS_DIR : "."

It's simple, if /etc/something_else exists, working_dir should became "SETTINGS_DIR". However, it sets to "false" value.
For fixing this, I'm using parenthesis for "Dir.exists?".

SETTINGS_DIR = "/etc/something_else" working_dir = Dir.exists?(SETTINGS_DIR) ? SETTINGS_DIR : "."

This is working fine.

This bug can also be reproduced in Ruby

crystal -v output:
`Crystal 0.35.1 (2020-06-19)

LLVM: 10.0.0
Default target: x86_64-unknown-linux-gnu`

ruby -v output:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

I'm on Fedora Linux (33) and I have installed crystal with this repo:
https://copr.fedorainfracloud.org/coprs/zawertun/crystal/

Thank you.

invalid

Most helpful comment

I would say this is working as intended and not a bug, ternary binds stronger (has a higher precedence) than method call. So the not parenthesized version is simply equivalent to working_dir = Dir.exists?(SETTINGS_DIR ? SETTINGS_DIR : ".")

All 3 comments

I would say this is working as intended and not a bug, ternary binds stronger (has a higher precedence) than method call. So the not parenthesized version is simply equivalent to working_dir = Dir.exists?(SETTINGS_DIR ? SETTINGS_DIR : ".")

Not a bug, but fully intended behaviour.

This bug can also be reproduced in Ruby

That's often a clear indicator that it's not an actual bug.

Oh, I suppose it as a bug. It seems I couldn't understand the logic. Sorry for the false reporting, thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Papierkorb picture Papierkorb  路  3Comments

oprypin picture oprypin  路  3Comments

relonger picture relonger  路  3Comments

TechMagister picture TechMagister  路  3Comments

ArthurZ picture ArthurZ  路  3Comments