Class variables in Ruby

前端 未结 3 1730
盖世英雄少女心
盖世英雄少女心 2020-12-11 13:11

I\'ve come across the following example from this tutorial:

class Song
  @@plays = 0

  def initialize(name, artist, duration)
    @name = name
    @artist =         


        
相关标签:
3条回答
  • 2020-12-11 13:30

    The @@ variable will a class variable. This is generally bad practice. In your code its redundant because @plays == @@plays (unless you set @@plays elsewhere in your code (bad practice))

    Actually now that I look at it, they aren't really the same. @plays keeps a count of how many times an individual song has been played, and @@plays will keep a count of all songs. Still, its likely bad practice to use @@plays. Usually, you'd have a parent class like "Player" that is managing all the songs. There should be an instance variable called @total_plays in the "Player" class.

    0 讨论(0)
  • 2020-12-11 13:35

    Class variables are never really required. But the reason isn't that they're shared state. I mean, it's good to avoid shared state where you can, but that's not the real problem here.

    The reason they're recommended against is, as shown in that article, they are really confusing. In particular, a class's class variables are shared by its subclasses and instances of its subclasses. For example:

    class Parent
    end
    
    class Child1 < Parent
      @@class_var = "Child1's"
    end
    
    class Child2 < Parent
      @@class_var = "Child2's"
    end
    

    With this code, Child1 and its instances will all see a class variable named @@class_var with the value "Child1's" and Child2 and its instances will all see a class variable named @@class_var with the value "Child2's". But suppose later on we reopen Parent and write this:

    class Parent
      @@class_var = "Parent's"
    end
    

    Now Parent and the instances it creates will all see a class variable named @@class_var with the value "Parent's". But that's not all. Now that the parent class has this variable, Child1 and Child2 suddenly share the variable, so all of the @@class_vars have the value "Parent's". And if you reassign the variable in Child1, it's still shared, so all of the classes get updated. How confusing!

    Instead of class variables, you can just use instance variables of the class, like this:

    class Parent
      @class_var = "Parent's"
      def self.class_var
        @class_var
      end
    end
    
    class Child1 < Parent
      @class_var = "Child1's"
    end
    
    class Child2 < Parent
      @class_var = "Child2's"
    end
    

    Now, Parent.class_var will return "Parent's", Child1.class_var will return "Child1's" and Child2.class_var will return "Child2's" — just like you expect.

    0 讨论(0)
  • 2020-12-11 13:35

    A class variable is a variable that is shared among all instances of a class. This means only one variable value exists for all objects instantiated from this class. This means that if one object instance changes the value of the variable, that new value will essentially change for all other object instances. Another way of thinking of thinking of class variables is as global variables within the context of a single class.

    @@plays #is a class variable
    @plays  #is an instance variable
    $plays  #is a global variable accessed outside a class
    

    So in your example you created a class variable @@plays to calculate the total number of songs played for all songs. Since it is a class variable, it cannot be accessed outside the class alone. If you wanted to access the total number of plays you can use a global variable. They start with a dollar sign $plays (in your case). I warn you, you should stay away from using global variables as they are problematic for numerous reasons. One thing you may consider is to create a method that pushes all song instances into an array. You can then sum all plays across all songs through iterators. Way more secure, way less prone to programmer error.

    Edit: Here are why global variables are bad

    Are global variables bad?

    0 讨论(0)
提交回复
热议问题