Is the ruby operator ||= intelligent?
Posted
by brad
on Stack Overflow
See other posts from Stack Overflow
or by brad
Published on 2010-06-07T13:22:29Z
Indexed on
2010/06/07
15:42 UTC
Read the original article
Hit count: 234
I have a question regarding the ||= statement in ruby and this is of particular interest to me as I'm using it to write to memcache. What I'm wondering is, does ||= check the receiver first to see if it's set before calling that setter, or is it literally an alias to x = x || y
This wouldn't really matter in the case of a normal variable but using something like:
CACHE[:some_key] ||= "Some String"
could possibly do a memcache write which is more expensive than a simple variable set. I couldn't find anything about ||= in the ruby api oddly enough so I haven't been able to answer this myself.
Of course I know that:
CACHE[:some_key] = "Some String" if CACHE[:some_key].nil?
would achieve this, I'm just looking for the most terse syntax.
© Stack Overflow or respective owner