diff --git a/chapter1/README.md b/chapter1/README.md index 8012a2c..061204e 100644 --- a/chapter1/README.md +++ b/chapter1/README.md @@ -21,54 +21,20 @@ This curriculum is a little different. In it, you’ll form a good model of - How all the things above act to make the world a living, breathing place - How to construct sophisticated worlds with interacting entities inside -We’ll be constructing a simple Todo list. Eventually, you’ll be able to run `todo show` from the console (Terminal), and see: - -``` -Good evening! You have 2 Todos to complete: -* Wash the dog -* Wash the car - -You have completed 2 Todos: -* Learn to code -* Make a Todo list program -``` - -You’ll also be able to add todos, with `todo add Put the dog in the car`: - -``` -Todo added: Put the dog in the car -``` - -And complete todos, with `todo complete Wash the dog`: - -``` -Todo completed: Wash the dog - -You have 2 Todos left to complete: -* Wash the car -* Put the dog in the car - -You have completed 3 Todos: -* Learn to code -* Make a Todo list program -* Wash the dog -``` - Although we’re going to build your understanding of programming in a different way to normal, we’re still going to start with ‘Hello World!’. Why? Because ‘Hello World’ is a small, minimal program, and we as programmers always try to build the smallest, most minimal things we can get away with. The difference for us is that we’re actually going to introduce a World, of sorts, in this chapter. So we really are saying, ‘Hello World!’ - ## Programs are worlds -There are two things required to run a program: a machine, and some code. +There are two things required to run a Ruby program: some code, and an interpreter. -![A machine and some code, side-by-side](../images/1-1.jpg) +![An interpreter and some code, side-by-side](../images/1-1.jpg) -* **The code** is a _set of instructions to the machine for how to set up a program_. -* **The machine** is the _place in which this program lives and breathes_. Computers are machines. +* **The code** is a _set of instructions to the interpreter_. +* **The interpreter** is the _thing that executes these instructions_. A computer is a machine containing interpreters. -When I think of a program, I think of a flat world, with things happening inside it. Objects live in this world. +When I think of a program, I think of a flat world, with things happening inside it. This world lives and breathes inside a computer. Objects live in this world. For instance, let’s start with a program we all know: Microsoft Word. When we click on the ‘W’ icon to open Word, the following world comes into being inside your computer: @@ -91,7 +57,9 @@ These objects, obvious and non-obvious, can interact with one another in set way ![The Word world being interacted with: saving, moving the cursor, and exiting the program](../images/1-word-interactions.gif) -Moreover, the world is _itself_ an object: in Ruby, it's called the 'main Object' (`main`) It’s like our real-life Universe: although everything that exists does so within the Universe, the Universe is itself a thing. It’s conceivable that some higher power could ‘exit the Universe’: with pretty catastrophic results for all us objects within it. +> Each of these objects is following a set of instructions. + +Moreover, the world is _itself_ an object: in Ruby, it's called the 'main object' (`main`). The main object is like our real-life Universe: although everything that exists does so within the Universe, the Universe is itself a thing. It’s conceivable that some higher power could ‘exit the Universe’: with pretty catastrophic results for all us objects within it. Ruby is a language for writing code – setting up a program, or world – and it’s also an environment in which that world can exist. Whenever we run a Ruby program, we set up a main object, and a bunch of other objects too. For the next few chapters, we’ll be exploring these objects, who they are, and how they work, using a special kind of Ruby program called a **REPL (or ‘Read–Evaluate–Print Loop’)**. @@ -100,7 +68,11 @@ When we run a REPL program, we: 1. set up the Ruby world, and 2. we get the ability to write Ruby code to modify that world on-the-fly. -You could think (2) as being an 'interactive window' into the program world. You are given a 'prompt', which waits for you to type instructions for the Ruby world. The main Ruby prompt is called **IRB**. It looks like this: +You could think (2) as being an 'interactive window' into the program world. You are given a 'prompt', which waits for you to type instructions for the Ruby world. + +> I like to imagine that, when I'm using a REPL, I'm pretending to be an object in the object world. I can send instructions to other objects, and they'll send me answers to my instructions. + +The main Ruby prompt is called **IRB**. It looks like this: ![An IRB prompt](../images/1-irb.jpg) @@ -147,7 +119,7 @@ When I want to illustrate something but I don't want the code to be executed, I' # This line of code will not be executed. ``` -In Ruby, putting a `#` at the beginning of a line of code means it won't be executed by the computer. Lines like this are used to give extra information about the code. They're called **comments**. +> In Ruby, putting a `#` at the beginning of an instruction (a 'line of code') means it won't be executed. Lines like this are used to give extra information about the lines of code coming after them. They're called **comments**. ## Destroying worlds @@ -179,13 +151,13 @@ The REPL treats us, the programmer, as if we were 'inside' the program world. Th When we tell an object to do something, it always responds. This is the foundation of [Chapter 3](../chapter3/README.md), which goes into detail about how this 'messaging' works. -However, when we tell ruby to load a pre-written world – to run a program from a file – we don't get to live in that world alongside the objects. It's created, it does its thing, then it disappears (unless you expressly tell it to stick around). +However, when we tell our computer to load a pre-written world – to run a program from a file – we don't get to live in that world alongside the objects. It's created, it does its thing, then it disappears (unless you expressly tell it to stick around). So how can we get a view into this world while it exists – while the program's running? We can use a special command: `puts`. It stands for 'put string'. > We'll find out more about strings in [Chapter 5](../chapter5/README.md). -`puts` is really only useful to us when we run pre-written code (for example, by running `ruby hello_world.rb` like we did above). In fact, something weird goes on when we try to use it in the REPL: +`puts` is really only useful to us when we run pre-written code (for example, by running `ruby hello_world.rb` like we did above). In fact, something weird goes on when we try to use it in the REPL, or in these interactive code examples: ```eval-ruby puts 1 + 1 diff --git a/chapter10/README.md b/chapter10/README.md index 24a9a70..7bb0626 100644 --- a/chapter10/README.md +++ b/chapter10/README.md @@ -917,6 +917,8 @@ Here's a visual representation of scope: > Confused by the word 'scope'? Think of the scope on top of a sniper-rifle: when you look down it, you can only see a part of the world: the part of the world you can shoot. +One more gotcha: even though files containing Ruby code are all loaded into the program straight away, local variables cannot be read from outside of the file they're declared in. So files act as scope gates, too. + ## Using scope to understand method parameters Scope is especially helpful when it comes to understanding method parameters. For each parameter, methods will define a local variable within their scope. The name of that variable will be set to the name of the parameter: diff --git a/chapter2/README.md b/chapter2/README.md index f567e6c..12801c0 100644 --- a/chapter2/README.md +++ b/chapter2/README.md @@ -55,7 +55,7 @@ We can interact with these numbers in `irb`. Start `irb` from the console. Then, %/accordion% -> This 'enter your instruction' followed by 'send your instruction to the program world' pattern is the basis by which all coding happens. +> The pattern 'enter your instruction' followed by 'send your instruction to the program world' is the basis by which all REPL interactions happen. Let's try this again with another number: @@ -169,7 +169,7 @@ Here's an example of that in action in `irb`: 1 + 2 ``` -In the case above, here's what's happening. The computer executes the code `1 + 1` character-by-character: +In the case above, here's what's happening. The interpreter executes the code `1 + 1` character-by-character: - The program world fetches the object referenced by the name `1`. - The program world asks the object referenced by `1` if it understands the message `+`. @@ -178,6 +178,8 @@ In the case above, here's what's happening. The computer executes the code `1 + - The first object referenced by `1` adds the value of the object referenced by `2` to itself. The result is returned to the program world. - The program world returns the result to the user: `3`. +> Notice that the interpreter doesn't 'add `1` to `1`'. Instead, the first object (`1`) goes and gets another object (`1`) and then adds itself together. In Ruby, objects do all the work. The interpreter turns our code into instructions for objects to do things. + Here's the above, visually: @@ -232,11 +234,11 @@ Some objects cannot change throughout the course of the program. Integers, like `1` doesn't turn into `3`, it just stays as `1`. Forever. Or, until we turn off the program. -In Ruby, we call these never-changing objects **constants**. We use naming for assigning constants, just like variables. But, there are special rules for naming constants: they have to start with a capital letter. Conventionally, the entire name is in capital letters. +In Ruby, we give these never-changing objects a special kind of name: a **constant**. Just like variables, we assign constants using `=`. But, there are special rules for naming constants: they have to start with a capital letter. Conventionally, the entire name is in capital letters. Here's an example of assigning a name, `CONSTANT`, to an object called `object`: -``` +```ruby CONSTANT = object ``` @@ -255,7 +257,7 @@ When we start the program world, some constants come into existence automaticall %/accordion% -In order to load all the names for your numeral calculator from a file and into the REPL, the names will need to be written as constants. +In order to load all the names for your numeral calculator from a file and into the REPL, the names will need to be written as constants. (The reasons for why are tricky, but for now, remember that variables can't be read outside the file they're written in, but constants can be.) That's OK, though, as each name should be pointing to a different integer: and the integers are constant objects.