Skip to content
Newer
Older
100644 118 lines (88 sloc) 3.82 KB
8768db2 @ihower Init project
ihower authored Jul 22, 2010
1 h2. Introduction
2
3 This is a small Ruby tutorial that should take no more than 20 minutes to
4 complete. It makes the assumption that you already have Ruby installed.
5 (If you don't have Ruby on your computer "download":/en/downloads/ and
6 install it before you get started.)
7
8 h2. Interactive Ruby
9
10 Ruby comes with a program that will show the results of any Ruby statements you feed it. Playing with Ruby code in interactive sessions like this is a terrific way to learn the language.
11
12 Open up IRB (which stands for Interactive Ruby).
13
14 * If you're using *Mac OS X* open up @Terminal@ and type
15 @irb@, then hit enter.
16 * If you're using *Linux*, open up a shell and type
17 @irb@ and hit enter.
18 * If you're using *Windows*, open @fxri@ from the Ruby section of your Start Menu.
19
20 <notextile><r:code>
21 irb(main):001:0>
22 </r:code></notextile>
23
24 Ok, so it's open. Now what?
25
26 Type this: @"Hello World"@
27
28 <notextile><r:code>
29 irb(main):001:0> "Hello World"
30 => "Hello World"
31 </r:code></notextile>
32
33 h2. Ruby Obeyed You!
34
35 What just happened? Did we just write the world's shortest "Hello
36 World" program? Not exactly. The second line is just IRB's way of
37 telling us the result of the last expression it evaluated. If we want
38 to print out "Hello World" we need a bit more:
39
40 <notextile><r:code>
41 irb(main):002:0> puts "Hello World"
42 Hello World
43 => nil
44 </r:code></notextile>
45
46 @puts@ is the basic command to print something out in Ruby. But then what's the
47 @=> nil@ bit? That's the result of the expression. @puts@ always
48 returns nil, which is Ruby's absolutely-positively-nothing value.
49
50 h2. Your Free Calculator is Here
51
52 Already, we have enough to use IRB as a basic calculator:
53
54 <notextile><r:code>
55 irb(main):003:0> 3+2
56 => 5
57 </r:code></notextile>
58
59 Three plus two. Easy enough. What about three _times_ two? You could
60 type it in, it's short enough, but you may also be able to go up and
61 change what you just entered. Try hitting the *up-arrow* on your
62 keyboard and see if it brings up the line with @3+2@ on it. If it
63 does, you can use the left arrow key to move just after the @+@ sign
64 and then use backspace to change it to a @*@ sign.
65
66 <notextile><r:code>
67 irb(main):004:0> 3*2
68 => 6
69 </r:code></notextile>
70
71 Next, let's try three squared:
72
73 <notextile><r:code>
74 irb(main):005:0> 3**2
75 => 9
76 </r:code></notextile>
77
78 In Ruby @**@ is the way you say "to the power of". But what if you
79 want to go the other way and find the square root of something?
80
81 <notextile><r:code>
82 irb(main):006:0> Math.sqrt(9)
83 => 3.0
84 </r:code></notextile>
85
86 Ok, wait, what was that last one? If you guessed, "it was figuring out
87 the square root of nine," you're right. But let's take a closer
88 look at things. First of all, what's @Math@?
89
90 h2. Modules, Group Code by Topic
91
92 @Math@ is a built-in module for mathematics. Modules serve two roles in
93 Ruby. This shows one role: grouping similar methods together under a familiar
94 name. @Math@ also contains methods like @sin()@ and @tan()@.
95
96 Next is a dot. What does the dot do? The dot is how you identify the
97 receiver of a message. What's the message? In this case it's
98 @sqrt(9)@, which means call the method @sqrt@, shorthand for "square
99 root" with the parameter of @9@.
100
101 The result of this method call is the value @3.0@. You might notice
102 it's not just @3@. That's because most of the time the square root of
103 a number won't be an integer, so the method always returns a
104 floating-point number.
105
106 What if we want to remember the result of some of this math? Assign
107 the result to a variable.
108
109 <notextile><r:code>
110 irb(main):007:0> a = 3 ** 2
111 => 9
112 irb(main):008:0> b = 4 ** 2
113 => 16
114 irb(main):009:0> Math.sqrt(a+b) => 5.0
115 </r:code></notextile>
116
117 As great as this is for a calculator, we’re getting away from the traditional
118 @Hello World@ message that beginning tutorials are supposed to focus on… "so let’s go back to that.":2/
Something went wrong with that request. Please try again.