Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

@ --- what does it mean?

hey helpers out there I basically just want to know what the @ sign means what's the difference between doing this: @balance=0 and this balance=0 ?

Thank you! Marwan

3 Answers

The difference is that they have different "scope". A @ symbol is recognizable to your code throughout the whole instance of an object. Without the @ symbol the variable's scope becomes local to the code you declared it in.

def amethod
  local_variable = "This cannot be seen/accessed by the other method"
  @instance_variable = "This variable can be accessed by the other method"
end

def anothermethod
  local_variable = "Even with the same name as the local variable
 declared above, this one is totally different, and altering it won't affect the other."
  @instance_variable #this variable has the same value as the one above
end
Sergio Barrera
Sergio Barrera
4,198 Points

Instance variable --> @my_var (created as an instance of a class, it can be called outside it's method). local variable ----> my_var (for example, this variable is only valid inside a method. If you call it outside its method, you'll get an error.)

Thank you both! That cleared things up