I''m having trouble understanding Ruby''s concept of scope. Could someone explain the difference between the following 2 programs. They both return the same output "aaaaaaaa", but I''m just not sure about self in #1. Is self.day and self.title referring to and setting class variables or are they instance variables? Thanks in advance. # 1. class Session attr_accessor :day, :title def initialize() self.day = "aaaaaaaa" self.title = "bbbbb" end end s=Session.new puts s.day ################### # 2. class Session attr_accessor :day, :title def initialize() @day = "aaaaaaaa" @title = "bbbbb" end end s=Session.new puts s.day ################### --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
bkocik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-28 05:16 UTC
Re: Learning ruby question
In both cases you''re setting instance variables. Class variables are designated with ''@@'', and instance variables are usually designated with ''@''. But as you''ve seen, you can also set instance variables up using the ''self'' reference, which is just a reference to the object receiving the message. Without the ''self'' in #1, day and title would wind up being local variables that would fall out of scope as soon as initialize() ended. If you''re familiar with Java, you can think of ''self'' as ''this''. As a thought experiment, what do you think the following code will do? class Session attr_accessor :day, :title def initialize() self.day = ''aaa'' self.title = ''bbb'' end def say_day puts @day # Accessing day like any other instance variable, with ''@''. Think it''ll work? end end s = Session.new s.say_day On Apr 27, 11:52 pm, jim <jjones35...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> I''m having trouble understanding Ruby''s concept of scope. Could > someone explain the difference between the following 2 programs. > They both return the same output "aaaaaaaa", but I''m just not sure > about self in #1. Is self.day and self.title referring to and setting > class variables or are they instance variables? Thanks in advance. > > # 1. > class Session > attr_accessor :day, :title > > def initialize() > self.day = "aaaaaaaa" > self.title = "bbbbb" > end > end > > s=Session.new > puts s.day > ################### > > # 2. > class Session > attr_accessor :day, :title > > def initialize() > @day = "aaaaaaaa" > @title = "bbbbb" > end > end > > s=Session.new > puts s.day > ###################--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Great thanks. So in my 2 examples above - the code is functionally the same. You can SET instance variables within a class using either @var = foo OR self.var = foo As for your example, based on what I''ve said above - it appears you can also GET instance variables using self.var or @var - so the output should be: aaa Is that correct? Thanks again. On Apr 28, 1:16 am, "bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In both cases you''re setting instance variables. Class variables are > designated with ''@@'', and instance variables are usually designated > with ''@''. But as you''ve seen, you can also set instance variables up > using the ''self'' reference, which is just a reference to the object > receiving the message. Without the ''self'' in #1, day and title would > wind up being local variables that would fall out of scope as soon as > initialize() ended. > > If you''re familiar with Java, you can think of ''self'' as ''this''. > > As a thought experiment, what do you think the following code will do? > > class Session > attr_accessor :day, :title > def initialize() > self.day = ''aaa'' > self.title = ''bbb'' > end > def say_day > puts @day # Accessing day like any other instance variable, with > ''@''. Think it''ll work? > end > end > s = Session.new > s.say_day > > On Apr 27, 11:52 pm,jim<jjones35...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m having trouble understandingRuby''sconcept of scope. Could > > someone explain the difference between the following 2 programs. > > They both return the same output "aaaaaaaa", but I''m just not sure > > about self in #1. Is self.day and self.title referring to and setting > > class variables or are they instance variables? Thanks in advance. > > > # 1. > > class Session > > attr_accessor :day, :title > > > def initialize() > > self.day = "aaaaaaaa" > > self.title = "bbbbb" > > end > > end > > > s=Session.new > > puts s.day > > ################### > > > # 2. > > class Session > > attr_accessor :day, :title > > > def initialize() > > @day = "aaaaaaaa" > > @title = "bbbbb" > > end > > end > > > s=Session.new > > puts s.day > > ###################- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 4/28/07, jim <jjones3535a-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > Great thanks. So in my 2 examples above - the code is functionally > the same. You can SET instance variables within a class using either > @var = foo OR self.var = foo > > As for your example, based on what I''ve said above - it appears you > can also GET instance variables using self.var or @var - so the output > should be: aaa > > Is that correct?That is correct. But don''t take my word for it - run it and see what it does. :) The best way to learn how all this stuff works is to play with it. If you think something might work a certain way, try it and see. If it doesn''t do what you expected, find out why. Interactive Ruby (irb) is your friend. -- Bill Kocik --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---