From: Farrel Lifson Date: 2006-07-27T17:18:03+09:00 Subject: Re: ||= in an expression On 27/07/06, rejoc wrote: > Hello, > > I new to Ruby and curious about this language I do use (yeet). > > I was browsing through some libraries/modules and I found a line looking > like > > a[:i] ||= "a string" > > I could not find this syntax in any manual online (and googling "||=" is > not of much help :-( ) > > Can anyone tell me what it means > > TiA > > In Ruby nil || anObject will return anObject and anotherObject || anObject will return anotherObject (assuming it's value isn't nil) The expression a[:i] ||= "a string" is equivalent to a[:i] = a[:i] || "a string" (sort of like 'num+= 1' is the same as 'num = num + 1')If a[:i] is not nil then it will remain unchanged. If it is nil then it will be set to "a string". Farrel