I am reading some of the ruby files in rails and I an seeing the ||=
method used a lot.
knowing ruby the way I do, I realize that she has lots of magical
surprises and I really want to get to know this girl!
Consider the initialize method of resources.rb
51 def initialize(entities, options)
52 @plural ||= entities
53 @singular ||= options[:singular] ||
54 @path_segment = options.delete(:as) || @plural
56 @options = options
(OK, so one iddy bitty part of this is a rails question, and dangit,
this is a ruby forum, but you guys are smarter and have better
After several hours of searching I find virtually nothing that fully
explains the line
@singular ||= options[:singular] || plural.to_s.singularize
I think know what is does, It assigns @singular a value for a
"singular named" controller if the options hash contains the
symbol :singular. I am looking more for a full description of how ||=
and it's friends like &&= actually work.
my current understanding shows me:
@singular = (options[:singular]) or (plural.to_s.singularize)
but the full logic seems to be more like:
if options contains a symbol named :singular then
@singular = plural.to_s.singularize
But what is the value of @singular if there is no :singular symbol?
What I think is going on is a lot more like an IF statement than a OR
in the line: @plural ||= entities
if entities is (false?, nil? something that fails "OR") what happens
Once again, the ||= seems more like an "equals If" statement rather
than an "equals Or" statement.
@path_segment = options.delete(:as) || @plural
So @path_segment is assigned either the return from the delete
operation (:as) ? or the plural name of the controller. Is that
correct? Where in a common resourceful routes mapping is there an :as?
Thanks in advance for any help.