Learning about instance variables and don''t understand why the instance variable "name" in the code example below is not visible where as "title" is. Could someone explain this to me and why I hadto use the "self.get_name()" method (which I don''t understand since I pulled it off the net) to get the value out of name? Thanks ------------------------------- class InstanceVariables @name = "Steve" def initialize() @title = "Software Eng" end def self.get_name @name end def get_name self.class.get_name # Why can''t I just return @name? end def get_title @title end def show_vars puts "name: #@name, title: #@title" # name is not set, at least that I can tell. end end ------------------------ obj1 = InstanceVariables.new puts obj1.get_name() puts obj1.get_title() obj1.show_vars() ------------------------ Steve Software Eng name: , title: Software Eng --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 18 Feb 2009, at 18:42, Steve wrote:> > Learning about instance variables and don''t understand why the > instance variable "name" in the code example below is not visible > where as "title" is. Could someone explain this to me and why I hadto > use the "self.get_name()" method (which I don''t understand since I > pulled it off the net) to get the value out of name? >Classes are objects in ruby and can have instance variables, so when you set @name = "Steve" where you have put it you''re create an instance variable on the class rather than an instance variable on a particular instance of the class. Fred> Thanks > ------------------------------- > class InstanceVariables > @name = "Steve" > > def initialize() > @title = "Software Eng" > end > > def self.get_name > @name > end > > def get_name > self.class.get_name # Why can''t I just return @name? > end > > def get_title > @title > end > > def show_vars > puts "name: #@name, title: #@title" # name is not set, at least > that I can tell. > end > end > ------------------------ > obj1 = InstanceVariables.new > puts obj1.get_name() > puts obj1.get_title() > obj1.show_vars() > ------------------------ > Steve > Software Eng > name: , title: Software Eng > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So what is the distinction between a class instance variable and a class variable (ie @@name="Steve")? On Feb 18, 1:56 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 Feb 2009, at 18:42, Steve wrote: > > > > > Learning about instance variables and don''t understand why the > > instance variable "name" in the code example below is not visible > > where as "title" is. Could someone explain this to me and why I hadto > > use the "self.get_name()" method (which I don''t understand since I > > pulled it off the net) to get the value out of name? > > Classes are objects in ruby and can have instance variables, so when > you set @name = "Steve" where you have put it you''re create an > instance variable on the class rather than an instance variable on a > particular instance of the class. > > Fred > > > Thanks > > ------------------------------- > > class InstanceVariables > > @name = "Steve" > > > def initialize() > > @title = "Software Eng" > > end > > > def self.get_name > > @name > > end > > > def get_name > > self.class.get_name # Why can''t I just return @name? > > end > > > def get_title > > @title > > end > > > def show_vars > > puts "name: #@name, title: #@title" # name is not set, at least > > that I can tell. > > end > > end > > ------------------------ > > obj1 = InstanceVariables.new > > puts obj1.get_name() > > puts obj1.get_title() > > obj1.show_vars() > > ------------------------ > > Steve > > Software Eng > > name: , title: Software Eng--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 19, 12:50 am, Steve <myde...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So what is the distinction between a class instance variable and a > class variable (ie @@name="Steve")? >mostly the odd scoping stuff which means that @@foo in an instance method and @@foo in a class context refer to the same object. Fred> On Feb 18, 1:56 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 18 Feb 2009, at 18:42, Steve wrote: > > > > Learning about instance variables and don''t understand why the > > > instance variable "name" in the code example below is not visible > > > where as "title" is. Could someone explain this to me and why I hadto > > > use the "self.get_name()" method (which I don''t understand since I > > > pulled it off the net) to get the value out of name? > > > Classes are objects in ruby and can have instance variables, so when > > you set @name = "Steve" where you have put it you''re create an > > instance variable on the class rather than an instance variable on a > > particular instance of the class. > > > Fred > > > > Thanks > > > ------------------------------- > > > class InstanceVariables > > > @name = "Steve" > > > > def initialize() > > > @title = "Software Eng" > > > end > > > > def self.get_name > > > @name > > > end > > > > def get_name > > > self.class.get_name # Why can''t I just return @name? > > > end > > > > def get_title > > > @title > > > end > > > > def show_vars > > > puts "name: #@name, title: #@title" # name is not set, at least > > > that I can tell. > > > end > > > end > > > ------------------------ > > > obj1 = InstanceVariables.new > > > puts obj1.get_name() > > > puts obj1.get_title() > > > obj1.show_vars() > > > ------------------------ > > > Steve > > > Software Eng > > > name: , title: Software Eng--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---