Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:49:23+00:00 2026-05-18T03:49:23+00:00

Several of my partials can be rendered in two modes. If full_display is false

  • 0

Several of my partials can be rendered in two “modes”. If full_display is false, I don’t render several fields. To make things easy I wanted to make one of the “modes” default – if full_display is not defined, treat it as false. I came up with this code:

(((not defined?(full_display).nil?) && full_display) || false)

Quite a lot to move around. It would be nice to put it inside a helper or something, but since Ruby has only lexical scope I can’t think of any good way to do it.

Bad ideas that I’ve already tried:

  • on the top of the partial do <% display = long code that is above %> and use display through the code, but creating local variables in a view looks bad and has to be copied into every partial using full_display.
  • wrap it inside a string, put it into a helper and use eval(display_helper) in view, but obviously this creates security concerns.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:49:23+00:00Added an answer on May 18, 2026 at 3:49 am

    That’s quite a convoluted way of saying something as simple as:

    defined?(full_display) && full_display
    

    In Ruby there are two values that are non-true, nil and false, all others evaluate as true, which includes 0, empty string, among others, that would otherwise evaluate as false in Perl, PHP, and C. Testing with .nil? is usually reserved for those rare cases where you want to differentiate between false and undefined, and this is sometimes the case with boolean fields where a missing value is different from a false value.

    In any case, in the view space it is a lot easier to assign defaults using the or-equals operator ||= like this:

    <% full_display ||= false %>
    

    That is equivalent to full_display = full_display || false. This does two things. First, it defines the full_display variable even if it was not previously created, and secondly it ensures that the it will contain a value that is at least false, never undefined.

    You will see the ||= default pattern a lot in Ruby code as it’s an easy way to assign something in the case where it will be nil otherwise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Several months (maybe even a year or two) ago, I saw an asp .net
I have a form where users create Person records. Each Person can have several
I have several view Partials (like paginator partial) which I want them to be
I'm outputting a collection in haml #- if @fields.count>0 .sfields #= render :partial=>sfields/field, :collection=>@fields,
I've achieved several partial successes, but can't cross the last hurdle. Symptom: When in
If a file is committed several times with various changes, how can I fetch
I'm used to working with Zend Framework where I can do a partial render.
I have a folder on my Mule ESB server which several sources can point
I have several Telerik's DropDownList controls which are rendered inside jQuery dialog It is
What i have: Several references to the same partial but with different locals like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.