Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better? Creating a instance variable or passing around a local variable in Ruby?

Tags:

ruby

In general what is the best practice and pro/cons to creating an instance variable that can be accessed from multiple methods or creating an instance variable that is simply passed as an argument to those methods. Functionally they are equivalent since the methods are still able to do the work using the variable. While I could see a benefit if you were updating the variable and wanted to return the updated value but in my specific case the variable is never updated only read by each method to decide how to operate.

Example code to be clear:

class Test
  @foo = "something"

  def self.a
    if @foo == "something"
      puts "do #{@foo}"
    end
  end

  a()
end

vs

class Test
  foo = "something"

  def self.a(foo)
    if foo == "something"
      puts "do #{foo}"
    end
  end

  a(foo)
end
like image 674
joe Avatar asked Nov 23 '10 00:11

joe


People also ask

When should we use instance variable?

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

How does a local variable differ from an instance variable when empty Ruby?

The main difference between instance variable and local variable is that instance variable is a variable that is declared in a class but outside a method, while a local variable is a variable declared within a method or a constructor.

What is the difference between class variable and instance variable in Ruby?

What is the difference between class variables and class instance variables? The main difference is the behavior concerning inheritance: class variables are shared between a class and all its subclasses, while class instance variables only belong to one specific class.


2 Answers

I don't pass instance variable around. They are state values for the instance.

Think of them as part of the DNA of that particular object, so they'll always be part of what makes the object be what it is. If I call a method of that object, it will already know how to access its own DNA and will do it internally, not through some parameter being passed in.

If I want to apply something that is foreign to the object, then I'll have to pass it in via the parameters.

like image 189
the Tin Man Avatar answered Oct 03 '22 04:10

the Tin Man


As you mentioned, this is a non-functional issue about the code. With that in mind...

It's hard to give a definitive rule about it since it depends entirely on the context. Is the variable set once and forgotten about it, or constantly updated? How many methods share the same variable? How will the code be used?

In my experience, variables that drive behavior of the object but are seldom (if at all) modified are set in the initialize method, or given to the method that will cascade behavior. Libraries and leaf methods tend to have the variable passed in, as it's likely somebody will want to call it in isolation.

I'd suggest you start by passing everything first, and then refactoring if you notice the same variable being passed around all over the class.

like image 27
Andres Freyria Avatar answered Oct 03 '22 02:10

Andres Freyria