3 ms·
> Ruby: a && b # `and` is available but is generally not recommended due to operator precedence They're low priority in both languages. I use and see others us
by Freaky 13y ago
> Ruby: a && b # `and` is available but is generally not recommended due to operator precedence
They're low priority in both languages. I use and see others using `and` and `or` all the time. In 15 years I can't say I've blown my foot off yet.
> Ruby: a = b ? c : d
If I'm really wanting a oneliner I'll tend to write
a = if b then c else d end
You can of course spread it across a few lines:
a = if b
c
else
d
end
More typically it's constructs like:
a ||= b
a = b if c
a = b unless c
> Ruby: "abc" =~ /[a-z]{3}/
/[a-z]{3}/.match("abc")
If you prefer.
- meowface 13y agoYou're right, but what I listed are generally considered the most idiomatic (the ternary one will be situational though). So when a beginner is trying to read someone else's code, they're likely to see those. But if they read some Python code that a seasoned veteran wrote, they're a lot less likely to see anything particularly confusing. I think Ruby does generally look more elegant, but if I was a beginner again I sure as hell would have a much easier time reading Python code compared to Ruby code.
- panzi 13y agoI find that "unless" adds some significant cognitive overhead for me, at least for more complex conditions. So I think this statement was a bad idea.