Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
When using variables inside classes, only instance variables, which are prefixed with the `@` character, will be visible to all of the methods in the class. A variable that only exists inside of a code block or method is called a local variable. The whole concept is called scope.
Code Samples
class Name
attr_accessor :title, :first_name, :middle_name, :last_name
attr_reader :first_and_middle_name
def initialize(title, first_name, middle_name, last_name)
@title = title
@first_name = first_name
@middle_name = middle_name
@last_name = last_name
end
def full_name
@first_and_middle_name = @first_name + " " + @middle_name
@first_and_middle_name + " " + @last_name
end
def full_name_with_title
@title + " " + full_name()
end
end
name = Name.new("Mr.", "Jason", "", "Seifer")
puts name.full_name_with_title
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Ahmed Mohamed Fouad
11,735 Points2 Answers
-
David Greenstein
6,734 Points1 Answer
-
Julia Fitzer
Front End Web Development Techdegree Student 5,574 Points1 Answer
-
Vamsi Pavan Mahesh Gunturu
4,541 Points3 Answers
-
sebastien penot
3,292 PointsWhen creating an instance variable for a method, why doesn't Jason adds it to the initialize method?
1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up