learn to program, the pragmatic programers (2005)

156 417 0
learn to program, the pragmatic programers (2005)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

[...]... simple calculator (Calculators always use floats, so if you want your computer to act just like a calculator, you should also use floats.) You type numbers using the digit keys (either at the top of your keyboard or on the numeric keypad) For decimal points, you use the period (or full-stop, normally close to the M key on the bottom row, or over on the numeric keypad) Don’t, however, type commas into your... think of them as folders They called them directories So they didn’t move from folder to folder; they changed directories But if you actually try typing change_directory desktop all day long, you barely have time to get down to your funky eight-tracks So it was shortened to just cd Report erratum 3 M AC OS X If you want to go back up a directory, you use cd : C:\Documents and Settings\chris\Desktop> cd... even try to run it The problem is that your computer can’t tell the difference between an apostrophe and a single quote (to end the string) I think the confusion is reasonable here, though: they are the same character, after all So we need a way to tell the computer, “I want an apostrophe here, inside this string.” How do we let the computer know we want to stay in the string? We have to escape the apostrophe,... variables point to them Now it’s time for them all to play nicely together We’ve seen that if we want a program to print 25 , the following does not work, because you can’t add numbers and strings together: var1 = 2 var2 = ' 5 ' puts var1 + var2 Part of the problem is that your computer doesn’t know if you were trying to get 7 (2 + 5) or if you wanted to get 25 ('2' + '5') But we’ll learn how to do both... would be nice if we could just type it in once and then hang on to it store it somewhere Well, we can, of course—it would have been insensitive to bring it up otherwise To store the string in your computer’s memory for use later in your program, you need to give the string a name Programmers often refer to this process as assignment, and they call the names variables A variable name can usually be... Before we can add these together, we need some way of getting the string version of var1 or of getting the integer version of var2 5.1 Conversions To get the string version of an object, we simply write to_ s after it: var1 = 2 var2 = ' 5 ' puts var1 .to_ s + var2 25 C ONVERSIONS Similarly, to_ i gives the integer version of an object, and to_ f gives the float version Let’s look at what these three methods... keyboards have these keys in the numeric keypad on the far-right side, but you can also use Shift 8 and / (the same key as the ? key) Let’s try to expand our calc.rb program a little Try coding the program at the top of the next page Report erratum 10 S IMPLE A RITHMETIC puts 1.0 + 2.0 puts 2.0 * 3.0 puts 5.0 - 8.0 puts 9.0 / 2.0 This is what the program returns: 3.0 6.0 -3.0 4.5 (The spaces in the program... tried to point var2 to var1, it really pointed to 8 instead (just like var1 was pointing to) Then on the eighth line, we had var1 point to the string 'eight', but since var2 was never really pointing at var1, it stays pointing at the number 8 If you like to think about these things visually, Figure 4.1, on the next page, might help Report erratum 20 Report erratum Figure 4.1: Variables point to values... converted into numbers On line 5, to_ i ignores the first thing it doesn’t understand (and the rest of the string from that point on) So the first one was converted to 5, but the others, since they started with letters, were ignored completely so the computer just picks zero Finally, we saw that our last two conversions did nothing at all, just as we would expect 5.2 Another Look at puts There’s something... this: puts 20 puts 20 .to_ s puts ' 20 ' 20 20 20 Why do these three all print the same thing? Well, the last two should, since 20 .to_ s is '20' But what about the first one, the integer 20? For that matter, what does it even mean to write the integer 20? When you write a 2 and then a 0 on a piece of paper, you are writing a string, not an integer The integer 20 is the number of fingers and toes I have; it isn’t . knife. 3. Spr ead the peanut butter on one slice of bread and the jelly on the other one. 4. Put the peanut butter and jelly away, and take care of the knife. 5. Put the slices together, put the sandwich. explanation. Step 3 says to spread the peanut butter on one slice of bread. It doesn’t say to spread it on only one side of the bread or to use the knife to do the spreading (as opposed to, say, your forehead) discovered crayons and Photoshop), so people didn’t thi nk of them as folders. They called them directories. So they didn’t move from folder to folder; they changed directories. But if you actually

Ngày đăng: 29/04/2014, 14:42

Từ khóa liên quan

Mục lục

  • Introduction

    • What Is Programming?

    • Programming Languages

    • The Art of Programming

    • Chapter 1 Getting Started

      • Windows

      • Mac OS X

      • Linux

      • Chapter 2 Numbers

        • Introduction to puts

        • Integer and Float

        • Simple Arithmetic

        • A Few Things to Try

        • Chapter 3 Letters

          • String Arithmetic

          • 12 vs. STRINGCOLOR12

          • Problems

          • Chapter 4 Variables and Assignment

          • Chapter 5 Mixing It Up

            • Conversions

            • Another Look at puts

            • The Methods gets and chomp

            • A Few Things to Try

            • Mind Your Variables

            • Chapter 6 More about Methods

              • Fancy String Methods

Tài liệu cùng người dùng

Tài liệu liên quan