SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    instance variables and accessors question...

    I have a doubt; if i had:

    Code:
    class Foo
    
      def first_instance_method
        @instance_variable_foo = 2
      end
    
      def second_instance_method
        @instance_variable_foo = 10
      end
    
      def foo_variable_accessor_method
        @instance_variable_foo
      end
    
    end
    ...the accessor method which "@instance_variable_foo" print out of the two instance methods?

  2. #2
    SitePoint Enthusiast satishtalim's Avatar
    Join Date
    Jun 2005
    Location
    Pune, India
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skyblaze View Post
    I have a doubt; if i had:

    Code:
    class Foo
    
      def first_instance_method
        @instance_variable_foo = 2
      end
    
      def second_instance_method
        @instance_variable_foo = 10
      end
    
      def foo_variable_accessor_method
        @instance_variable_foo
      end
    
    end
    ...the accessor method which "@instance_variable_foo" print out of the two instance methods?
    I am not quite sure what your doubt is:
    In case you add the following code to your class -

    Code:
    f = Foo.new
    puts f.first_instance_method
    puts f.second_instance_method
    puts f.foo_variable_accessor_method
    It prints out:

    Code:
    >ruby tmp.rb
    2
    10
    10
    >Exit code: 0
    For the object f above there's only one instance of the variable @instance_variable_foo
    Last edited by satishtalim; Feb 25, 2007 at 23:43.
    Free Ruby Study Notes
    Blog on Java, Ruby programming, Chess and Making $$$ online

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by satishtalim View Post
    I am not quite sure what your doubt is:
    In case you add the following code to your class -

    Code:
    f = Foo.new
    puts f.first_instance_method
    puts f.second_instance_method
    puts f.foo_variable_accessor_method
    It prints out:

    Code:
    >ruby tmp.rb
    2
    10
    10
    >Exit code: 0
    For the object f above there's only one instance of the variable @instance_variable_foo
    why it prints out two times ten instead two times two?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is only one variable. It's like this:

    Code:
    puts a = 2  # sets a to 2 and prints 2
    puts a = 10 # sets a to 10 and prints 10
    puts a # prints a (which is 10 now)
    Code:
    f = Foo.new
    puts f.first_instance_method # sets the variable to 2 and returns 2
    puts f.second_instance_method # sets the *same* variable to 10 and returns 10
    puts f.foo_variable_accessor_method # returns the instance variable (which is 10 now)

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    There is only one variable. It's like this:

    Code:
    puts a = 2  # sets a to 2 and prints 2
    puts a = 10 # sets a to 10 and prints 10
    puts a # prints a (which is 10 now)
    Code:
    f = Foo.new
    puts f.first_instance_method # sets the variable to 2 and returns 2
    puts f.second_instance_method # sets the *same* variable to 10 and returns 10
    puts f.foo_variable_accessor_method # returns the instance variable (which is 10 now)
    ok, so the last called method is responsible for the actual value of that variable right?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •