# Difference between false and :false

**URL:** <https://www.ruby-forum.com/t/difference-between-false-and-false/139169>\
**Category:** Rails\
**Created:** [May 31, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/difference-between-false-and-false/139169 "2008-05-31T22:28:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 31, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/difference-between-false-and-false/139169/1 "2008-05-31T22:28:15Z")

</div>

I was noticing that some code (acts\_as\_authenticated in particular)  
uses the symbol :false. What is the purpose of this symbol? Why not  
just use false? How can I write and if blog that will not be  
triggered if the variable is :false?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 31, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/difference-between-false-and-false/139169/2 "2008-05-31T23:18:04Z")

</div>

On Sat, May 31, 2008 at 11:05 AM, removed\_email\_address@domain.invalid \<  
removed\_email\_address@domain.invalid\> wrote:

> I was noticing that some code (acts\_as\_authenticated in particular)  
> uses the symbol :false. What is the purpose of this symbol? Why not  
> just use false? How can I write and if blog that will not be  
> triggered if the variable is :false?

:false is just a symbol that has no special meaning. It has nothing in  
common with false. I assume you’re referring to this line in the  
current\_user method of acts\_as\_authenticated/restful\_authentication:

@current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
login\_from\_cookie || :false)

The reason it uses :false instead of false is because the shorthand  
assignment (||=) would re-evaluate all the login methods every time  
#current\_user was called if the user was not logged in.

I think the use of :false is nasty and it should be rewritten to use  
something like:

unless @current\_user == false  
@current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
login\_from\_cookie || false)  
end

Hope that clears it up.

Brandon

* * *

Sessions by Collective Idea: Ruby on Rails training in a vacation  
setting  
[http://sessions.collectiveidea.com](http://sessions.collectiveidea.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2008, 12:47am UTC](https://www.ruby-forum.com/t/difference-between-false-and-false/139169/3 "2008-06-01T00:47:05Z")

</div>

On Saturday 31 May 2008, Brandon K. wrote:

> I think the use of :false is nasty and it should be rewritten to use  
> something like:
> 
> unless @current\_user == false  
> @current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
> login\_from\_cookie || false) end

Yes, it is downright evil. However, I disagree on how to clean up this  
mess. This is really a classic situation for using the Null Object  
Pattern in order to avoid special cases for non-existent (nil) or  
otherwise absent (:false) objects. A Null Object is simply a real  
object of the appropriate (duck) type

def current\_user  
@current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
login\_from\_cookie || guest\_user)  
end

def guest\_user  
GuestUser.new  
end

def logged\_in?  
current\_user.logged\_in?  
end

class GuestUser  
def logged\_in?  
false  
end  
def active?  
true  
end  
# whatever other methods you need  
…  
end

With this, you can always be certain that you get a sensible (not nil)  
result from current\_user, can call methods on it and don’t have to care  
whether it is a real, logged-in user or a guest.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2008, 1:28am UTC](https://www.ruby-forum.com/t/difference-between-false-and-false/139169/4 "2008-06-01T01:28:40Z")

</div>

Michael S. wrote:

> def current\_user  
> @current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
> login\_from\_cookie || guest\_user)  
> end

> class GuestUser  
> def logged\_in?  
> false  
> …  
> With this, you can always be certain that you get a sensible (not nil)  
> result from current\_user, can call methods on it and don’t have to care  
> whether it is a real, logged-in user or a guest.

+1.

I used exactly that on every Rails project I ever did, and it never  
gets old. There’s just no way to describe the freedom of writing…

def index  
@favorite\_posts = Post.find\_by\_tags(someone.tags)  
end

…and not give a second thought to whether ‘someone’ is a guest, a  
logged-in  
user, or a premium user. And you write ‘someone…’ in sooo many places!

–  
Phlip
