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 trialShreemangal Sethi
Full Stack JavaScript Techdegree Student 15,552 PointsInstance Variable
I want to know when creating the full_name method - in the string interpolation why isn't "@" used before the variable. I created this rb file on atom and my method looks like this
def full_name "#{@first_name} #{@middle_name} #{@last_name}" end
and it returns the full name - "Shreemangal Sethi", with an extra space.
But when i do def full_name "#{first_name} #{middle_name} #{last_name}" end
i get this error
in full_name': undefined local variable or method
middle_name' for #<Contact:0x00007f92120c40a8> (NameError)
Did you mean? midddle_name
middle_name=
why is that?
1 Answer
Tiago Garcia
4,835 Pointswell, when you refer to #{first_name}, the interpreter thinks that's a local variable. When it does not find that on the function you created, it errors out even if there is a instance variable of the same name. the interpolation needs to be specifically told it is an instance variable by the use of "@" before the variable's name.