-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_lazy_programming.rb
More file actions
121 lines (87 loc) · 2.54 KB
/
01_lazy_programming.rb
File metadata and controls
121 lines (87 loc) · 2.54 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#lambda examples
x = lambda { return "lambda example" }
raise ArgumentError.new "this should not run" unless x.call == "lambda example"
four = lambda { 4 }
five = four.call + 1
raise ArgumentError.new "this should not run" unless five == 5
three = four.call - 1
raise ArgumentError.new "this should not run" unless three == 3
twenty = four.call * 5
raise ArgumentError.new "this should not run" unless twenty == 20
#&block in method examples
def method_block(&block)
block.call
end
raise ArgumentError.new "this should not run" unless method_block { 1 + 1 } == 2
raise ArgumentError.new "this should not run" unless method_block { "foobar" } == "foobar"
x = method_block do
a_string = "this string"
a_string += " was created"
a_string += " in a block"
a_string
end
raise ArgumentError.new "this should not run" unless x == "this string was created in a block"
#class examples
class Square
attr_accessor :width, :height, :counter, :area
def initialize(w, h)
@counter = 0
@width = w
@height = h
end
def area
@area ||= calculate_area
end
def calculate_area
@counter += 1
@height * @width
end
end
square = Square.new(10, 10)
raise ArgumentError.new "this should not run" unless square.counter == 0
square.area
raise ArgumentError.new "this should not run" unless square.counter == 1
square.area
raise ArgumentError.new "this should not run" unless square.counter == 1
class Lazy < BasicObject
def initialize(&block)
@computation = block
end
def __run_method__
if @computation
@result = @computation.call
@computation = nil
end
@result
end
def method_missing(*a, &b)
__run_method__.__send__(*a, &b)
end
end
def foo
"bar"
end
bar = Lazy.new { foo }
raise ArgumentError.new "this should not run" unless bar.eql? "bar"
num = Lazy.new { 5 }
raise ArgumentError.new "this should not run" unless (num + 5) == 10
raise ArgumentError.new "this should not run" unless (num * 2) == 10
raise ArgumentError.new "this should not run" unless (num - 5) == 0
class BiggerSquare
attr_accessor :width, :height, :counter, :area
def initialize(w, h)
@counter = 0
@width = w
@height = h
@area = Lazy.new {calculate_area}
end
def calculate_area
@counter += 1
@height * @width
end
end
big_square = BiggerSquare.new(10, 10)
raise ArgumentError.new "this should not run" unless big_square.area.eql? 100
raise ArgumentError.new "this should not run" unless big_square.counter == 1
big_square.area
raise ArgumentError.new "this should not run" unless big_square.counter == 1