-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-methods.rb
49 lines (31 loc) · 1.19 KB
/
3-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
# Okay, now on to our last lesson file before we start doing some fun stuff.
# This one is about methods.
# Methods encapsulate code. They make them reusable. For instance:
first_number = 10
second_number = 7
puts first_number + second_number
# The code above will do what you think it'll do. But to get it to happen again,
# I have to retype the whole thing. Instead, I could write a method...
def all_the_numbers_yo
first_number = 10
second_number = 7
first_number + second_number
end
# Now look:
puts all_the_numbers_yo
# Notice I had to define the numbers again inside the method. That's called SCOPE.
# For the most part methods cannot look outside of themselves unless you tell them to.
# Or....unless you feed them something from the outside.
def feed_me_numbers! ( x, y )
x + y
end
puts feed_me_numbers!( 100, 70 )
# Shit, that's cool. Isn't it?
# When you define a method, you can call its _arguments_ anything you'd like.
# Above, we called them x and y. They're just placeholders for something we'll pass in.
# Write a method that contains a conditional or iterator, and takes an argument.
def your_method
puts "Your method will go here."
end
# Let's call your method up:
your_method