Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

Ruby Unless Keyword

Apr 20

Ruby has a very elegant syntax when it comes to several things. A good example is how I was checking for the existence of a session variable in my Rails app:

if(session[:my_variable) == nil)
// Do something here
end

I don't really have a problem with the syntax above, but was suprised to find the following syntax that does the exact same thing:

unless session[:my_variable]
// Do something here
end

Ah yes, much better. I find it much more readible and human-like, less machine-like.

2 comments

  1. For the first one you could use if session(:var).nil?

  2. Piotr. Thanks for the message. That’s definitely more elegant than my approach and I’ll give it a try!

Leave a Reply