Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if not nil and not empty in Rails shortcut?

I have a show page for my Users and each attribute should only be visible on that page, if it is not nil and not an empty string. Below I have my controller and it is quite annoying having to write the same line of code @user.city != nil && @user.city != "" for every variable. I am not too familiar with creating my own methods, but can I somehow create a shortcut to do something like this: @city = check_attr(@user.city)? Or is there a better way to shorten this procedure?

users_controller.rb

def show    @city = @user.city != nil && @user.city != ""   @state = @user.state != nil && @user.state != ""   @bio = @user.bio != nil && @user.bio != ""   @contact = @user.contact != nil && @user.contact != ""   @twitter = @user.twitter != nil && @user.twitter != ""   @mail = @user.mail != nil && @user.mail != "" end 
like image 271
user3048402 Avatar asked Apr 09 '14 17:04

user3048402


People also ask

How do you check if something is not nil in Ruby?

That's the easy part. In Ruby, you can check if an object is nil, just by calling the nil? on the object... even if the object is nil. That's quite logical if you think about it :) Side note : in Ruby, by convention, every method that ends with a question mark is designed to return a boolean (true or false).

Is nil empty Ruby?

Well, nil is a special Ruby object used to represent an “empty” or “default” value. It's also a “falsy” value, meaning that it behaves like false when used in a conditional statement.

What is the difference between nil and empty in Ruby?

# nil? can be used on any Ruby object. It returns true only if the object is nil. # empty? can be used on some Ruby objects including Arrays, Hashes and Strings. It returns true only if the object's length is zero.

Is nil an object in Ruby?

However, in terms of how it's implemented, nil is fundamentally different than in other languages. In Ruby, nil is—you've guessed it—an object. It's the single instance of the NilClass class. Since nil in Ruby is just an object like virtually anything else, this means that handling it is not a special case.


1 Answers

There's a method that does this for you:

def show   @city = @user.city.present? end 

The present? method tests for not-nil plus has content. Empty strings, strings consisting of spaces or tabs, are considered not present.

Since this pattern is so common there's even a shortcut in ActiveRecord:

def show   @city = @user.city? end 

This is roughly equivalent.

As a note, testing vs nil is almost always redundant. There are only two logically false values in Ruby: nil and false. Unless it's possible for a variable to be literal false, this would be sufficient:

if (variable)   # ... end 

This is preferable to the usual if (!variable.nil?) or if (variable != nil) stuff that shows up occasionally. Ruby tends to wards a more reductionist type of expression.

One reason you'd want to compare vs. nil is if you have a tri-state variable that can be true, false or nil and you need to distinguish between the last two states.

like image 187
tadman Avatar answered Oct 12 '22 19:10

tadman