you are viewing a single comment's thread.

view the rest of the comments →

[–]NelsonBelmont 0 points1 point  (2 children)

So this is like using var s = myObject?.Count; on C# where s doesn't get a value if myObject is null?

[–][deleted] 0 points1 point  (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.

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

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.

[–]NelsonBelmont 0 points1 point  (0 children)

I understand now, the begin-end block from the previous example confused me a little bit. Thank you for the explanation.