Going through the guessing_game tutorial and i keep receiving this error… Not sure why I copied exactly as written. Any suggestions? Can i post code directly here?
<main>': undefined local variable or method
get’ for main:Object (NameError)
Going through the guessing_game tutorial and i keep receiving this error… Not sure why I copied exactly as written. Any suggestions? Can i post code directly here?
<main>': undefined local variable or method
get’ for main:Object (NameError)
You can paste code directly here. If you look at the icons above the input field there is a little computer after a double quote, click that.
puts "Welcome to my guessing game"
puts “_” * 20
print “Guess my number >”
number = 5
guess = gets.to_i
puts guess.inspect
if get == number
puts “You Win!”
end
You want to be using
if guess == number
I didn’t copy so good…lol Thank you for the quick response!
Can you explain why I needed to add the + 1 after rand(10) in the example?
number = rand(10) + 1
puts “Welcome to my guessing game”
puts “_” * 20
won = false
5.times do
print “Guess my number (1-10) >”
guess = gets.to_i
if guess == number
won = true
break
end
end
if won
puts “You Win!”
else
puts “You lost. The number was #{number}”
end
rand(10) returns random number from 0 to 9. So if you need random number from 1 to 10, then you need to add +1
Ok got it …but then why not say rand(11)?
rand(11) returns random number from 0 to 10 and I presume that you need random number between 1 - 10.
so rand(11) can return zero and you probably don’t want it.
Thank you patrik
When setting up a database in ruby should I look into another database solution besides the default in rails ex: PostgreSQL, MySql or MongoDB?
@chriskuffo for a production application I would highly suggest it. My defacto choice is PostgreSQL, but there are valid reasons to use the others. If you’re deploying to Heroku, I’d suggest using PostgreSQL. Also as discussed in office hours today but you can also do
rand(1..10)
instead of
rand(10) + 1
and get the same result.
Awesome… Its amazing one day into my membership and i have learned so much. Thank you all.
What does this operator signify “=>” in the following hash example.
card1 = {
"front" => "cat"
"back" => "neko"
}
I had answered this last question in the chat, but I wanted to follow up here with the answer as well. @chriskuffo it would be helpful to post a new topic for each separate question, this’ll make it easier for others to participate and keep the conversations clean.
In response to your question, the first items are the keys, the second items in the value.
So "front"
is the key and "cat"
is the value.
So you can do card1["front"]
and get "cat"
and you are doing a lookup in the hash on "front"
The =>
is the syntax for the mapping, and yes, it is intentionally meant to look like an arrow.
Thanks Chad… Will do!