I''m a newcomer to Ruby and I''ve read as much as I can but when running a rails application I still sometimes get unexpected results. 1. What is the difference between self.name and @name in a class method (particularly a model class)? 2. I have a getter for a property, tags_list, which formats a property whose value comes from the DB called tags. Why is that within the getter the value of tags, self.tags, and @tags is the same? 3. In my form I set the value of tags_list. In my before_create method only @tags_list is set while tags_list and self.tags_list is empty. Is this expected? This makes more sense to me than what I observe in #2. Thanks, Frank
Hi, I am going to attempt to answer what I asked with the hope this might help someone including me later if I forget again. :-) 1. The confusion for me w/ Ruby is that coming from a Java background I''m used to expecting to see brackets at the end of a function call but they are optional in Ruby. self.name can be thought of as self.name() where name() is an accessor method for the name member. It could be that self.name and @name are the same or they could be different if self.name() does more than just return @name. 2. This is explained above. 3. @tags_list is set by the form in this case because there is an attr_writer for this member. @tags_list is not a property that corresponds to a column in the database which is why tags_list and self.tags_list are empty. -Frank On 11/2/05, Frank Kim <mtmusko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m a newcomer to Ruby and I''ve read as much as I can but when running > a rails application I still sometimes get unexpected results. > > 1. What is the difference between self.name and @name in a class > method (particularly a model class)? > > 2. I have a getter for a property, tags_list, which formats a property > whose value comes from the DB called tags. Why is that within the > getter the value of tags, self.tags, and @tags is the same? > > 3. In my form I set the value of tags_list. In my before_create > method only @tags_list is set while tags_list and self.tags_list is > empty. Is this expected? This makes more sense to me than what I > observe in #2. > > Thanks, > Frank >-- Frank Kim http://betweengo.com
On Wed, 2005-11-02 at 07:21 -0500, Frank Kim wrote:> I''m a newcomer to Ruby and I''ve read as much as I can but when running > a rails application I still sometimes get unexpected results. > > 1. What is the difference between self.name and @name in a class > method (particularly a model class)?If you by class method really mean an instance method (which I think makes more sense in this context), @name in the instance variable that actually holds the data, and self.name is probably a getter method that returns @name. self.name(), being a method, could possibly also do other things than just return the data in @name. If you actually do mean a class method (that is, an instance method on the Class object/instance :), I apologise for assuming you don''t know :) @name would then be a class instance variable (rarely used), and self.name() a class method, as self refers to the class itself.> 2. I have a getter for a property, tags_list, which formats a property > whose value comes from the DB called tags. Why is that within the > getter the value of tags, self.tags, and @tags is the same?tags and self.tags are the same, as self is implied in the scope of your tags_list method. The tags method probably just returns @tags. It is often best to use the setter/getter methods ("tags") instead of manipulating the instance variable (@tags) directly.> 3. In my form I set the value of tags_list. In my before_create > method only @tags_list is set while tags_list and self.tags_list is > empty. Is this expected? This makes more sense to me than what I > observe in #2. > > Thanks, > Frank > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails