Welcome! In this module, we’re going to start learning how to program in Ruby.
It will be a long process and you will have moments of elation, excitement and, sometimes, we're sorry to say frustration. But we promise you, learning to think in the disciplined way that programming teaches will change your life for the better.
Our method for teaching programming starts from a different place than you might expect. If you've ever looked at programming tutorial or book you might be expecting lectures on electricity, how to convert decimal numbers to binary numbers, data types, variables, or something like that. We've designed our curriculum to not do that.
We think there's a different, and better, way. We start by thinking deeply about having conversations.
Many of the ideas we’ll share in this module ask you to reflect on "when you learned to talk" or "when you’re talking with a friend." We call this "programming as conversation" and it gets you to think about communicating instead of "memorizing strange programmer words, facts, and symbols." You'll learn those strange words and symbols eventually, of course, but you'll see them as part of the act of communication, something you find natural right now.
At its heart, programming is about communicating strategies for solving problems. You:
- Document these strategies for yourself
- Communicate these documented strategies to others
- And then, lastly, ask a computer to run these strategies very fast
A "program" then is actually a strategy for solving a problem that people use to communicate with one another as well as give to a machine.
A recipe for a fancy dish is a good comparison: you document the ingredients and preparation for yourself using a human-language. You can then communicate that plan for food preparation by sharing the recipe with a friend.
Even in recipes, we commonly use special notation like 3g
for "3 grams" or
½C.
for "½ Imperial Cup." In recipes, we usually use notation to save time
so our wrists don't get tired. But with computers, we have to communicate using
notation. Human language is often vague and imprecise and computers are
terrible about handling unclear directions. To help prevent communication
breakdowns, we converse with them using a special notation. The "special
notation" is the programming language's rules or "syntax."
DEFINITION Notation: A specialized vocabulary or system of symbols for communication. Examples Musical notation, arithmetic symbols, chemistry symbols, traffic signs, or knitting patterns.
Let's propose a definition of programming:
DEFINITION Programming: The act of creating programs: documents written in a notation system that can be understood by other humans as well as computers.
DEFINITION Programming Language: A programming language defines the rules of notation ("syntax") that are required for it to ask the computer to do work on your behalf.
When programmers say "I prefer Ruby to JavaScript" they're saying "I like the notation system that Ruby uses for expressing problems and their solutions." It's a lot like why some people like to play one instrument over another. In your heart you might feel like a bassoonist, tuba player, or pianist — but all those instruments play a Middle-C note at the same frequency. Programming languages are similar: while their notation varies, they all ask the chips inside the computer to do the same work at the end of the day.
As such a lot of the "fashion parade" of "what's hot" in the technical scene is just fashion. The really important thing to learn is: "What kind of problems or solutions does this language make easier to express?"
One of the most essential ways of communicating, that we've all practiced thousands of times, is a conversation. "Conversation" comes from the Latin words meaning "by taking turns." In conversation, we "take turns" expressing ourselves.
A unit of "ideas" is an expression. When a listener hears an expression they interpret the words and symbols and sounds in the expression into a meaning. We call the work they're doing there evaluation. The result of evaluation for a human is "understanding." Computers show their understanding by returning the result of that evaluation. That value is called a return value.
Consider this diagram:
In the next lessons, we're going to see how Ruby acts as a "listener" in a conversation. When given an expression, Ruby will evaluate it and, like a good friend, give you a reaction about how it evaluated your expression. In the programming world, we call responses from the programming language a return value.
When expressions are evaluated, they return something
Sometimes our expressions might not make sense to Ruby, just like sometimes our conversations with friends don't quite make sense to them.
Work, you stupid machine
is not part of the valid notation of Ruby. Likewise
1 + "Cookie Monster"
is also not valid. Here we've broken the rules of the
notation system. These are known as syntax errors. Computers require us to
honor their limited vocabulary. Syntax errors are prompts from the
programming language to help steer our communication to be more precise.
Congratulations! You've finished your first lesson in thinking about programming as a conversation. As you might guess, expressions are important because they're how we "push" ideas to computers and other programmers. We're going to spend the remainder of this module learning to build expressions and understanding how Ruby will evaluate them. Make sure you've integrated the vocabulary words of "expression," "evaluation," and "return value" before moving on.