R
Ruby Freak
I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
statement:
"Class instance variables cannot be referenced from within instance
methods and, in general are not very useful"
huh?..
This , in my feeble mind, contradicts everything else I have ever
read, except that the example he gives is similar to the @y = 7
example below, and in that case, @y is not available to the accessor
or any other method that I have played with.
In the following code, the original assignment of @x = 7 and @y = 5
don't seem to do anything, even with the attr_accessor. I know that
both are read by the compiler as I can assign @y = 5/0 and get a
division by zero error.
So, could someone please explain why the accessor for @y does not work
here. If I hand write a reader and writer for @y, it works just fine.
There are different scopes here, but I had the impression that the
accessor would break down that barrier and make @y available
throughout the class just as the initialize method is able to access
the class variable @x and assign the passed in value. That value is
then available to the @x accessor.
I thought for a while that the @y accessor might work in the singleton
class of Myclass, but it doesn't.
class Myclass
attr_accessor :y, :x
@x = 7
@y = 5
def initialize(new_val= l)
@x = new_val ? new_val : 0
end
end
mc = Myclass.new(3)
puts mc.y #=> Nil
puts mc.x #-> 3
I know I can make the @x and @y class variables available to instance
methods by referencing them inside defined methods and the scope seem
to be class wide, but it just seems like the first assignments above
should work as written.
Thanks in advance
statement:
"Class instance variables cannot be referenced from within instance
methods and, in general are not very useful"
huh?..
This , in my feeble mind, contradicts everything else I have ever
read, except that the example he gives is similar to the @y = 7
example below, and in that case, @y is not available to the accessor
or any other method that I have played with.
In the following code, the original assignment of @x = 7 and @y = 5
don't seem to do anything, even with the attr_accessor. I know that
both are read by the compiler as I can assign @y = 5/0 and get a
division by zero error.
So, could someone please explain why the accessor for @y does not work
here. If I hand write a reader and writer for @y, it works just fine.
There are different scopes here, but I had the impression that the
accessor would break down that barrier and make @y available
throughout the class just as the initialize method is able to access
the class variable @x and assign the passed in value. That value is
then available to the @x accessor.
I thought for a while that the @y accessor might work in the singleton
class of Myclass, but it doesn't.
class Myclass
attr_accessor :y, :x
@x = 7
@y = 5
def initialize(new_val= l)
@x = new_val ? new_val : 0
end
end
mc = Myclass.new(3)
puts mc.y #=> Nil
puts mc.x #-> 3
I know I can make the @x and @y class variables available to instance
methods by referencing them inside defined methods and the scope seem
to be class wide, but it just seems like the first assignments above
should work as written.
Thanks in advance