Not quite because the summary could change to nil, but it brings up a good point. There's often a better way in Rails, but &&= is native Ruby so it's especially useful in non-Rails applications.
Python doesn't need it; the savings from not double-evaluating things aren't there. Additionally, Python lore is to expand this idiom to an if statement, for explicitness, especially in polymorphic situations where you actually want to check for "<expr> is None" rather than "bool(<expr>)". Both of these examples are Railsy; equivalent Djangoy statements in form validation would have to deal with None:
if data["incoming"] is None:
data["incoming"] = "Default value"
...
if data["incoming"]:
data["incoming"] = data["incoming"].strip()
Because of Python's string immutability, there's no point to dodging the assignment; you're still going to incur new string creation anyway.
||= turns out to be rather useful as a general assign once operator, but i'm having a hard time justifying use of &&=. That and I find myself after learning Erlang to try to assign only once when possible to make debugging easier.
While ||= isn't strictly needed, it makes assigning to class/module variables in a single line easier in general. I'm not sure I agree with the Python "always expand to an if statement" methodology though, it just seems like a bit of unneeded yak shaving. (note: i don't use rails so your post may be specific to web frameworks and I'm off my rocker, if so disregard. >.<)
10 comments
[ 1.8 ms ] story [ 24.3 ms ] thread&&= is less well known than ||=. RTFA for examples.
@value ||= "foo" is more efficient than
As it actually evaluates as follows: This is especially good when working with hashes, as the left hand side is only evaluated once, not twice.For the example cited, you could also use ActiveRecord::Dirty
For example this:
versus a more conventional/idiomatic if: ||= turns out to be rather useful as a general assign once operator, but i'm having a hard time justifying use of &&=. That and I find myself after learning Erlang to try to assign only once when possible to make debugging easier.While ||= isn't strictly needed, it makes assigning to class/module variables in a single line easier in general. I'm not sure I agree with the Python "always expand to an if statement" methodology though, it just seems like a bit of unneeded yak shaving. (note: i don't use rails so your post may be specific to web frameworks and I'm off my rocker, if so disregard. >.<)