"Ruby Operators and Control Structures" was retired on November 27, 2017. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Ruby Objects and Classes!
      
    
You have completed Ruby Objects and Classes!
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 upRelated 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