R
Ruby Freak
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] ||
plural.to_s.singularize
54 @path_segment = options.delete
as) || @plural
55
56 @options = options
57
58 arrange_actions
59 add_default_actions
60 set_prefixes
61 end
(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
haircuts)
Question (1)
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
end if
But what is the value of @singular if there is no :singular symbol?
nil?
What I think is going on is a lot more like an IF statement than a OR
statement.
Question (2)
in the line: @plural ||= entities
if entities is (false?, nil? something that fails "OR") what happens
to @plural?
Once again, the ||= seems more like an "equals If" statement rather
than an "equals Or" statement.
Question (3)
@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.
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] ||
plural.to_s.singularize
54 @path_segment = options.delete
55
56 @options = options
57
58 arrange_actions
59 add_default_actions
60 set_prefixes
61 end
(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
haircuts)
Question (1)
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
end if
But what is the value of @singular if there is no :singular symbol?
nil?
What I think is going on is a lot more like an IF statement than a OR
statement.
Question (2)
in the line: @plural ||= entities
if entities is (false?, nil? something that fails "OR") what happens
to @plural?
Once again, the ||= seems more like an "equals If" statement rather
than an "equals Or" statement.
Question (3)
@path_segment = options.delete
So @path_segment is assigned either the return from the delete
operation
correct? Where in a common resourceful routes mapping is there an :as?
Thanks in advance for any help.