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.

For the first one you could use if session(:var).nil?
Piotr. Thanks for the message. That’s definitely more elegant than my approach and I’ll give it a try!