# FAQ: The Zen of Ruby - Conditional Assignment

This community-built FAQ covers the “Conditional Assignment” exercise from the lesson “The Zen of Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Conditional Assignment

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

why does this not work?

``````favorite_book = [nil , nil , "Green Eggs and Ham" , nil]
puts favorite_book

favorite_book.each do |book|
end
puts favorite_book

favorite_book.each do |book|
book ||= "Why's (Poignant) Guide to Ruby"
end
puts favorite_book

favorite_book.each do |book|
book = "Why's (Poignant) Guide to Ruby"
end
puts favorite_book

#returns
#[nil, nil, "Green Eggs and Ham", nil]
#[nil, nil, "Green Eggs and Ham", nil]
#[nil, nil, "Green Eggs and Ham", nil]
#[nil, nil, "Green Eggs and Ham", nil]
``````

Why does changing the first assignment to be a conditional assignment completely change the output?

Here’s my code:

``````favorite_book ||= nil  # All I changed was switching `=` to `||=`
puts favorite_book

puts favorite_book

favorite_book ||= "Why's (Poignant) Guide to Ruby"
puts favorite_book

favorite_book = "Why's (Poignant) Guide to Ruby"
puts favorite_book
``````

The output becomes this:

``````Why's (Poignant) Guide to Ruby
Why's (Poignant) Guide to Ruby
Why's (Poignant) Guide to Ruby
Why's (Poignant) Guide to Ruby
``````

I would expect the output to be identical to the version without the `||=` on the first line

The output is identical. But you need to start fresh. You probably ran the second code after you had already run the first version and the value of `favorite_book` persisted from that last run.
If you start with a new ruby script/file, you will get output identical to the first version.

I don’t get in which Situations i need this cause i also can write “=” to assign a variable ever and ever again?

2 Likes

Hi everyone!

What is the point of the conditional operator ||=? Other than the fact it exists, the lesson doesn’t explain much about it at all.

When would we use it? Why not just use =?

Thanks.

Since no one had answered this - in case anyone is checking, I’m assuming you wanted to replace each `nil` with a book, one by one. It might help to see this forum post: Conditional assignment operator?

`.each` by itself doesn’t actually change the array - see the Ruby documentation.

I have added a few ways you can do this below - not the only ways you can play with this code.

``````# Target the specific index that you already know the value of; you don't actually have to use conditional assignment here, though, because you already know that the index is `nil`.
favorite_book = [nil , nil , "Green Eggs and Ham", nil]
favorite_book[1] = "Why's (Poignant) Guide to Ruby"
favorite_book[3] = "Why's (Poignant) Guide to Ruby"
puts favorite_book
# => ["Cat's Cradle", "Why's (Poignant) Guide to Ruby", "Green Eggs and Ham", "Why's (Poignant) Guide to Ruby"]

# You can check if the index is equal to nil; it's not necessary to use conditional assignment; it makes more sense with an array to check if the index is `nil`.
favorite_book = [nil, nil ,"Green Eggs and Ham", nil]

favorite_book.each_with_index do |element, index|
if favorite_book[index].nil?
break # stops every nil value from being replaced with the same book
end
end

favorite_book.each_with_index do |element, index|
if favorite_book[index].nil?
favorite_book[index] = "Why's (Poignant) Guide to Ruby"
# no break allows all nil values to be replaced with the same book
end
end

puts favorite_book

# =>  ["Cat's Cradle", "Why's (Poignant) Guide to Ruby", "Green Eggs and Ham", "Why's (Poignant) Guide to Ruby"]

# You could also choose to replace every single nil value with the same book. Starting from the beginning:

favorite_book.each_with_index do |book, index|
favorite_book[index] = "Cat's Cradle" if book.nil?
end
puts favorite_book
``````

You can use a conditional operator if you don’t want to overwrite a variable that has already been assigned a value. This post about the conditional assignment operator that I mentioned in the post above has a lot of additional helpful information.

``````favorite_books = "Green Eggs and Ham"

favorite_books ||= "Why's (Poignant) Guide to Ruby"

puts favorite_books
# => Green Eggs and Ham

favorite_books = nil

favorite_books ||= "Why's (Poignant) Guide to Ruby"

puts favorite_books

# => Why's (Poignant) Guide to Ruby

# Additionally, you can't overwrite true

favorite_books = true

favorite_books ||= "Why's (Poignant) Guide to Ruby"

puts favorite_books

# => true

# You can overwrite false:

favorite_books = false

favorite_books ||= "Why's (Poignant) Guide to Ruby"

puts favorite_books

# => Why's (Poignant) Guide to Ruby
``````