|
1 | | -# Variables may either have a global or local "scope". |
| 1 | +# Variables may either have a global or local "scope". |
2 | 2 | # For example, variables declared within either the |
3 | 3 | # setup or draw functions may be only used in these |
4 | 4 | # functions. Global variables, variables declared outside |
5 | 5 | # of setup and draw, may be used anywhere within the program. |
6 | 6 | # If a local variable is declared with the same name as a |
7 | | -# global variable, the program will use the local variable to make |
| 7 | +# global variable, the program will use the local variable to make |
8 | 8 | # its calculations within the current scope. Variables may be localized |
9 | 9 | # within classes, functions, and iterative statements. |
10 | 10 |
|
11 | | -# Please note that there are some changes on variable scope inside blocks |
12 | | -# between Ruby versions 1.8 and 1.9. |
13 | | - |
14 | | - |
15 | 11 | def setup |
16 | 12 | background 51 |
17 | 13 | stroke 255 |
18 | 14 | no_loop |
19 | | - |
20 | | - @a = 20 # Use "@" before the name to create an instance variable ("@a"), |
| 15 | + @a = 20 # Use "@" before the name to create an instance variable ("@a"), |
21 | 16 | # which will be available anywhere inside this instance of "VariableScope". |
22 | 17 | end |
23 | 18 |
|
24 | 19 | def draw |
25 | | - # Draw a line using the instance variable "a", |
| 20 | + # Draw a line using the instance variable "a", |
26 | 21 | # as returned by its getter function below |
27 | 22 | line a, 0, a, height |
28 | | - |
29 | 23 | # Create a new variable "a" local to the block (do-end) |
30 | 24 | (50..80).step(2) do |a| |
31 | 25 | line a, 0, a, height |
32 | 26 | end |
33 | | - |
34 | 27 | # Create a new variable "a" local to the draw method |
35 | 28 | a = 100 |
36 | 29 | line a, 0, a, height |
37 | | - |
38 | 30 | # Make a call to the custom function draw_another_line |
39 | 31 | draw_another_line |
40 | | - |
41 | 32 | # Make a call to the custom function draw_yet_another_line |
42 | 33 | draw_yet_another_line |
43 | 34 | end |
44 | 35 |
|
45 | 36 | def draw_another_line |
46 | 37 | # Create a new variable "a" local to this method |
47 | 38 | a = 185 |
48 | | - |
49 | 39 | # Draw a line using the local variable "a" |
50 | 40 | line a, 0, a, height |
51 | 41 | end |
52 | 42 |
|
53 | 43 | def draw_yet_another_line |
54 | | - # Because no new local variable "a" is set, this line draws using the |
55 | | - # instance variable "a" (returned by its getter function) which was |
| 44 | + # Because no new local variable "a" is set, this line draws using the |
| 45 | + # instance variable "a" (returned by its getter function) which was |
56 | 46 | # set to the value 20 in setup. |
57 | | - line a+2, 0, a+2, height |
| 47 | + line a + 2, 0, a + 2, height |
58 | 48 | end |
59 | 49 |
|
60 | | -# This is a "getter" function, it returns the value of the instance variable "a" |
| 50 | +# This is a "getter" function, it returns the value of the instance variable "a" which is |
| 51 | +# completely pointless in ruby, use instead attr_reader see below |
61 | 52 | def a |
62 | 53 | @a |
63 | 54 | end |
|
71 | 62 |
|
72 | 63 | # Or you could just have Ruby add both at once with: |
73 | 64 | # attr_accessor :a |
74 | | - |
0 commit comments