Uninitialized constant Context


#1

print "What's your first name?"
first_name = gets.chomp
first_name = "Eric"
puts "#{Eric}"
answer.capitalize!

print "What's your Last name?"
last_name = gets.chomp
last_name = "Lin"
puts "#{Lin}"
answer.capitalize!

print "What city are you from?"
city = gets.chomp
city = "New York"
puts "#{New York}"
answer.capitalize!

print "What state/province are you from?"
state = gets.chomp
state = "NY"
puts "#{NY}"
answer.upcase!

I'm having a little trouble with this lesson, it says I'm completing them all but upon the answer. I receive the error:

uninitialized constant Context::Eric


#2

The uninitialized variable is Eric, you're trying to use the content of that variable but you have not created a variable by that name. You do however have a variable named first_name which refers to the value "Eric"


#3

The only thing missing is the variable by my name? How do i create a variable by "Eric"? It lets me pass the section but I feel like it's completely wrong.


#4

Are you supposed to have a variable with that name? Maybe you're not supposed to access that variable, but use another one.


#5

No the variables im supposed to use are first_name, last_name, city and state/province. I included those. I'm just wondering if I did it right or if I'm missing something.


#6

If those are the variables you are meant to have, then why are you trying to read some variable other than those? Maybe you should read one of the variables that you have defined instead.

name = "bob"
puts bob  # error, bob not defined

#7

I'm not, the codeacademy website errors says that.


#8

See my example code in my previous post. What is my mistake?
Should I solve it like this?

name = "bob"
bob = "name"
puts bob  # ok, prints "name"

#9

You didn't have the "#{}" symbol? Sorry I'm new to programming as a whole, I did a little python and some html/css.


#10

My point here is that you have to consider what you intend something to do. Do you intend to access a variable named Eric? If not, and an error message is telling you that you are accessing a variable called Eric that has not been defined, then to fix it, you would stop trying to read something that doesn't exist and instead ask yourself what you were trying to access and use that instead.

Patching it up with something that makes it run doesn't necessarily make it do what you want. You have to consider what you want to do and consider the difference between that and what is currently happening and remove that difference.


#11

I don't know. What you said sounds right but I don't know what it means or the steps to take to complete that. But I went back and tweaked it, the first name and last name went through but I got to city and it gave me the error:

undefined method `New' for #

print "What's your first name?"
first_name = gets.chomp
first_name = "Eric"
Eric = "first name"
puts "#{Eric}"
Eric.capitalize!

print "What's your Last name?"
last_name = gets.chomp
last_name = "Lin"
Lin = "last name"
puts "#{Lin}"
Lin.capitalize!

print "What city are you from?"
city = gets.chomp
city = "New York"
New York = "city"
puts "#{New York}"
New York.capitalize!

print "What state/province are you from?"
state = gets.chomp
state = "NY"
NY = "state
puts "#{NY}"
NY.upcase!

and again it says I'm correct, is this website bugged?

it's this lesson by the way,
https://www.codecademy.com/en/courses/ruby-beginner-en-MxXx5/0/6?curriculum_id=5059f8619189a5000201fbcb


#12

okay nevermind, i found the fix. Whatever I just did, I just thought maybe the space in New York was the problem so I just changed it to a one word and it worked.

print "What's your first name?"
first_name = gets.chomp
first_name = "Eric"
Eric = "first name"
puts "#{Eric}"
Eric.capitalize!

print "What's your Last name?"
last_name = gets.chomp
last_name = "Lin"
Lin = "last name"
puts "#{Lin}"
Lin.capitalize!

print "What city are you from?"
city = gets.chomp
city = "Bayside"
Bayside = "city"
puts "#{Bayside}"
Bayside.capitalize!

print "What state/province are you from?"
state = gets.chomp
state = "NY"
NY = "state
puts "#{NY}"
NY.upcase!

Results on right side :
What's your first name? Eric
Eric
What's your Last name? Lin
last name
What city are you from? Bayside
city
What state/province are you from? NY
"STATE\nPUTS "

Thanks for your help and input.


#13

You're still printing out the content of the variable Eric :confused:
And also Lin and Bayside and what not, why do you have variables that have those names?

You should have variables like first_name, last_name, city, state, there should be no mentions at all of instances of those

If my name is bob, then why would the program know anything about Eric? Or if it knows the user is Eric, why would it be asking?

Don't add what's missing if it isn't supposed to be there.
If something is not supposed to be there, don't use it.

If you get an error message like "invalid authorization code for missiles"
then the answer to that problem may not be to put in the authorizaiton code, instead you might want to not try to fire the missiles.

Ruby doesn't know what you want to do, when it tells you something is missing, that doesn't mean you have to provide it, it means that in order to do what you asked, it is missing, so you will have to decide yourself if you actually meant to do that at all. So if you don't mean to have a variable called Eric, then don't add one because it's missing. Stop trying to access it instead.

Write code based on your intention.


#14

well if i dont have those variables in, the error pops up
uninitialized constant Context

same if i remove the variable of eric, bayside, or NY.

instructions were :
- After each variable assignment: first_name, last_name, and city add the .capitalize! method
- For state use the .upcase! method


#15

Yes and so the question is why you are trying to access those variables.

If I say my name is bob, then why would Eric be part of the program logic? Makes no sense, it shouldn't be.

It should ask for a name and then print that name yes?

If you store the name in a variable called name, then what variable would you then use to obtain the name again? Eric? bob? Julia?

If you remove all occurrences of Eric in your code, then ruby will no longer tell you that that variable is missing, because you are no longer trying to access it.


#16

if i dont "access" or display those variables. codeacademy doesnt let me continue the lesson.

do you mind trying that section and letting me know what you displayed?

Eric is the first_name variable so i need that. If i dont make eric a variable, it says uninitialized constant context.

I'm just confused?


#17

Your code has no knowledge about its user.
So nowhere in the code should it say "Eric"

Here are instructions that can be executed. They are written in English

1) Ask the user for a name
2) print the users name

They do not mention Eric. see?

Or should it refer to all users as Eric?
Why not instead remember whichever name the user chose, and print that instead. You would not refer to that name as Eric, you would refer to it as the users name, the name of the user, user_name, you put the name in a variable so that you can use it again later.


#18

That doesn't correspond to the lesson. It's asking me to display the name along with specific methods.


#19

Yes, display the name.
Not display Eric.


#20

but not displaying eric leads to an error. how to fix?

so this? (i cant make anymore replies)

print "What's your first name?"
first_name = gets.chomp
first_name = "Eric"
puts "#{Eric}"
Eric.capitalize!

print "What's your Last name?"
last_name = gets.chomp
last_name = "Lin"
puts "#{Lin}"
Lin.capitalize!

print "What city are you from?"
city = gets.chomp
city = "Bayside"
puts "#{Bayside}"
Bayside.capitalize!

print "What state/province are you from?"
state = gets.chomp
state = "NY"
puts "#{NY}"
NY.upcase!

i just took out all the variables like eric, etc.