Skip to content

Instantly share code, notes, and snippets.

@darinthompson
Created December 28, 2015 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darinthompson/de1ad7f1c901c978019c to your computer and use it in GitHub Desktop.
Save darinthompson/de1ad7f1c901c978019c to your computer and use it in GitHub Desktop.
Greeting not working. I don't understand what's happening.
class Person
attr_accessor :name, :age
def initialize
@name = name
end
def get_name
puts "What is your name?"
name = gets.chomp
return name
end
def greeting
puts "Hello #{name}"
end
end
darin = Person.new
darin.get_name
darin.greeting
@joshuadavidson
Copy link

@darinthompson I'm new to Ruby but I'll take a stab: It looks like the problem is arising from how to define and call the name variable. Your variable name should be an instance variable called with an @ symbol like so @name which limits it's scope to an instance of the class Person. Your get_name method should store the name like so: @name = gets.chomp, same applies when calling that variable later in the greeting method: puts "Hello #{@name}". Give it a try and let me know if that helps. You may also want to check out this stackoverflow thread:

@darinthompson
Copy link
Author

Hey, yeah, that was kind of what it was. I learned that since putting this up. Thanks for the input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment