Forum: Ruby Method vs variable assigment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
392450979e40b958c4bdad04579e74d5?d=identicon&s=25 Gioele Barabucci (Guest)
on 2006-04-22 20:43
(Received via mailing list)
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-22 20:46
(Received via mailing list)
Hi --

On Sun, 23 Apr 2006, Gioele Barabucci wrote:

> |   def mod_explicit_setter
> | end
>
> | ["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.

No; actually whenever it sees this:

   var = value

it assumes that var is a local variable. So for methods ending in =
you get the syntactic sugar on the right (= instead of .=()) but not
on the left (you have to indicate the receiver explicitly).


David

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

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
This topic is locked and can not be replied to.