← Back to Upcase

Intro to Ruby on Rails


(Chris Kuffo) #1

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 methodget’ for main:Object (NameError)


(Matthew Mongeau) #2

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.


(Chris Kuffo) #3
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


(Matthew Mongeau) #4

You want to be using

if guess == number

(Chris Kuffo) #5

I didn’t copy so good…lol Thank you for the quick response!


(Chris Kuffo) #6

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


(Patrik Bóna) #7

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


(Chris Kuffo) #8

Ok got it …but then why not say rand(11)?


(Patrik Bóna) #9

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.


(Chris Kuffo) #10

Thank you patrik


(Chris Kuffo) #11

When setting up a database in ruby should I look into another database solution besides the default in rails ex: PostgreSQL, MySql or MongoDB?


(Matthew Mongeau) #12

@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.


(Chris Kuffo) #13

Awesome… Its amazing one day into my membership and i have learned so much. Thank you all.


(Chris Kuffo) #14

What does this operator signify “=>” in the following hash example.

card1 = {
"front" => "cat"
"back" => "neko"

}


(Chad Pytel) #15

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.


(Chris Kuffo) #16

Thanks Chad… Will do!