-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippets.rb
156 lines (131 loc) · 2.47 KB
/
snippets.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# ruby snippets
# >> 25.percent.of 16
# => 4.0
# >> 18.percent.of 321
# => 57.78
class Percentage
attr_reader :amount
def initialize(amount)
@amount = amount
end
def of(number)
number * (amount / 100.0)
end
end
class Numeric
def percent
Percentage.new(self)
end
end
# ---------
# >> Date.today.at_some_point
# => Tue May 21 10:23:00 -0500 2008
# >> Date.today.at_some_point
# => Tue May 21 02:10:00 -0500 2008
# >> Date.today.at_some_point
# => Tue May 21 18:28:00 -0500 2008
# >> Date.today.at_some_point
# => Tue May 21 07:25:00 -0500 2008
class Date
def at_some_point
(at_midnight..tomorrow.at_midnight).to_a.rand
end
end
# ---------
module Kernel
def maybe(&block)
if block_given? && maybe
block.call
else
Kernel.rand(2).zero?
end
end
end
module Enumerable
def some
map do |element|
maybe do
element
end
end.compact
end
end
# ---------
# >> 100_000.reduce_by {|number| !number.very_large?}
# => 6250
# >> 100_000.reduce_by 20.percent {|number| !number.large?}
# => 32
# >> 100_000.reduce
# => 48
class Numeric
def reduce_by(percentage = 50.percent, &block)
block ||= Proc.new {|number| number.small?}
return self if block.call(self)
reduced_to = self
until block.call(reduced_to)
reduced_to = reduced_to.reduce_to(percentage)
end
Integer(reduced_to)
end
alias_method :reduce, :reduce_by
end
# ---------
# @person ? @person.name : nil
# vs
# @person.try(:name)
class Object
def try(method)
send method if respond_to? method
end
end
# ----------
class Percentage
def chance
Chance.new(self)
end
class Chance
attr_reader :odds, :happens
alias :happens? :happens
def initialize(percent)
@odds = percent.amount
@happens = @odds > Kernel.rand(100)
end
def of(&block)
yield if happens?
end
end
end
module Kernel
def maybe(percent = 50.percent, &block)
if block_given?
percent.chance.of &block
else
percent.chance.happens?
end
end
def probably(&block)
80.percent.chance.of &block
end
def rarely(&block)
20.percent.chance.of &block
end
end
# -----------
module Kernel
def probably
yield if (0...8).include? Kernel.rand(10)
end
def rarely
yield if (0...2).include? Kernel.rand(10)
end
end
1_000_000.times do
probably do
good
end
rarely do
evil
end
end
# Good done 799,086 of 1,000,000
# Evil done 199,662 of 1,000,000