Take the 2-minute tour ×

Is this not evaluating the if statement?

<%= current_user.profile.name || current_user.email if current_user.profile.name.blank? %>

Debug on current_user.profile.name shows it is an empty string, but it's not printing email. Changing to a ternary operator like this:

<%= current_user.profile.name.blank? ? current_user.email : current_user.profile.name %>

works, but I'd like to understand why the first way doesn't work.

share|improve this question
a || b if c is parsed as (a || b) if c In this case, c is only true if the profile name is blank .. so, now, to the next part: suppose a is "" (blank, must be given the if) and b is a fallback value, then consider: "" || "hello world!" <- What is that result? (Hint: What does ruby consider truthy values? What would the result of nil || "hello world!" be?) –  user166390 Jan 18 '13 at 6:36
so in ruby an empty string is not falsey... why would this still not work if i was checking for an empty string... while playing with this trying to figure it out, at one point I had <%= current_user.profile.name || current_user.email if current_user.profile.name == '' %> and it still wouldn't print the email –  Joel Grannas Jan 18 '13 at 6:44
Exactly, "" is not false-y :) In that case, what would a and b be? What would a || b be? Would it ever be useful? (Note that str == "" implies str.blank? and str.empty?) –  user166390 Jan 18 '13 at 6:46
nil || nil, so not useful? –  Joel Grannas Jan 18 '13 at 6:47
Exactly. There is no false-y string in Ruby so x || y, where x is a string, always evaluates to x. –  user166390 Jan 18 '13 at 6:53

4 Answers 4

In Ruby, only nil and false count as falsy. An empty string is not falsy, so it satisfies the condition, and the || and after is not evaluated.

On the other hand, blank? returns true for an empty string. That is the difference between the two examples.

share|improve this answer

As others have already pointed out, an empty string is true-ish in Ruby, that explains why you need that extra blank?. That said, note that active_support is eager to ease the pain, Object#presence:

<%= current_user.profile.name.presence || current_user.email %>
share|improve this answer

debug on current_user.profile.name is an empty string means the following condition

if current_user.profile.name.blank? == false

this implies


current_user.profile.name || current_user.email

will not be executed ,hence the result

share|improve this answer
I've tried .empty? and also == "" they all seem to return the same results –  Joel Grannas Jan 18 '13 at 6:40

The line below:

<%= current_user.profile.name || current_user.email if current_user.profile.name.blank? %>

Interpreter checks Part 1:

<%=  current_user.profile.name || 

Part 2:

current_user.email if current_user.profile.name.blank? %>

Then OR statements gets dilemma , and gives an error. Second Parameter (current_user.email if current_user.profile.name.blank?) is available or not...

Its as per my understanding... Hope you get it.

share|improve this answer
wrong you are. right part (if condition) is evaluated first, and only if if condition evaluates to true => then left part evaluates –  zed_0xff Jan 18 '13 at 8:07

Your Answer


