1

I'm into Ruby on Rails programming for almost 5 weeks now.

I was wondering why people always use instance variables instead of local ones.

I always thought that you would use instance variables only for classes (so these instance variables are the attributes of the class).

But people also use them not only for being attributes of a class. And this is the part where I am getting confused.

For instance, take a look at these lines of codes:

class Foo 
    def print_a_hello
        puts "Hello World"
    end
end

@instance_variable = Foo.new
@instance_variable.print_a_hello
# => "Hello World"

locale_variable = Foo.new
locale_variable.print_a_hello
# => "Hello World"

So, who of you got a great explanation for me?

4 Answers 4

4

I was wondering why people always use instance variables instead of locale ones.

I'm not sure how you get that impression. I certainly don't "always" use instance variables. I use instance variables when I need an instance variable, and I use local variables, when I need a local variable, and most code I see does it the same way.

Usually, it doesn't even make sense to interchange them. They have completely different purpose: local variables have static lexical scope, instance variables have dynamic object scope. There's pretty much no way to interchange them, except for the very narrow case of a simple single-file procedural script, where the dynamic scope of the top-level main object and the lexical scope of the script body are identical.

I always thought that you would use instance variables only for classes (so these instance variables are the attributes of the class).

No. Instance variables are attributes of the instance (i.e. object), not the class, that's why they are called "instance variables", after all. Class variables are attributes of the class, but class variables are a different beast and only used in very specific circumstances. (Classes are objects (i.e. instances), too, so they can have instance variables as well; there's generally no need to use class variables, which have some weird and un-intuitive properties, unless you specifically need those weird and un-intuitive properties).

For instance, take a look on this short codelines:

class Foo 
  def print_a_hello
    puts "Hello World"
  end
end

@instance_variable = Foo.new
@instance_variable.print_a_hello
# => "Hello World"

locale_variable = Foo.new
locale_variable.print_a_hello
# => "Hello World"

This is the case I mentioned above: in this specific case (and only in this case), the dynamic scope of the top-level main object and the static lexical scope of the script body are identical, so it doesn't matter whether you use a local variable of the script body or an instance variable of the main object.

However, if we make just a tiny change to that, by adding a second script and requireing it from the first, that condition will no longer hold, because we now have two separate script bodies and thus two separate script scopes, but still only one top-level object.

The idiomatic way in your example would definitely be to use a local variable, and nobody I know would do otherwise.

Sign up to request clarification or add additional context in comments.

Comments

2

Best use case for instance variables is in Controller's when you want to pass parameter to the view.

Then you use something like

class TestController < ActionController::Base
  def show
    @usable_in_view = Test.first
    not_usable_in_view = Test.first
  end
end

In your view you can now use @usable_in_view, but cant use variable not_usable_in_view. Most people always use instance variable in controllers even if they do not need them in view, because they do not understand why they need instance variable

2 Comments

With instance variables you can't run into trouble then. Because with locale variables on a view you are not able to get access to them anymore, right? So why are there still locale variables in Ruby? You can use instance variables for everything...
Using instance vars is akin to using global vars. By electing to use local vars, you're controlling the scope of when that variable is available whereas instance variables broadens the scope of where that variable is accessible (and changeable). The broader the scope of the var, the more likely you are to have undesirable/undetected side-effects in your code that is hard to track down. You can potentially be combing through a few thousand lines of code to find the issue. So think about your solutions and choose the right scope for your variables to keep your debugging effort to a minimum.
1

Instance variables are used so that they can be accessed in the view page. Local variables are not accessible in the view. It has become the habit even I sometimes write instance variables though it is not required in the view.:-)

1 Comment

This example is indeed clear for me. I know how to create Objects in Ruby and also know the "major" use of instance-variables. My Question above was more about... why is it possible to use "instance variables" like everywhere in Ruby? You can easily use instance variables only instead of using locale variables. So this fact make the use of locale variables irrelevant for me.
0

People probably get in the [bad] habit of using instance variables everywhere since it's common in Rails to use them to get information from the controller to the view.

In my own Ruby code I use instance variables only when I need to, local variables otherwise. That's the proper way to use Ruby.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.