-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods.rb
74 lines (51 loc) · 963 Bytes
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# ------------------
# Methods
# ------------------
# Simple Method
def say
puts "hello"
end
say
# Brackets or no brackets
def say_with_brackets(message)
puts message
end
say_with_brackets("hello with brackets")
def say_without_brackets message
puts message
end
say_without_brackets "hello without bracket"
# Return or no return
def say_with_return message
return message
end
puts say_with_return "many happy returns"
def say_without_return message
message
end
puts say_without_return "who needs to return"
# Multiple parameters for a method
def say_lots *messages
messages.each do |message|
puts message
end
end
say_lots "dog sees cats", "dog chases cat"
# parameter defaults
def dog_says message="bark"
puts message
end
dog_says
dog_says "yelp"
# method aliases
def speak_it message
puts message
end
alias say_it speak_it
say_it "can you hear me"
# splats
# 1.9
def splat_it (a, c, *b)
p b
end
splat_it(1,2,3,4,5)