[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Method vs variable assigment
From:       Gioele Barabucci <ml () gioelebarabucci ! com>
Date:       2006-04-22 18:42:03
Message-ID: e2dtda$702$1 () sea ! gmane ! org
[Download RAW message or body]

I can't understand why this script

| #!/usr/bin/ruby
| class A
|   attr_accessor :v
|
|   def test
|     p @v
|   end
|
|   def mod_explicit_setter
|     self.v = "explicit setter"
|   end
|
|   def mod_setter
|     v = "setter"
|   end
|
|   def mod_direct
|     @v = "direct"
|   end
| end
|
| a = A.new
| p a.methods - Object.new.methods
|
| a.mod_setter; a.test
| a.mod_direct; a.test
| a.mod_setter; a.test
| a.mod_explicit_setter; a.test

have this output

| ["mod_setter", "mod_direct", "v", "v=", "mod_explicit_setter", "test"]
| ./vm.rb:7: warning: instance variable @v not initialized
| nil                 <- correct, v is a local variable
| "direct"            <- correct
| "direct"            <- correct? v= is an already seen method
| "explicit setter"   <- correct

I thought that, once ruby sees a methods, it prefers to call that method
instead of creating a new local variable.

--
Gioele <dev@gioelebarabucci.com>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic