From: Kyle Schmitt Date: 2008-05-02T01:31:16+09:00 Subject: Re: Please explain nuances of ||= You're close on some, right on some. @plural||=entries simply says, if @plural is nil or false (someone correct me if I'm a tad off), set it to entries, otherwise don't do anything. For @singular, close but.. you need the or @singular ||= ((options[:singular]) or (plural.to_s.singularize)) @path_segment is a little bit stranger, but for me it harkens back to c days. if options.delete(:as) returns any value that evalutes to true (anything but nil or false), @path_segment is set to that value. If options.delete(:as) fails or returns nil, @path_segment is set to @plural. Does that help? On Thu, May 1, 2008 at 11:20 AM, Ruby Freak wrote: > 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. > > >