-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.rb
66 lines (54 loc) · 1.09 KB
/
blocks.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
[1,2,3,4].each do |value|
puts value
end
[1,2,3,4].each {|value| puts value}
File.open_and_process("hello.rb", "r") do |file|
while line = file.gets
puts line
end
end
# variable scope
sheep = 20
1.upto(50) do |sheep|
puts sheep
end
puts sheep
sheep = 20
name = "shaun"
1.upto(50) do |sheep|
puts sheep
name = "fred"
end
puts sheep
sheep = 20
name = "shaun"
1.upto(50) do |sheep; fred|
puts sheep
name = "fred"
end
puts sheep
# yield
def test
puts "You are in the method"
yield
puts "You are again back to the method"
yield
end
test {puts "You are in the block"}
def test_with_parameters
puts "You are in the method"
yield 10, 10
puts "You are again back to the method"
yield 100
end
test_with_parameters {|i,j| puts "You are in the block #{i} #{j}"}
# BEGIN and END
# Any code that found by Ruby in the BEGIN block is run before any other code
# Similarly any code in the END block will be run last
# If there are multiple BEGIN and ENDs then they are run in the sequence that they're found by Ruby
BEGIN {
# begin block code
}
END {
# end block code
}