Scope of an @variable

Discussion in 'Ruby' started by Nathan Olberding, Mar 14, 2006.

  1. I've got a class. I want some methods of this class to be able to edit
    some data that's "global" within any given instance of this class. For
    example:

    class Person

    @name

    def changeName(newName)
    @name = newName
    end

    def sayName()
    puts "My name is " + @name
    end
    end

    It seems that @name reverts back once I leave the scope of any method
    that manipulates it.

    --
    Posted via http://www.ruby-forum.com/.
     
    Nathan Olberding, Mar 14, 2006
    #1
    1. Advertising

  2. Nathan Olberding

    Adam Shelly Guest

    you need to use @@name.

    On 3/14/06, Nathan Olberding <> wrote:
    > I've got a class. I want some methods of this class to be able to edit
    > some data that's "global" within any given instance of this class. For
    > example:
    >
    > class Person
    >
    > @name
    >
    > def changeName(newName)
    > @name =3D newName
    > end
    >
    > def sayName()
    > puts "My name is " + @name
    > end
    > end
    >
    > It seems that @name reverts back once I leave the scope of any method
    > that manipulates it.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Adam Shelly, Mar 14, 2006
    #2
    1. Advertising

  3. Adam Shelly wrote:
    > you need to use @@name.


    I was under the impression (newbie alert) that @name was for instances
    and @@name was for classes as a whole (ie, @@ variables change that
    value in all instances of Class). Is there a way to have variables that
    apply to all instances of a Class?

    --
    Posted via http://www.ruby-forum.com/.
     
    Nathan Olberding, Mar 14, 2006
    #3
  4. On Mar 14, 2006, at 11:18 PM, Nathan Olberding wrote:

    > I was under the impression (newbie alert) that @name was for instances
    > and @@name was for classes as a whole (ie, @@ variables change that
    > value in all instances of Class). Is there a way to have variables
    > that
    > apply to all instances of a Class?


    Use an initialize method:

    class Person
    def initialize(name="Anonymous")
    @name = name
    end
    attr_accessor :name
    def to_s
    "My name is #{@name}"
    end
    end

    person = Person.new
    puts person # -> My name is Anonymous
    person.name = "Fred"
    puts person # -> My name is Fred

    This should help you: http://ruby-doc.org/docs/ProgrammingRuby

    -- Daniel
     
    Daniel Harple, Mar 14, 2006
    #4
  5. Nathan Olberding

    Guest

    Hi --

    On Wed, 15 Mar 2006, Nathan Olberding wrote:

    > I've got a class. I want some methods of this class to be able to edit
    > some data that's "global" within any given instance of this class. For
    > example:
    >
    > class Person
    >
    > @name
    >
    > def changeName(newName)
    > @name = newName
    > end
    >
    > def sayName()
    > puts "My name is " + @name
    > end
    > end
    >
    > It seems that @name reverts back once I leave the scope of any method
    > that manipulates it.


    As you've learned from some of the other responses, @name is an
    instance variable. Each instance variable belongs to one object. You
    can always tell *which* object: it's whatever the default object
    (self) is, at the point where the "@var" is executed.

    Note that self changes between a class definition and an instance
    method definition:

    class Person
    puts self
    def some_method
    puts self
    end
    end

    Person.new.some_method

    This code will give you:

    Person
    #<Person:0x352814>

    In the top level of the class definition, self is the actual class
    object (Person), but inside an instance method, it's the instance
    (indicated by the #<Person...> expression).

    So... @name in the outer scope is actually an instance variable
    belonging to the class object, while inside any instance method, @name
    is an instance variable belonging to the instance. The two @name's
    have no connection to each other at all.

    You can use class variables to get a variable that can be seen in both
    scopes, but if you've got a property like "name" and you find yourself
    manipulating it outside of any instance method, something's probably
    in need of tweaking in the program design (since the name of any
    particular instance shouldn't be of concern at the class level).


    David

    --
    David A. Black ()
    Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

    "Ruby for Rails" chapters now available
    from Manning Early Access Program! http://www.manning.com/books/black
     
    , Mar 15, 2006
    #5
  6. wrote:
    >
    > Note that self changes between a class definition and an instance
    > method definition:


    Light bulbs floating above list readers' heads around the world just
    illuminated.

    Thanks for the great explanation!

    --Steve
     
    Stephen Waits, Mar 15, 2006
    #6
  7. Nathan Olberding

    Adam Shelly Guest

    On 3/14/06, Nathan Olberding <> wrote:
    > Adam Shelly wrote:
    > > you need to use @@name.

    >
    > I was under the impression (newbie alert) that @name was for instances
    > and @@name was for classes as a whole (ie, @@ variables change that
    > value in all instances of Class). Is there a way to have variables that
    > apply to all instances of a Class?


    Oops, I read the question too quickly. Sorry. David Black has the
    right explanation.
    -Adam
     
    Adam Shelly, Mar 15, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    973
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    504
  4. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page