2

I'm wondering why assignment with the ternary operator reacts strangely:

a = "foo"­
=> "foo"
a = nil ? nil : a
=> "foo"
a
=> "foo"

but:

a = nil ? nil : a
=> "foo"
a = "bar"­ ? "bar"­ : a
=> "bar"
a
=> "bar"

and:

if a = nil
  puts "should be nil"
end
=> nil

won't puts the string because a = nil will return nil thus false, although the assignment was successful.

Is that all behaving like intended?

4
  • 1
    "the assignment was successful" seems to be the key misconception here. The result of an assignment expression is (and is supposed to be) the assigned value, not whether the assignment "succeeded". (In fact a variable assignment can't really fail.) Commented Jul 2, 2013 at 0:04
  • in this example i'm not confusing assignment with the comparison, ok if i assign i get back the value assign confusion cleared about that thx to you all but why is a still "foo" in my first example with the Ternary Operator although i assigned nil ? Commented Jul 2, 2013 at 0:10
  • 2
    Your first example should be read as a = (nil ? nil : a). Since nil is "falsey", you assign the previous value of a to a, which is "foo". Commented Jul 2, 2013 at 0:12
  • so (a = nil) ? nil : a would return my expected value, great thx ;) Commented Jul 2, 2013 at 0:18

5 Answers 5

7
if a = nil

This isn't returning false, it's returning what was assigned, which in this case was nil. nil is 'falsy' so that's why it does not go into the puts

As to why:

a = "foo"­
=> "foo"
a = nil ? nil : a
=> "foo"
a
=> "foo"

It's because you are assigning a again. nil ? nil : a returns a so that's what gets assigned. So a = nil ? nil: a ends up being interpreted like a = a.

Sign up to request clarification or add additional context in comments.

Comments

5

I believe this:

if a = nil

should be:

if a == nil

A single = means assignment, and a = nil is assigning nil to a and evaluating to nil as a result, which is false. That's why the execution doesn't enter the puts part, whereas == means equality testing.

Other than that, what do you find strange in the code? It's normal behavior after all.

Comments

2

Assuming you intended a = nil to be an assignment and knew it was not a comparison, it doesn't return whether or not it is successful.

It returns the value assigned, namely nil.

Comments

1

The example you provided is behaving as intended.

Perhaps you're confusing the assignment operator = with the comparison operator ==. Try the last snipped of code like this:

if a == nil
  puts "should be nil"
end
=> nil
> a
=> "bar"

Comments

1
a = "foo"­
# => "foo"
a = nil ? nil : a
# => "foo"
a
# => "foo"

To explain the above I would start with this - "Every thing in Ruby is an object except block;all object in Ruby is having a truth value,except nil and false." Thus in your above part of code a = nil ? nil : a expression will be evaluated as a = a. That's why 'foo'has been returned as a value ofa`.

a = "bar"­ ? "bar"­ : a
# => "bar"
a
# => "bar"

As I just said except nil and false all object has truthy value,"bar" is always true. so the expression a = "bar"­ ? "bar"­ : a will be evaluated as a = "bar".

if a = nil
  puts "should be nil"
end
=> nil

The same explanation goes to the if a = nil part as I explained above. So due to false control doesn't get into the if clause body,and returned nil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.