Created
December 28, 2015 19:36
-
-
Save darinthompson/de1ad7f1c901c978019c to your computer and use it in GitHub Desktop.
Greeting not working. I don't understand what's happening.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
@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. Yourget_name
method should store the name like so:@name = gets.chomp
, same applies when calling that variable later in thegreeting
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: