Hacker News new | ask | show | jobs
by 333c 609 days ago
Yeah, it would be confusing. In Ruby, this would leave spam unmodified.
1 comments

That's even more confusing. There's an =, I expect an assignment to happen. Maybe we just leave this one alone.
In Ruby, the `if [condition]` modifier at the end of a line is used for more cases than just assignment. For example, `return if s.blank?` and `raise "invalid" if input.length > 100`. In Ruby, this pattern makes it clear that the statement is only executed if the condition is met.

I'm not advocating for this feature to be added to Python, just explaining why it's not confusing in Ruby.

Though I used it for 2 years of it as my primary language at work, I never quite got used to Ruby's quirky idioms like this. It just reads badly to me, in terms of quickly understanding code flow, to have statements that start with "raise" or "return" which might not raise or return. Similar to the up-thread comment about assignment.
Perhaps using a parenthesized assignment expression with the walrus operator would be unambiguous:

(spam := eggs) if bar

I think that seems reasonable? It would act just the same as it already does with an explicit `else None`, if I'm not mistaken. I don't find it beautiful though.