Hi all, I''m working my way through the Rails book and have come to a section with some code I don''t quite understand, and that''s not adequately explained in the text. Particularly, I''m having problems with file 54, the User model in iteration F2 of the Depot application. I''ve attached the file with my questions in comments for your convenience. My two basic questions are this: First, what does the expression "password || '''' " mean when passed as a parameter to a method? I imagine it means "if password is TRUE, send that as the parameter, otherwise send a blank string," But I''ve not come across this particular twist of Ruby before and can''t find a reference to it in Pickax II. It looks to me like it /should/ mean "Send the value of the boolean expression "password OR '''' ", which would always TRUE since a blank string is a TRUE object (I believe, but correct me if I''m wrong.) My second question is, why the use of "self" to access attributes here? As I understand it (and correct me if I''m wrong again,) the database columns used here (user, password, hashed_password) are instance variables and/or instance methods. If this is the case, isn''t the use of "self" redundant? Couldn''t line 16, for example, read simply "@hashed_password = User.hash_password(@password)"? Keep in mind that I''m going through the book chronologically, so if the answer is explained later in the book, please tell me. Thanks! -Matt Torok _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
This is a guess, but I think the OR operator returns the first TRUE argument, allowing you to use it in this way. Jacob On 10/1/05, Matt Torok <magicmat@gmail.com> wrote:> Hi all, > > I'm working my way through the Rails book and have come to a section > with some code I don't quite understand, and that's not adequately > explained in the text. Particularly, I'm having problems with file 54, > the User model in iteration F2 of the Depot application. I've attached > the file with my questions in comments for your convenience. > > My two basic questions are this: First, what does the expression > "password || '' " mean when passed as a parameter to a method? I > imagine it means "if password is TRUE, send that as the parameter, > otherwise send a blank string," But I've not come across this > particular twist of Ruby before and can't find a reference to it in > Pickax II. It looks to me like it /should/ mean "Send the value of the > boolean expression "password OR '' ", which would always TRUE since a > blank string is a TRUE object (I believe, but correct me if I'm > wrong.) > > My second question is, why the use of "self" to access attributes > here? As I understand it (and correct me if I'm wrong again,) the > database columns used here (user, password, hashed_password) are > instance variables and/or instance methods. If this is the case, isn't > the use of "self" redundant? Couldn't line 16, for example, read > simply "@hashed_password = User.hash_password(@password)"? > > Keep in mind that I'm going through the book chronologically, so if > the answer is explained later in the book, please tell me. > > Thanks! > -Matt Torok > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Oct 1, 2005, at 5:25 AM, Matt Torok wrote:> My two basic questions are this: First, what does the expression > "password || '''' " mean when passed as a parameter to a method? I > imagine it means "if password is TRUE, send that as the parameter, > otherwise send a blank string," But I''ve not come across this > particular twist of Ruby before and can''t find a reference to it in > Pickax II. It looks to me like it /should/ mean "Send the value of the > boolean expression "password OR '''' ", which would always TRUE since a > blank string is a TRUE object (I believe, but correct me if I''m > wrong.)You are 100% correct.> My second question is, why the use of "self" to access attributes > here? As I understand it (and correct me if I''m wrong again,) the > database columns used here (user, password, hashed_password) are > instance variables and/or instance methods. If this is the case, isn''t > the use of "self" redundant? Couldn''t line 16, for example, read > simply "@hashed_password = User.hash_password(@password)"?ActiveRecord requires use of accessor methods, likely because they''re not ''real'' classes. Since the attributes are created automatically by ActiveRecord via DB inspection, it''s likely that they''re stored in a hash or other data type internally by ActiveRecord. -- -- Tom Mornini
On 10/1/05, Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> wrote:> On Oct 1, 2005, at 5:25 AM, Matt Torok wrote: > > > My two basic questions are this: First, what does the expression > > "password || '''' " mean when passed as a parameter to a method? I > > imagine it means "if password is TRUE, send that as the parameter, > > otherwise send a blank string," But I''ve not come across this > > particular twist of Ruby before and can''t find a reference to it in > > Pickax II. It looks to me like it /should/ mean "Send the value of the > > boolean expression "password OR '''' ", which would always TRUE since a > > blank string is a TRUE object (I believe, but correct me if I''m > > wrong.) > > You are 100% correct. >About my first statement (that it sends whatever value is TRUE) or about my second (that it sends a boolean result of a boolean expression)? If the former, do you happen to know if and where the pickax describes this use?> > My second question is, why the use of "self" to access attributes > > here? As I understand it (and correct me if I''m wrong again,) the > > database columns used here (user, password, hashed_password) are > > instance variables and/or instance methods. If this is the case, isn''t > > the use of "self" redundant? Couldn''t line 16, for example, read > > simply "@hashed_password = User.hash_password(@password)"? > > ActiveRecord requires use of accessor methods, likely because they''re > not ''real'' classes. Since the attributes are created automatically by > ActiveRecord via DB inspection, it''s likely that they''re stored in a > hash or other data type internally by ActiveRecord. >Why does the model use ''@password'' in the after_create method, though? Is it because @password is a instance variable that we (rather than Rails) created via the attr_accessor method at the top? If so, why does the model then access @password as "self.password" in before_create and try_to_login?> -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Thank you for your help so far, it is helping me get a handle on things. I appreciate it. -- -Matt Torok
On 1 Oct 2005, at 13:25, Matt Torok wrote:> My two basic questions are this: First, what does the expression > "password || '''' " mean when passed as a parameter to a method?This is down to the way ruby handles truth. In a boolean expression, if an expression evaluates to an object, that is taken as true, and if it evaluates to nil, it is false. The result, though, is either the object or nil, not true or false. See: http://www.rubycentral.com/book/tut_expressions.html "Ruby has a simple definition of truth. Any value that is not nil or the constant false is true." Also see page 493 of the book (in the Ruby overview). So, you can read that expression as: ''the password if there is one, or the empty string otherwise''. The rest of that webpage (from the free online first edition of ''Programming Ruby'') is a very useful read, but I unreservedly recommend you buy an online/print copy of the second edition - it''s an excellent way to become a comfortable ruby coder.> My second question is, why the use of "self" to access attributes > here? As I understand it (and correct me if I''m wrong again,) the > database columns used here (user, password, hashed_password) are > instance variables and/or instance methods. If this is the case, isn''t > the use of "self" redundant? Couldn''t line 16, for example, read > simply "@hashed_password = User.hash_password(@password)"?Well, not exactly. hashed_password is a model method, not an attribute. That is, it is the method defined by activerecord to be used to set and get the value hashed_password from the database. Skip on a bit to the chapter on ActiveRecord basics for an explanation of this. I think you are right to say that the ''self'' is not mandatory here - but you can consider it as a reminder to the programmer/maintainer: ''I mean the automatically defined hashed_password method here, not a local variable called hashed_password''. I hope someone on the list (perhaps Mr Thomas? :) has a more lucid explanation of this for you - I hope this helps (and isn''t wrong!) in the meantime. Mike
Ahhh, that explains a lot. Thank you, you''ve saved me a lot of confusion. One question I still have, though, is with the @password instance variable. We create this manually with :attr_accessor since it''s not a column in the DB and, thus, AR doesn''t make it an attribute automatically. In the method after_create we access @password just like any instance variable, with the ''@''. But in the methods before_create and try_to_login the book uses "self.password", going through the accessor method of the User object. Why do this instead of just accessing the variable directly with ''@''? -Matt Torok> ------------Original Message------------ > From: Michael Houghton <mike-GDRLylaSUshUrdklU0bPTip2UmYkHbXO@public.gmane.org> > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org, magicmat-sq7/30T9sIgcWVvVuXF20w@public.gmane.org > Date: Sat, Oct-1-2005 7:01 PM > Subject: Re: [Rails] Question about something in the Rails book > > > On 1 Oct 2005, at 13:25, Matt Torok wrote: > > > My two basic questions are this: First, what does the expression > > "password || '''' " mean when passed as a parameter to a method? > > This is down to the way ruby handles truth. In a boolean expression, > if an expression evaluates to an object, that is taken as true, > and if it evaluates to nil, it is false. The result, though, is > either the object or nil, not true or false. > > See: http://www.rubycentral.com/book/tut_expressions.html > > "Ruby has a simple definition of truth. Any value that is > not nil or the constant false is true." > > Also see page 493 of the book (in the Ruby overview). > > So, you can read that expression as: > > ''the password if there is one, or the empty string otherwise''. > > The rest of that webpage (from the free online first edition of > ''Programming Ruby'') is a very useful read, but I unreservedly > recommend you buy an online/print copy of the second edition - it''s > an excellent way to become a comfortable ruby coder. > > > My second question is, why the use of "self" to access attributes > > here? As I understand it (and correct me if I''m wrong again,) the > > database columns used here (user, password, hashed_password) are > > instance variables and/or instance methods. If this is the case, > isn''t > > the use of "self" redundant? Couldn''t line 16, for example, read > > simply "@hashed_password = User.hash_password(@password)"? > > Well, not exactly. hashed_password is a model method, not an attribute. > > That is, it is the method defined by activerecord to be used to set > and get the value hashed_password from the database. Skip on a bit to > the chapter on ActiveRecord basics for an explanation of this. > > I think you are right to say that the ''self'' is not mandatory here - > but you can consider it as a reminder to the programmer/maintainer: > ''I mean the automatically defined hashed_password method here, not > a local variable called hashed_password''. > > I hope someone on the list (perhaps Mr Thomas? :) has a more lucid > explanation of this for you - I hope this helps (and isn''t wrong!) > in the meantime. > > Mike > >
On Oct 1, 2005, at 6:29 PM, Jacob Quinn Shenker wrote:> This is a guess, but I think the OR operator returns the first TRUE > argument, allowing you to use it in this way.As previously detailed in this thread, it''s the first (!nil && ! false) argument - using Ruby''s definition of "truth". --Steve
On 10/1/05, Matt Torok <magicmat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I''m working my way through the Rails book and have come to a section > with some code I don''t quite understand, and that''s not adequately > explained in the text. Particularly, I''m having problems with file 54, > the User model in iteration F2 of the Depot application. I''ve attached > the file with my questions in comments for your convenience. > > My two basic questions are this: First, what does the expression > "password || '''' " mean when passed as a parameter to a method? I > imagine it means "if password is TRUE, send that as the parameter, > otherwise send a blank string," But I''ve not come across this > particular twist of Ruby before and can''t find a reference to it in > Pickax II. It looks to me like it /should/ mean "Send the value of the > boolean expression "password OR '''' ", which would always TRUE since a > blank string is a TRUE object (I believe, but correct me if I''m > wrong.)this line in file 54 : hashed_password = hash_password(password || "") translates to hash the password unless it''s nil then hash an empty string || is just the standard OR notation. Pickaxe page 94 has some references to it.> > My second question is, why the use of "self" to access attributes > here? As I understand it (and correct me if I''m wrong again,) the > database columns used here (user, password, hashed_password) are > instance variables and/or instance methods. If this is the case, isn''t > the use of "self" redundant? Couldn''t line 16, for example, read > simply "@hashed_password = User.hash_password(@password)"? >I''m not so sure about this.......btw, password isn''t a column in the database.> Keep in mind that I''m going through the book chronologically, so if > the answer is explained later in the book, please tell me. >> Thanks! > -Matt Torok > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Marlon "And I Sleep, and I dream of the person I might have been, and I''ll be free again And I Speak, like someone who''s been to the highest peaks, and back again And I Swear, that my grass is greener than anyones, until I believe again"
OK, I think I have the hang of the first question, but what about the second? Why the use of self instead of ''@'' for password, and the use of ''self'' for the other attributes? On 10/1/05, Stephen Waits <steve-g8GSkY9QmIteoWH0uzbU5w@public.gmane.org> wrote:> > On Oct 1, 2005, at 6:29 PM, Jacob Quinn Shenker wrote: > > > This is a guess, but I think the OR operator returns the first TRUE > > argument, allowing you to use it in this way. > > As previously detailed in this thread, it''s the first (!nil && ! > false) argument - using Ruby''s definition of "truth". > > --Steve > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -Matt Torok