Module 1 - Display a Welcome Message

Lets make a game of blackjack.   If you don’t know how to play the rules are here:

Computers understand programming languages and Ruby is a good place for you to start. 

Lets start by showing you how to talk to the computer using Ruby.  You have to be precise and if we misspell a word, well the computer is not going to understand it.

We want to display a welcome message. The command to do this is puts which is a shortened name for put string. 

Strings are sequences of characters or numbers that can be displayed on a screen or written to a file or printed on a printer.  You cannot add two strings together or divide by them.  Think about it:  4 divided by 2 makes sense.  “A” divided by “B” doesn’t.  In many programming languages, double quotes (“) are used to indicate that something is a string.

So now it’s time to do it:

Type:
 

puts "Welcome to Black Jack"
 

 

inside of an environment that lets you enter Ruby commands.   Eventually, it is going to be helpful if your own computer has Ruby installed.  Detailed directions for doing all that are included here.

You should see the message:  Welcome to Black Jack.

In many programming languages books, the first thing they tell you to do is have the computer says HELLO WORLD.   To do that we just use the same puts command:

puts "Hello World."

Now that we showed you how to do it and you have it working, its a good time to show you the kinds of things that can go wrong when writing programs.

Change your puts command to put as in:

put "Welcome to Black Jack"
petrycki
 

You’re going to need to change the flow based on the student having experienced the error. You don’t have to tell them.

You’ll get the following (which should send confusing thoughts to your brain and make you less sure that learning how to program is going to be so fun).   Trust us, its an awesome feeling when you get something to work that you thought at the start that you might not be able to get it work.  

NoMethodError: undefined method `put’ for main:Object

You’ll get a nice message of “NoMethodError: undefined method for put”.   It is amazing anyone thinks computers are so smart.    People are far more resilient.  One little character.  

It is annoying that computers have to always care about every last little letter.   

Lets take this error message piece by piece.

Well first it tells you the type of error you have followed by a colon.  In this case you have a NoMethodError.  

Well it turns out a method is piece of code that you might want to repeat.  Terrific name isn’t it.   We will talk about this more later.  Some languages call it a function based on the mathmatical definition of a function.   In Math and in Computer Science, functions are used to make life easier to express equations.   A simple function is f(x) = x + 2.  This means whenever we send the function a value for x, we want it to return a value that adds two to the value we sent.  So if we ask for f(4), the result of the function is 6. 

Anyway,  you simplify your computer programs with methods and we’ll get to all that later.  We just wanted you to know its a thing that comes in handy to make it so you don’t have to write the same programming code over and over.

So Ruby has a way to make it so you can define new methods.  When you entered puts, that referenced a method that was written by the people who invented Ruby and its job is to output the string that follows to the screen.  

When you got rid of the s in puts and just entered put, the ruby interpreter (the thing that processes each Ruby command) decided you must have written a new method called put.  It looked for it and couldn’t find it and gave you a NoMethodError.

Now fix your PUT to PUTS and get it to work again.   What a relief that is.

Ready for one more typo and its impact?

Lets get rid of the double quotes. 

so your code will now look like

puts Welcome to Blackjack

 

Now you get a new error message

NameError: undefined local variable or method `blackjack’ for main:Object

Well now the Ruby interpreter says you entered PUTS correctly so it was thinking you would follow it with a nice string.   With quotes, the string has a value that is inside of the quotes, without quotes the string is assumed to be in a variable. 

When you took off the quotes around Welcome to blackjack -- ruby thought you were referring to a variable named WELCOME.   We never defined this variable so it then said undefined local variable.  

We showed you these two error messages for a reason. So if you end up getting one, you will have seen it before and you will know that probably it is some small issue that you have mistyped something.  Sometimes you won’t find the error because its a little like looking for spelling errors in your own e-mails (without a spell checker).  You don’t see the mistake because you are thinking you got it right.  Sometimes it helps to take a break, go do something else for a few minutes, come back and look for the error again.

Also, remember everything you type into a program should make sense to you.  Don’t just type something because its in a book or someone told you to do it, make sure it makes sense before you type it and you know what you are trying to accomplish. 

So fix the program you have written and we are ready to move on and get this game working. 

puts Welcome to Blackjack"