Prev: Fill a table in ruby
Next: combined ranges...
From: Tom Stone on 2 Apr 2010 15:42 Hi,I'm new here. I have a question: What exactly does the '@' symbol mean when used for the scope of a variable? For instance: @screen=[640,400]. I haven't found anything explicitly explaining exactly what this does. Thanks! -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 2 Apr 2010 15:52 On Fri, Apr 2, 2010 at 9:42 PM, Tom Stone <s1ay3r44(a)gmail.com> wrote: > Hi,I'm new here. > > I have a question: > > What exactly does the '@' symbol mean when used for the scope of a > variable? > For instance: @screen=[640,400]. > > I haven't found anything explicitly explaining exactly what this does. It means it's an instance variable of the object that is currently self in that context. For example: class A def initialize @value = 3 end end a = A.new The object referenced by a has an instance variable called @value with value 3. Jesus.
From: Tom Stone on 2 Apr 2010 15:56 Oh! I see! So it's a variable that is automtically given to each instance of that class right? -- Posted via http://www.ruby-forum.com/.
From: Jonathan Nielsen on 2 Apr 2010 15:58 > So it's a variable that is automtically given to each instance of that > class right? Not quite... it's a variable in the instance that it is declared. So, if you put it in the 'initialize' method as seen in that example, it will assign that variable for that instance when it is created. -Jonathan Nielsen
From: Tom Stone on 2 Apr 2010 16:15
Jonathan Nielsen wrote: >> So it's a variable that is automtically given to each instance of that >> class right? > > Not quite... it's a variable in the instance that it is declared. So, > if you put it in the 'initialize' method as seen in that example, it > will assign that variable for that instance when it is created. > > -Jonathan Nielsen So... it's a variable that can only be used in that instance and nowhere else? such as: Class Test def initialize @x=5 end def testing puts"#{@x}" end end problem=Test.new problem.testing >5 #however puts"#{@x}" >error -- Posted via http://www.ruby-forum.com/. |