I saw this excerpt in the agile rails programming book:
module StoreHelper
def page_title
@page_title || "Pragmatic Store"
end
end
Can I change || to =?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The longer answer is that you’ve stumbled on a programming style which is popular because it’s very concise, yet still easy to understand. It’s a shortcut way to write:
So that one line with logical or’s removes the need for an if/then. It does this because of the way that || is evaluated: from left-to-right, stopping at the first item that’s not false. The example you found also leaves out the return keyword, because it’s not explicitly necessary here.