Ruby: Random Observations
I thought it’d be interesting to write down some of my observations after working with Ruby and Rails for a couple more weeks so here are some more things I’ve come across and others that I’ve got confused with…
The :: operator
(apparently also known as the leading double colon operator)
I came across this while looking at some of the rails_warden code to try to understand how that gem opens the ActionController::Base class to add helper methods to it.
The code reads as follows:
Rails.configuration.after_initialize do
class ::ActionController::Base
include RailsWarden::Mixins::HelperMethods
include RailsWarden::Mixins::ControllerOnlyMethods
end
module ::ApplicationHelper
include RailsWarden::Mixins::HelperMethods
end
end
The '::' operator is used on line 78 and it means that Ruby will look in the global scope for the constant which follows the operator - in this case ActionController::Base. Marcos Ricardo explains this in more detail on his blog.
In this case I’m not entirely sure why the operator is necessary since there doesn’t seem to be another constant defined with the same name in the local scope.
The !! operator
This is another operator that I came across while reading the Warden code.
def halted?
!!@halted
end
Sidu explained that this operator ensures that true or false will be returned rather than a truish/falish value.
For example:
ruby-1.8.7-p299 > !!nil
=> false
ruby-1.8.7-p299 > !!1
=> true
Single '=' in if statements
A mistake I’ve made a few times now is using '=' in if statements instead of '==' which means that the code tends to fail in a somewhat confusing way.
I’m not sure if this is because I’ve been playing around with Clojure a bit recently and in Clojure you use '=' for comparison or if I do this anyway and usually get saved by the compiler.
Either way it’s very frustrating!
Open classes
I’m used to being able to see exactly what is defined on a class in one place but in Ruby it’s possible to open a class from anywhere and add to it or change the existing behaviour.
I still don’t know all of the hooks that Rails provides for opening classes so it’s still a big magical for me at the moment.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.