From: Rick DeNatale Date: 2007-10-27T22:59:25+09:00 Subject: Re: find all member in a class On 10/27/07, trebor777 wrote: > > > > THUFIR HAWAT wrote: > > > > On Fri, 26 Oct 2007 20:58:01 +0900, Rick DeNatale wrote: > > > > > >> class A > >> def i > >> @i ||= 42 > >> end > > > > > > Some rudimentary explanation please: > > > > the def is to define a method? > > it's a coincidence that the method name is "i" and the instance variable > > has the same name, "I"? > > > > > > the third line declares an instance variable, "i", what do the "||" do in > > that line, please? > > > > > > > > thanks, > > > > Thufir > > > > > > > > > > > > Yeap "def" is to define a method > It could be a coincidence... or not, depends on what you want to do with > this method... hum Here it's not a coincidence... this method return the > value of the instance variable i (it's a reader accessor) or if it's nil, > set it to 42 and return it. > > basically || means "if it's nil do that ", then = assigns the value 42 to @i Correct, it's an attribute reader with lazy initialization of the instance variable. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/