-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_methods.rb
executable file
·46 lines (40 loc) · 1.39 KB
/
test_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Practicing method basics, as per Chris Pine's Learn To Program, 2nd Edition (chapter 9)
# Starting off simple - a method that says "moooo..."
def say_moo
puts "moooo..."
end
say_moo
puts ""
# Changing it up a bit...
def say_moo number_of_moos
puts "moooo..." * number_of_moos
end
say_moo(5)
puts ""
# Note that we can change method defenitions like this. However, if we were
# to try say_moo again, we'd get an error - because the method defention has
# been changed to expect a condition/parameter (in this case, number_of_moos)
# Now to try something new...
def double_this num
num_times_2 = (num * 2).to_s
puts num.to_s + " doubled is " + num_times_2
end
double_this 22
puts ""
# You'll notice that we have an addional variable (num_times_2) INSIDE the method.
# This type of variable is special - it is considered a Local Variable. That is,
# it exists, locally, within something (in this case, a method) - it still functions
# as a variable should. However, trying to access it outside the method (or "globally"),
# will get us an error...
# num_times_2
# Regarding how methods can take global variables and use them LOCALLY, without
# changing them GLOBALLY...
def slice_in_half some_number
some_number /= 2
puts "Scha-wiiing, I cut your global variable in half! See:"
puts some_number
end
some_number = 12
slice_in_half some_number
puts "But out here, globally, it's still the same:"
puts some_number