views:

260

answers:

5

The ruby class-instance stuff is giving me a headache. I understand given this...

class Foo
  @var = 'bar'
end

...that @var is a variable on the created class's instance.

But how do I create a sub-class overridable class variable?

Here is an example of what I would do in Python:

class Fish:
var = 'fish'
def v(self):
    return self.var

class Trout(Fish):
    var = 'trout'

class Salmon(Fish):
    var = 'salmon'

print Trout().v()
print Salmon().v()

Which outputs:

trout
salmon

How do I do the same thing in ruby?

+3  A: 

Check this out, it really explains it in-depth: http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby

Basically, if you want inheritance then use an instance variable.

hobodave
That explains why the `@@` was causing me headaches. Sheesh. So confused.
The Doctor What
+3  A: 

@var mentioned above is called class instance variable, which is different from instance variables... read the answer here to see the diff.

Anyway this is the equivalent Ruby code:

class Fish
  def initialize
    @var = 'fish'
  end

  def v
    @var
  end
end

class Trout < Fish
  def initialize
    @var = 'trout' 
  end
end

class Salmon < Fish
  def initialize
    @var = 'trout' 
  end
end

puts Trout.new.v
puts Salmon.new.v
khelll
It's not really, because you have to override the initialize every time you subclass. Which isn't very usable. It may have been better in my example to include some code in initialize.
The Doctor What
+2  A: 

To contrast @khelll's answer, this uses instance variables on the Class objects:

class Fish
  # an instance variable of this Class object
  @var = 'fish'

  # the "getter"
  def self.v
    @var
  end

  # the "setter"
  def self.v=(a_fish)
    @var = a_fish
  end
end

class Trout < Fish
  self.v = 'trout'
end

class Salmon < Fish
  self.v = 'salmon'
end

p Trout.v   # => "trout"
p Salmon.v  # => "salmon"

Edit: to give instances read-access to the class's instance variable:

class Fish
  def type_of_fish
    self.class.v
  end
end

p Trout.new.type_of_fish   # => "trout"
p Salmon.new.type_of_fish  # => "salmon"
glenn jackman
No, not quite. You aren't doing a Trout.new or Salmon.new at the end. You're using the class itself. I want the *instance* to get the class variables.
The Doctor What
@The Doctor -- how about now?
glenn jackman
Yup. I think it is now functionally equiv. to the answer I gave. You're just writing the accessors manually.
The Doctor What
Have to write the accessors: all instance variables are private, and the `attr_*` methods apply to instance objects not class objects.
glenn jackman
A: 

It's a common mistake made by Java coders coming to Ruby as well, and one of the big conceptual jumps I had to get my head around. At first it seems odd, but it's really one of the cooler aspects of Ruby -- all code is executable, including class definitions.

So, instance variables must be declared inside methods. It has to do with how 'self' is evaluated. 'self' is the current object. The interpreter will lookup method calls and variable references first in 'self':

class Fish
    @var = "foo" # here 'self' == Fish, the constant which contains the class object  
    def foo
        # do foo
    end
end

fish = Fish.new
fish.foo # here 'self' == fish, an instance of Fish

In a class definition, 'self' is set to be the class object being defined, so any references within a class definition will refer to that class object, in this case Fish.

When a method is called on an instance of Fish, however, self is set to be the receiver of the call, the particular instance of Fish. So outside of a method definition, self is the class object. Inside a method, self is the instance of the receiver. This is why @var outside of a method definition is more like a static variable in Java, and @var inside a method definition is an instance variable.

Dave Sims
Typo: `fish.foo` should be `fish.var`
The Doctor What
Actually, no. I didn't define 'foo' but the point was that the Ruby interpreter would see 'fish' as the receiver of the call and set 'self' to 'fish' in order to resolve the reference. I've added the foo method for clarity though. Calling 'fish.var' would throw a NoMethodError.
Dave Sims
Ah. My mistake. Thanks for explaining it.
The Doctor What
+1  A: 

Here's the version I ended up figuring out using hobodave's link:

class Fish
  class << self
    attr_accessor :var
  end

  @var = 'fish'
  def v
    self.class.var
  end
end

class Trout < Fish
  @var = 'trout'
end

class Salmon < Fish
  @var = 'salmon'
end

puts (Trout.new).v   # => trout
puts (Salmon.new).v  # => salmon

Notice that subclassing only requires adding an @var -- no need to override initialize.

The Doctor What