ab11 ab11 - 1 year ago 145
Ruby Question

Are Ruby class variables similar to the Java static variables?

I am transitioning to Ruby from Java and trying to understand Ruby scoping.

It seems class variables behave the same as static variables in Java. Is this interpretation correct? If so, are Ruby global variables then equivalent to public static variables in Java?

Answer Source

There's a lot of similarity between Ruby and Java by virtue of them being object-oriented, but their family tree is different. Ruby leans very heavily on Smalltalk while Java inherits from the C++ school of thinking.

The difference here is that Ruby's concept of public/private/protected is a lot weaker, they're more suggestions than rules, and things like static methods or constants are more of a pattern than a construct in the language.

Global variables are frowned on quite heavily, they can cause chaos if used liberally. The Ruby way is to namespace things:

$ugly_global = 0  # Not recommended, could conflict with other code
                  # Ownership of this variable isn't made clear.

$ugly_global += 1 # Works, but again, it's without context.

module UglyCounter    # Defines a module/namespace to live in
  def self.current    # Defines a clear interface to this value
    @counter ||= 0    # Initializes a local instance variable

  def self.current=(v)   # Allow modification of this value
    @counter = v.to_i    # A chance to perform any casting/cleaning

UglyCounter.current += 1   # Modifies the state of a variable, but
                           # the context is made clear.

Even a thin layer like this module gives you the ability to intercept read/write operations from this variable and alter the behaviour. Maybe you want to default to a particular value or convert values into a normalized form. With a bare global you have to repeat this code everywhere. Here you can consolidate it.

Class variables are a whole different thing. They're also best avoided because sharing data between the class and instances of this class can be messy. They're two different contexts and that separation should be respected.

class MessyClass
  @@shared = 0

  def counter

  def counter=(v)
    @@shared = v

This is a pretty rough take on how to use a shared class-level instance variable. The problem here is each instance is directly modifying it, bypassing the class context, which means the class is helpless. This is fundamentally rude, the instance is over-extending its authority. A better approach is this:

class CleanerClass
  def self.counter
    @counter ||= 0

  def self.counter=(v)
    @counter = v.to_i

  # These are reduced to simple bridge methods, nothing more. Because
  # they simply forward calls there's no breach of authority.
  def counter

  def counter=(v)
    self.class.counter = v

In many languages a static class method becomes available in the scope of an instance automatically, but this is not the case in Ruby. You must write bridge/proxy/delegate methods, the terminology here varying depending on what you're used to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download