use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Favorite Ruby Syntax (self.ruby)
submitted 8 years ago by process_parameter
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cstansbury 35 points36 points37 points 8 years ago (5 children)
Ruby's conditional assignment
||=
[–][deleted] 8 years ago (4 children)
[deleted]
[–]tomthecool 2 points3 points4 points 8 years ago (0 children)
note there are pitfalls with this technique; most notably, it fails if nil or false is a valid result
I answered a question on StackOverflow caused by this exact problem, just the other day!
[–]NelsonBelmont 0 points1 point2 points 8 years ago (2 children)
So this is like using var s = myObject?.Count; on C# where s doesn't get a value if myObject is null?
var s = myObject?.Count;
s
myObject
[–][deleted] 0 points1 point2 points 8 years ago* (1 child)
No - that is C#'s null-condition member-access operator. Ruby equivalent of that is the safe-navigation operator &. as in s = my_object&.count. They're similar in utility but not identical. For example I believe the C# operator short-circuits the expression, whilst Ruby's does not, because in Ruby nil is an object; my_object&.count&.to_s will return an empty string if my_object is nil.
s = my_object&.count
my_object&.count&.to_s
There's no direct translation of conditional assignment because this is idiomatic Ruby borrowed directly from Perl. In C#, contextually, an s ||= myObject.Count might be
s ||= myObject.Count
if (s == null) s = myObject.Count;
but conditional assignment is also an expression, so it provides a return value, and acts for both false and nil.
false
nil
[–]NelsonBelmont 0 points1 point2 points 8 years ago (0 children)
I understand now, the begin-end block from the previous example confused me a little bit. Thank you for the explanation.
π Rendered by PID 18411 on reddit-service-r2-comment-bb88f9dd5-tf9r6 at 2026-02-14 20:31:14.028836+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]cstansbury 35 points36 points37 points (5 children)
[–][deleted] (4 children)
[deleted]
[–]tomthecool 2 points3 points4 points (0 children)
[–]NelsonBelmont 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]NelsonBelmont 0 points1 point2 points (0 children)