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 Ruby Objects and Classes Build a Bank Account Class Part 4: Printing the register

Why can you write #{name} instead of #{@name} when doing the string interpolation?

Does Ruby infer that name must be instance variable?

1 Answer

Ruby will not infer it is an instance variable, it will actually infer it is a method call if a local variable is not defined from what I read here (depending on where/how this is all happening)

http://www.tutorialspoint.com/ruby/ruby_variables.htm

"Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}.

When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments."

It depends on where you are writing it. (The instance variable assignment and the local variable assignment)

Do you have a question on a specific project/with code?

In the video he has the attr_reader :name

which creates the method call

So basically the #{name} is actually calling the get function that returns the instance variable name for that class.

When you write

attr_reader :name

it is similar to defining a function that returns the value for a specific instance variable, such as:

def name
  @name
end