Marek Sapota
Marek Sapota

Reputation: 20591

Making dynamic scope helpers in Rails

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:

Upvotes: 1

Views: 663

Answers (1)

tadman
tadman

Reputation: 211590

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.

Upvotes: 2

Related Questions