zucker 2 → 3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -0
- data/README +4 -1
- data/Rakefile +17 -7
- data/lib/zucker.rb +7 -1
- data/lib/zucker/2/all.rb +2 -2
- data/lib/zucker/2/debug.rb +3 -3
- data/lib/zucker/2/default.rb +1 -1
- data/lib/zucker/3/D.rb +19 -0
- data/lib/zucker/3/alias_for.rb +19 -0
- data/lib/zucker/3/aliases.rb +28 -0
- data/lib/zucker/3/all.rb +6 -0
- data/lib/zucker/3/array.rb +23 -0
- data/lib/zucker/3/array2proc.rb +16 -0
- data/lib/zucker/3/binding.rb +25 -0
- data/lib/zucker/3/blank.rb +26 -0
- data/lib/zucker/3/class2proc.rb +10 -0
- data/lib/zucker/3/debug.rb +6 -0
- data/lib/zucker/3/default.rb +35 -0
- data/lib/zucker/3/egonil.rb +14 -0
- data/lib/zucker/3/enumerable.rb +12 -0
- data/lib/zucker/3/hash.rb +19 -0
- data/lib/zucker/3/hash2proc.rb +14 -0
- data/lib/zucker/3/info.rb +167 -0
- data/lib/zucker/3/iterate.rb +23 -0
- data/lib/zucker/3/ivars.rb +26 -0
- data/lib/zucker/3/kernel.rb +48 -0
- data/lib/zucker/3/mcopy.rb +8 -0
- data/lib/zucker/3/mm.rb +31 -0
- data/lib/zucker/3/not.rb +17 -0
- data/lib/zucker/3/regexp2proc.rb +10 -0
- data/lib/zucker/3/sandbox.rb +19 -0
- data/lib/zucker/3/square_brackets_for.rb +20 -0
- data/lib/zucker/3/string.rb +47 -0
- data/lib/zucker/3/tap.rb +9 -0
- data/lib/zucker/3/unary.rb +22 -0
- data/lib/zucker/3/union.rb +14 -0
- data/lib/zucker/alias_for.rb +19 -0
- data/lib/zucker/array.rb +8 -2
- data/lib/zucker/debug.rb +2 -0
- data/lib/zucker/default.rb +11 -1
- data/lib/zucker/info.rb +5 -2
- data/lib/zucker/kernel.rb +41 -11
- data/lib/zucker/mcopy.rb +0 -1
- data/lib/zucker/not.rb +17 -0
- data/lib/zucker/string.rb +24 -2
- data/lib/zucker/tap.rb +9 -0
- metadata +41 -160
- data/lib/zucker/1/desc/D.yaml +0 -23
- data/lib/zucker/1/desc/aliases.yaml +0 -16
- data/lib/zucker/1/desc/array.yaml +0 -14
- data/lib/zucker/1/desc/array2proc.yaml +0 -13
- data/lib/zucker/1/desc/binding.yaml +0 -10
- data/lib/zucker/1/desc/blank.yaml +0 -12
- data/lib/zucker/1/desc/class2proc.yaml +0 -12
- data/lib/zucker/1/desc/egonil.yaml +0 -17
- data/lib/zucker/1/desc/enumerable.yaml +0 -11
- data/lib/zucker/1/desc/hash.yaml +0 -12
- data/lib/zucker/1/desc/hash2proc.yaml +0 -11
- data/lib/zucker/1/desc/iterate.yaml +0 -17
- data/lib/zucker/1/desc/ivars.yaml +0 -20
- data/lib/zucker/1/desc/kernel.yaml +0 -13
- data/lib/zucker/1/desc/mcopy.yaml +0 -12
- data/lib/zucker/1/desc/mm.yaml +0 -12
- data/lib/zucker/1/desc/regexp2proc.yaml +0 -13
- data/lib/zucker/1/desc/sandbox.yaml +0 -14
- data/lib/zucker/1/desc/square_brackets_for.yaml +0 -23
- data/lib/zucker/1/desc/string.yaml +0 -13
- data/lib/zucker/1/desc/unary.yaml +0 -15
- data/lib/zucker/1/desc/union.yaml +0 -11
- data/lib/zucker/1/doc/meta.yaml +0 -4
- data/lib/zucker/1/doc/zucker_doc.html +0 -1286
- data/lib/zucker/1/doc/zucker_doc.rb +0 -493
- data/lib/zucker/1/spec/D_spec.rb +0 -27
- data/lib/zucker/1/spec/aliases_spec.rb +0 -34
- data/lib/zucker/1/spec/array2proc_spec.rb +0 -8
- data/lib/zucker/1/spec/array_spec.rb +0 -23
- data/lib/zucker/1/spec/binding_spec.rb +0 -6
- data/lib/zucker/1/spec/blank_spec.rb +0 -20
- data/lib/zucker/1/spec/class2proc_spec.rb +0 -9
- data/lib/zucker/1/spec/egonil_spec.rb +0 -30
- data/lib/zucker/1/spec/enumerable_spec.rb +0 -8
- data/lib/zucker/1/spec/hash2proc_spec.rb +0 -12
- data/lib/zucker/1/spec/hash_spec.rb +0 -10
- data/lib/zucker/1/spec/iterate_spec.rb +0 -54
- data/lib/zucker/1/spec/ivars_spec.rb +0 -20
- data/lib/zucker/1/spec/kernel_spec.rb +0 -34
- data/lib/zucker/1/spec/mcopy_spec.rb +0 -13
- data/lib/zucker/1/spec/mm_spec.rb +0 -6
- data/lib/zucker/1/spec/regexp2proc_spec.rb +0 -9
- data/lib/zucker/1/spec/sandbox_spec.rb +0 -7
- data/lib/zucker/1/spec/square_brackets_for_spec.rb +0 -59
- data/lib/zucker/1/spec/string_spec.rb +0 -29
- data/lib/zucker/1/spec/unary_spec.rb +0 -26
- data/lib/zucker/1/spec/union_spec.rb +0 -13
- data/lib/zucker/2/desc/D.yaml +0 -23
- data/lib/zucker/2/desc/aliases.yaml +0 -16
- data/lib/zucker/2/desc/array.yaml +0 -14
- data/lib/zucker/2/desc/array2proc.yaml +0 -17
- data/lib/zucker/2/desc/binding.yaml +0 -10
- data/lib/zucker/2/desc/blank.yaml +0 -10
- data/lib/zucker/2/desc/class2proc.yaml +0 -12
- data/lib/zucker/2/desc/egonil.yaml +0 -17
- data/lib/zucker/2/desc/enumerable.yaml +0 -11
- data/lib/zucker/2/desc/hash.yaml +0 -14
- data/lib/zucker/2/desc/hash2proc.yaml +0 -11
- data/lib/zucker/2/desc/info.yaml +0 -12
- data/lib/zucker/2/desc/iterate.yaml +0 -17
- data/lib/zucker/2/desc/ivars.yaml +0 -20
- data/lib/zucker/2/desc/kernel.yaml +0 -13
- data/lib/zucker/2/desc/mcopy.yaml +0 -12
- data/lib/zucker/2/desc/mm.yaml +0 -12
- data/lib/zucker/2/desc/regexp2proc.yaml +0 -13
- data/lib/zucker/2/desc/sandbox.yaml +0 -19
- data/lib/zucker/2/desc/square_brackets_for.yaml +0 -23
- data/lib/zucker/2/desc/string.yaml +0 -13
- data/lib/zucker/2/desc/unary.yaml +0 -15
- data/lib/zucker/2/desc/union.yaml +0 -11
- data/lib/zucker/2/doc/zucker_doc.html +0 -1494
- data/lib/zucker/2/doc/zucker_doc.rb +0 -498
- data/lib/zucker/2/spec/D_spec.rb +0 -35
- data/lib/zucker/2/spec/aliases_spec.rb +0 -13
- data/lib/zucker/2/spec/array2proc_spec.rb +0 -12
- data/lib/zucker/2/spec/array_spec.rb +0 -23
- data/lib/zucker/2/spec/binding_spec.rb +0 -8
- data/lib/zucker/2/spec/blank_spec.rb +0 -20
- data/lib/zucker/2/spec/class2proc_spec.rb +0 -9
- data/lib/zucker/2/spec/egonil_spec.rb +0 -30
- data/lib/zucker/2/spec/enumerable_spec.rb +0 -8
- data/lib/zucker/2/spec/hash2proc_spec.rb +0 -12
- data/lib/zucker/2/spec/hash_spec.rb +0 -17
- data/lib/zucker/2/spec/info_spec.rb +0 -8
- data/lib/zucker/2/spec/iterate_spec.rb +0 -54
- data/lib/zucker/2/spec/ivars_spec.rb +0 -20
- data/lib/zucker/2/spec/kernel_spec.rb +0 -28
- data/lib/zucker/2/spec/mcopy_spec.rb +0 -13
- data/lib/zucker/2/spec/mm_spec.rb +0 -8
- data/lib/zucker/2/spec/regexp2proc_spec.rb +0 -9
- data/lib/zucker/2/spec/sandbox_spec.rb +0 -26
- data/lib/zucker/2/spec/spec_helper.rb +0 -18
- data/lib/zucker/2/spec/square_brackets_for_spec.rb +0 -59
- data/lib/zucker/2/spec/string_spec.rb +0 -29
- data/lib/zucker/2/spec/unary_spec.rb +0 -26
- data/lib/zucker/2/spec/union_spec.rb +0 -15
- data/lib/zucker/desc/D.yaml +0 -23
- data/lib/zucker/desc/aliases.yaml +0 -16
- data/lib/zucker/desc/array.yaml +0 -14
- data/lib/zucker/desc/array2proc.yaml +0 -17
- data/lib/zucker/desc/binding.yaml +0 -10
- data/lib/zucker/desc/blank.yaml +0 -10
- data/lib/zucker/desc/class2proc.yaml +0 -12
- data/lib/zucker/desc/egonil.yaml +0 -17
- data/lib/zucker/desc/enumerable.yaml +0 -11
- data/lib/zucker/desc/hash.yaml +0 -14
- data/lib/zucker/desc/hash2proc.yaml +0 -11
- data/lib/zucker/desc/info.yaml +0 -12
- data/lib/zucker/desc/iterate.yaml +0 -17
- data/lib/zucker/desc/ivars.yaml +0 -20
- data/lib/zucker/desc/kernel.yaml +0 -13
- data/lib/zucker/desc/mcopy.yaml +0 -12
- data/lib/zucker/desc/mm.yaml +0 -12
- data/lib/zucker/desc/regexp2proc.yaml +0 -13
- data/lib/zucker/desc/sandbox.yaml +0 -19
- data/lib/zucker/desc/square_brackets_for.yaml +0 -23
- data/lib/zucker/desc/string.yaml +0 -13
- data/lib/zucker/desc/unary.yaml +0 -15
- data/lib/zucker/desc/union.yaml +0 -11
- data/lib/zucker/doc/zucker_doc.html +0 -1494
- data/lib/zucker/doc/zucker_doc.rb +0 -498
- data/lib/zucker/edge/ideas/args.rb +0 -22
- data/lib/zucker/edge/ideas/args.yaml +0 -19
- data/lib/zucker/edge/ideas/args_spec.rb +0 -19
- data/lib/zucker/edge/ideas/clone_deep.rb +0 -21
- data/lib/zucker/edge/ideas/clone_deep_spec.rb +0 -12
- data/lib/zucker/edge/ideas/dclone.yaml +0 -12
- data/lib/zucker/edge/ideas/hash.rb +0 -18
- data/lib/zucker/edge/ideas/require_directory.rb +0 -6
- data/lib/zucker/spec/D_spec.rb +0 -35
- data/lib/zucker/spec/aliases_spec.rb +0 -13
- data/lib/zucker/spec/array2proc_spec.rb +0 -12
- data/lib/zucker/spec/array_spec.rb +0 -23
- data/lib/zucker/spec/binding_spec.rb +0 -8
- data/lib/zucker/spec/blank_spec.rb +0 -20
- data/lib/zucker/spec/class2proc_spec.rb +0 -9
- data/lib/zucker/spec/egonil_spec.rb +0 -30
- data/lib/zucker/spec/enumerable_spec.rb +0 -8
- data/lib/zucker/spec/hash2proc_spec.rb +0 -12
- data/lib/zucker/spec/hash_spec.rb +0 -17
- data/lib/zucker/spec/info_spec.rb +0 -8
- data/lib/zucker/spec/iterate_spec.rb +0 -54
- data/lib/zucker/spec/ivars_spec.rb +0 -20
- data/lib/zucker/spec/kernel_spec.rb +0 -28
- data/lib/zucker/spec/mcopy_spec.rb +0 -13
- data/lib/zucker/spec/mm_spec.rb +0 -8
- data/lib/zucker/spec/regexp2proc_spec.rb +0 -9
- data/lib/zucker/spec/sandbox_spec.rb +0 -26
- data/lib/zucker/spec/spec_helper.rb +0 -18
- data/lib/zucker/spec/square_brackets_for_spec.rb +0 -59
- data/lib/zucker/spec/string_spec.rb +0 -29
- data/lib/zucker/spec/unary_spec.rb +0 -26
- data/lib/zucker/spec/union_spec.rb +0 -15
@@ -1,1494 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
-
|
4
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
7
|
-
|
8
|
-
<title>Ruby Zucker 2</title>
|
9
|
-
<script type="text/javascript">
|
10
|
-
var show = function(snippet){
|
11
|
-
document.getElementById( snippet ).style.display = 'block'
|
12
|
-
if( document.getElementById( 'show_'+snippet ) ){
|
13
|
-
document.getElementById( 'show_'+snippet ).style.display = 'none'
|
14
|
-
}
|
15
|
-
}
|
16
|
-
</script>
|
17
|
-
<style type="text/css">
|
18
|
-
body{
|
19
|
-
background-color:#aaa;
|
20
|
-
color:#111;
|
21
|
-
font-family:sans-serif;
|
22
|
-
}
|
23
|
-
|
24
|
-
#world{
|
25
|
-
background:#fff;
|
26
|
-
}
|
27
|
-
|
28
|
-
h1, h2, h3, h4, h5, h6{
|
29
|
-
margin:0px;
|
30
|
-
padding:0px;
|
31
|
-
}
|
32
|
-
|
33
|
-
h1{
|
34
|
-
color:#222;
|
35
|
-
text-align:center;
|
36
|
-
padding:0.5em;
|
37
|
-
}
|
38
|
-
h2{
|
39
|
-
margin-left:0.5em;
|
40
|
-
margin-top:0.8em;
|
41
|
-
margin-bottom:0.5em;
|
42
|
-
}
|
43
|
-
|
44
|
-
th{
|
45
|
-
width:200px;
|
46
|
-
color:#444;
|
47
|
-
}
|
48
|
-
|
49
|
-
p{
|
50
|
-
margin:0px;
|
51
|
-
margin-bottom:0.5em;
|
52
|
-
}
|
53
|
-
|
54
|
-
p.text{
|
55
|
-
margin-left:1.5em;
|
56
|
-
margin-right:1em;
|
57
|
-
}
|
58
|
-
|
59
|
-
code{
|
60
|
-
}
|
61
|
-
|
62
|
-
.scode{
|
63
|
-
display:block;
|
64
|
-
margin:0.8em;
|
65
|
-
# margin-right:1.8em;
|
66
|
-
padding:0.5em;
|
67
|
-
border:1px solid black;
|
68
|
-
background:#eee;
|
69
|
-
}
|
70
|
-
|
71
|
-
#.cube_table{
|
72
|
-
# display:none;
|
73
|
-
#}
|
74
|
-
.cubes{
|
75
|
-
margin:0px;
|
76
|
-
margin-left:1.5em;
|
77
|
-
margin-right:1em;
|
78
|
-
}
|
79
|
-
|
80
|
-
a{
|
81
|
-
color:#111;
|
82
|
-
}
|
83
|
-
|
84
|
-
table{
|
85
|
-
margin:0.8em;
|
86
|
-
margin-top:0.2em;
|
87
|
-
padding:0.2em;
|
88
|
-
border:1px solid #111;
|
89
|
-
background:#eee;
|
90
|
-
overflow:auto;
|
91
|
-
display:block;
|
92
|
-
}
|
93
|
-
|
94
|
-
th{
|
95
|
-
text-align:left;
|
96
|
-
vertical-align:top;
|
97
|
-
padding-right:3em;
|
98
|
-
}
|
99
|
-
|
100
|
-
td{
|
101
|
-
width:100%;
|
102
|
-
}
|
103
|
-
|
104
|
-
li{
|
105
|
-
list-style:none;
|
106
|
-
}
|
107
|
-
|
108
|
-
|
109
|
-
#foot{
|
110
|
-
text-align:left;
|
111
|
-
padding:0.3em;
|
112
|
-
font-size:70%
|
113
|
-
}
|
114
|
-
#foot, #foot a{
|
115
|
-
color:#444;
|
116
|
-
}
|
117
|
-
#smile{
|
118
|
-
font-size:150%;
|
119
|
-
float:right;
|
120
|
-
}
|
121
|
-
#smile a{
|
122
|
-
text-decoration:none;
|
123
|
-
}
|
124
|
-
|
125
|
-
.small{
|
126
|
-
font-size:70%;
|
127
|
-
}
|
128
|
-
|
129
|
-
code, pre{
|
130
|
-
font-face:mono;
|
131
|
-
margin:0px;
|
132
|
-
padding:0px;
|
133
|
-
}
|
134
|
-
|
135
|
-
.source{
|
136
|
-
display:none;
|
137
|
-
width:98%;
|
138
|
-
padding: 3px 5px;
|
139
|
-
overflow: auto;
|
140
|
-
font-size: 12px;
|
141
|
-
background-color: #f8f8f8;
|
142
|
-
border:1px solid #005;
|
143
|
-
border: 1px solid silver;
|
144
|
-
font-family: 'Courier New', 'Terminal', monospace;
|
145
|
-
color: #000;
|
146
|
-
}
|
147
|
-
|
148
|
-
#/*railscasts*/
|
149
|
-
#.source .an { color:#E7BE69 } /* html attribute */
|
150
|
-
#.source .c { color:#BC9358; font-style: italic; } /* comment */
|
151
|
-
#.source .ch { color:#509E4F } /* escaped character */
|
152
|
-
#.source .cl { color:#FFF } /* class */
|
153
|
-
#.source .co { color:#FFF } /* constant */
|
154
|
-
#.source .fl { color:#A4C260 } /* float */
|
155
|
-
#.source .fu { color:#FFC56D } /* function */
|
156
|
-
#.source .gv { color:#D0CFFE } /* global variable */
|
157
|
-
#.source .i { color:#A4C260 } /* integer */
|
158
|
-
#.source .il { background:#151515 } /* inline code */
|
159
|
-
#.source .iv { color:#D0CFFE } /* instance variable */
|
160
|
-
#.source .pp { color:#E7BE69 } /* doctype */
|
161
|
-
#.source .r { color:#CB7832 } /* keyword */
|
162
|
-
#.source .rx { color:#A4C260 } /* regex */
|
163
|
-
#.source .s { color:#A4C260 } /* string */
|
164
|
-
#.source .sy { color:#6C9CBD } /* symbol */
|
165
|
-
#.source .ta { color:#E7BE69 } /* html tag */
|
166
|
-
#.source .pc { color:#6C9CBD } /* boolean */
|
167
|
-
|
168
|
-
# http://coderay.rubychan.de/
|
169
|
-
.source pre { margin: 0px; }
|
170
|
-
|
171
|
-
span.source { white-space: pre; border: 0px; padding: 2px; }
|
172
|
-
|
173
|
-
table.source { border-collapse: collapse; width: 100%; padding: 2px; }
|
174
|
-
table.source td { padding: 2px 4px; vertical-align: top; }
|
175
|
-
|
176
|
-
.source .line_numbers, .source .no {
|
177
|
-
background-color: #def;
|
178
|
-
color: gray;
|
179
|
-
text-align: right;
|
180
|
-
}
|
181
|
-
.source .line_numbers a:target, .source .no a:target { color: blue; }
|
182
|
-
.source .line_numbers .highlighted, .source .no .highlighted { color: red; }
|
183
|
-
.source .no { padding: 0px 4px; }
|
184
|
-
.source .code { width: 100%; }
|
185
|
-
.source .code pre { overflow: auto; }
|
186
|
-
|
187
|
-
.source .debug { color:white ! important; background:blue ! important; }
|
188
|
-
|
189
|
-
.source .an { color:#007 }
|
190
|
-
.source .at { color:#f08 }
|
191
|
-
.source .av { color:#700 }
|
192
|
-
.source .bi { color:#509; font-weight:bold }
|
193
|
-
.source .c { color:#888; }
|
194
|
-
.source .c .dl { color:#444; }
|
195
|
-
.source .c .ch { color:#444; }
|
196
|
-
|
197
|
-
.source .ch { color:#04D }
|
198
|
-
.source .ch .k { color:#04D }
|
199
|
-
.source .ch .dl { color:#039 }
|
200
|
-
|
201
|
-
.source .cl { color:#B06; font-weight:bold }
|
202
|
-
.source .cm { color:#A08; font-weight:bold }
|
203
|
-
.source .co { color:#036; font-weight:bold }
|
204
|
-
.source .cr { color:#0A0 }
|
205
|
-
.source .cv { color:#369 }
|
206
|
-
.source .de { color:#B0B; }
|
207
|
-
.source .df { color:#099; font-weight:bold }
|
208
|
-
.source .di { color:#088; font-weight:bold }
|
209
|
-
.source .dl { color:black }
|
210
|
-
.source .do { color:#970 }
|
211
|
-
.source .dt { color:#34b }
|
212
|
-
.source .ds { color:#D42; font-weight:bold }
|
213
|
-
.source .e { color:#666; font-weight:bold }
|
214
|
-
.source .en { color:#800; font-weight:bold }
|
215
|
-
.source .er { color:#F00; background-color:#FAA }
|
216
|
-
.source .ex { color:#C00; font-weight:bold }
|
217
|
-
.source .fl { color:#60E; font-weight:bold }
|
218
|
-
.source .fu { color:#06B; font-weight:bold }
|
219
|
-
.source .gv { color:#d70; font-weight:bold }
|
220
|
-
.source .hx { color:#058; font-weight:bold }
|
221
|
-
.source .i { color:#00D; font-weight:bold }
|
222
|
-
.source .ic { color:#B44; font-weight:bold }
|
223
|
-
|
224
|
-
.source .il { background-color: hsla(0,0%,0%,0.1); color: black }
|
225
|
-
.source .il .idl { font-weight: bold; color: #666 }
|
226
|
-
.source .idl { font-weight: bold; background-color: hsla(0,0%,0%,0.1); color: #666; }
|
227
|
-
|
228
|
-
.source .im { color:#f00; }
|
229
|
-
.source .in { color:#B2B; font-weight:bold }
|
230
|
-
.source .iv { color:#33B }
|
231
|
-
.source .la { color:#970; font-weight:bold }
|
232
|
-
.source .lv { color:#963 }
|
233
|
-
.source .ns { color:#707; font-weight:bold }
|
234
|
-
.source .oc { color:#40E; font-weight:bold }
|
235
|
-
.source .op { }
|
236
|
-
.source .pc { color:#058; font-weight:bold }
|
237
|
-
.source .pd { color:#369; font-weight:bold }
|
238
|
-
.source .pp { color:#579; }
|
239
|
-
.source .ps { color:#00C; font-weight:bold }
|
240
|
-
.source .pt { color:#074; font-weight:bold }
|
241
|
-
.source .r, .kw { color:#080; font-weight:bold }
|
242
|
-
|
243
|
-
.source .ke { color: #808; }
|
244
|
-
.source .ke .dl { color: #606; }
|
245
|
-
.source .ke .ch { color: #80f; }
|
246
|
-
.source .vl { color: #088; }
|
247
|
-
|
248
|
-
.source .rx { background-color:hsla(300,100%,50%,0.1); }
|
249
|
-
.source .rx .k { color:#808 }
|
250
|
-
.source .rx .dl { color:#404 }
|
251
|
-
.source .rx .mod { color:#C2C }
|
252
|
-
.source .rx .fu { color:#404; font-weight: bold }
|
253
|
-
|
254
|
-
.source .s { background-color:hsla(0,100%,50%,0.1); }
|
255
|
-
.source .s .k { color: #D20; }
|
256
|
-
.source .s .ch { color: #b0b; }
|
257
|
-
.source .s .dl { color: #710; }
|
258
|
-
|
259
|
-
.source .sh { background-color:hsla(120,100%,50%,0.1); }
|
260
|
-
.source .sh .k { color:#2B2 }
|
261
|
-
.source .sh .dl { color:#161 }
|
262
|
-
|
263
|
-
.source .sy { color:#A60 }
|
264
|
-
.source .sy .k { color:#A60 }
|
265
|
-
.source .sy .dl { color:#630 }
|
266
|
-
|
267
|
-
.source .ta { color:#070 }
|
268
|
-
.source .ts { color:#D70; font-weight:bold }
|
269
|
-
.source .ty { color:#339; font-weight:bold }
|
270
|
-
.source .v { color:#036 }
|
271
|
-
.source .xt { color:#444 }
|
272
|
-
|
273
|
-
.source .ins { background: hsla(120,100%,50%,0.2) }
|
274
|
-
.source .del { background: hsla(0,100%,50%,0.2) }
|
275
|
-
.source .chg { color: #aaf; background: #007; }
|
276
|
-
.source .head { color: #f8f; background: #505 }
|
277
|
-
.source .head .filename { color: white; }
|
278
|
-
|
279
|
-
.source .ins .eye { background-color: hsla(120,100%,50%,0.2) }
|
280
|
-
.source .del .eye { background-color: hsla(0,100%,50%,0.2) }
|
281
|
-
|
282
|
-
.source .ins .ins { color: #080; background:transparent; font-weight:bold }
|
283
|
-
.source .del .del { color: #800; background:transparent; font-weight:bold }
|
284
|
-
.source .chg .chg { color: #66f; }
|
285
|
-
.source .head .head { color: #f4f; }
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
</style>
|
290
|
-
</head>
|
291
|
-
<body>
|
292
|
-
<div id="world">
|
293
|
-
|
294
|
-
<h1>Ruby Zucker 2</h1>
|
295
|
-
<h2>What is it?</h2>
|
296
|
-
<p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>). It adds syntactical sugar in the form of independent, small scripts that make Ruby even more sweet. Read <a href="http://rbjl.net/32-introducing-ruby-zucker-a-new-syntactical-sugar-gem">this blog post</a> for a little introduction.</p>
|
297
|
-
|
298
|
-
<h2>Install</h2>
|
299
|
-
<p class="text">
|
300
|
-
<code class="scode">gem install zucker # might need sudo</code>
|
301
|
-
</p>
|
302
|
-
|
303
|
-
<h2>Usage / Organisation</h2>
|
304
|
-
<p class="text">The gem consists of many small snippets, called <em>cubes</em>, which are bundled in <em>packages</em>. Currently, there are two packages available: <strong>default</strong> and <strong>debug</strong>. You can use a package be requiring it in this way:
|
305
|
-
<code class="scode">require 'zucker/default'</code>
|
306
|
-
and
|
307
|
-
<code class="scode">require 'zucker/debug'</code>
|
308
|
-
|
309
|
-
Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
|
310
|
-
|
311
|
-
<code class="scode">require 'zucker/egonil'</code>
|
312
|
-
</p>
|
313
|
-
|
314
|
-
<p class="text">You can also lock your require to a specific version of Zucker by simply putting the version before the cube name in this way:
|
315
|
-
<code>require 'zucker/1/egonil'</code>. Future releases of the gem will include all previous (main) versions, so the behaviour of these directly required cubes will not change (except for critical bugs).</p>
|
316
|
-
|
317
|
-
|
318
|
-
<h2 title="require 'zucker/default'">Cubes[default]</h2>
|
319
|
-
<div class="cubes">
|
320
|
-
<h3 title="require 'zucker/aliases'">aliases</h3>
|
321
|
-
<table class="cube_table"
|
322
|
-
id="aliases_cube"
|
323
|
-
title="require 'zucker/aliases'"> <tr><th>Summary</th> <td>Some convenient aliases (and constants) for existing methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#is_an?</h5><pre class="usage source" style="display:block"><span class="co">Object</span>.is_a?</pre><h5>Enumerable#with</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Enumerable#%</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Array#**</h5><pre class="usage source" style="display:block"><span class="co">Array</span><span class="c">#product</span></pre><h5>Hash#+</h5><pre class="usage source" style="display:block"><span class="co">Hash</span><span class="c">#merge</span></pre><h5>Binding#[]</h5><pre class="usage source" style="display:block"><span class="co">Binding</span><span class="c">#eval</span></pre><h5>Infinity</h5><pre class="usage source" style="display:block"><span class="fl">1.0</span> / <span class="fl">0.0</span></pre><h5>NaN</h5><pre class="usage source" style="display:block"><span class="fl">0.0</span> / <span class="fl">0.0</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_aliases_spec">(<a href="javascript:show('aliases_spec')">show</a>)</span>
|
324
|
-
<pre class="source" id="aliases_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/aliases</span><span class="dl">'</span></span>
|
325
|
-
|
326
|
-
describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</span><span class="dl">'</span></span> <span class="r">do</span>
|
327
|
-
it <span class="s"><span class="dl">'</span><span class="k">should create these aliases</span><span class="dl">'</span></span> <span class="r">do</span>
|
328
|
-
<span class="c"># see aliases.rb for aliase list</span>
|
329
|
-
<span class="r">end</span>
|
330
|
-
|
331
|
-
it <span class="s"><span class="dl">'</span><span class="k">should define these constants</span><span class="dl">'</span></span> <span class="r">do</span>
|
332
|
-
<span class="co">Infinity</span>.finite?.should == <span class="pc">false</span>
|
333
|
-
<span class="co">NaN</span>.nan?.should == <span class="pc">true</span>
|
334
|
-
<span class="r">end</span>
|
335
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_aliases_source">(<a href="javascript:show('aliases_source')">show</a>)</span>
|
336
|
-
<pre class="source" id="aliases_source"><span class="r">alias</span> <span class="fu">is_an?</span> <span class="fu">is_a?</span>
|
337
|
-
|
338
|
-
<span class="r">module</span> <span class="cl">Enumerable</span>
|
339
|
-
<span class="r">alias</span> <span class="fu">with</span> <span class="fu">zip</span>
|
340
|
-
<span class="r">alias</span> <span class="fu">%</span> <span class="fu">zip</span>
|
341
|
-
<span class="r">end</span>
|
342
|
-
|
343
|
-
<span class="r">class</span> <span class="cl">Array</span>
|
344
|
-
<span class="r">alias</span> <span class="fu">**</span> <span class="fu">product</span>
|
345
|
-
<span class="r">end</span>
|
346
|
-
|
347
|
-
<span class="r">class</span> <span class="cl">Hash</span>
|
348
|
-
<span class="r">alias</span> <span class="fu">+</span> <span class="fu">merge</span>
|
349
|
-
<span class="r">end</span>
|
350
|
-
|
351
|
-
<span class="r">class</span> <span class="cl">Binding</span>
|
352
|
-
<span class="c">#alias [] eval</span>
|
353
|
-
<span class="r">def</span> <span class="fu">[]</span>(expr)
|
354
|
-
<span class="pc">self</span>.eval <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>expr<span class="idl">}</span></span><span class="dl">"</span></span>
|
355
|
-
<span class="r">end</span>
|
356
|
-
<span class="r">end</span>
|
357
|
-
|
358
|
-
<span class="c"># constants - who would use these in real-world code for other things?</span>
|
359
|
-
<span class="co">Infinity</span> = <span class="fl">1.0</span> / <span class="fl">0.0</span> <span class="c"># or 2*Float::MAX</span>
|
360
|
-
<span class="co">NaN</span> = <span class="fl">0.0</span> / <span class="fl">0.0</span>
|
361
|
-
|
362
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/array'">array</h3>
|
363
|
-
<table class="cube_table"
|
364
|
-
id="array_cube"
|
365
|
-
title="require 'zucker/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code>.</td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&</code> and <code>|</code>, but why does it not have <code>^</code>?</p><p><code>sum</code> is just an alias for <code>inject(:+)</code>, but it is needed pretty often..</p></td></tr><tr><th>Methods/Usage</th> <td><h5>Array#^</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>] ^ [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => [1,2,5,6]</span></pre><h5>Array#sum</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum <span class="c"># => 15</span></pre><h5>Array#chrs</h5><pre class="usage source" style="display:block">[<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs <span class="c"># => 'Hallo'</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_array_spec">(<a href="javascript:show('array_spec')">show</a>)</span>
|
366
|
-
<pre class="source" id="array_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array</span><span class="dl">'</span></span>
|
367
|
-
|
368
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
369
|
-
it <span class="s"><span class="dl">'</span><span class="k">should do an exclusive or</span><span class="dl">'</span></span> <span class="r">do</span>
|
370
|
-
a = [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>]
|
371
|
-
b = [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>]
|
372
|
-
(a^b).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">5</span>,<span class="i">6</span>]
|
373
|
-
<span class="r">end</span>
|
374
|
-
<span class="r">end</span>
|
375
|
-
|
376
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Array#sum</span><span class="dl">'</span></span> <span class="r">do</span>
|
377
|
-
it <span class="s"><span class="dl">'</span><span class="k">should sum the array</span><span class="dl">'</span></span> <span class="r">do</span>
|
378
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum.should == <span class="i">15</span>
|
379
|
-
<span class="s"><span class="dl">%w|</span><span class="k">More Ruby</span><span class="dl">|</span></span>.sum.should == <span class="s"><span class="dl">'</span><span class="k">MoreRuby</span><span class="dl">'</span></span>
|
380
|
-
<span class="r">end</span>
|
381
|
-
<span class="r">end</span>
|
382
|
-
|
383
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</span><span class="dl">'</span></span> <span class="r">do</span>
|
384
|
-
it <span class="s"><span class="dl">'</span><span class="k">should convert the array to a string, using each element as ord value for the char</span><span class="dl">'</span></span> <span class="r">do</span>
|
385
|
-
[<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs.should == <span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>
|
386
|
-
<span class="r">end</span>
|
387
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_array_source">(<a href="javascript:show('array_source')">show</a>)</span>
|
388
|
-
<pre class="source" id="array_source"><span class="r">class</span> <span class="cl">Array</span>
|
389
|
-
<span class="r">def</span> <span class="fu">^</span>(other) <span class="c"># TODO: more efficient</span>
|
390
|
-
(<span class="pc">self</span> - other) +
|
391
|
-
(other - <span class="pc">self</span>)
|
392
|
-
<span class="r">end</span>
|
393
|
-
|
394
|
-
<span class="r">def</span> <span class="fu">sum</span> <span class="c"># sry, you just need this one too often</span>
|
395
|
-
inject <span class="sy">:+</span>
|
396
|
-
<span class="r">end</span>
|
397
|
-
|
398
|
-
<span class="r">def</span> <span class="fu">chrs</span>
|
399
|
-
<span class="pc">self</span>.pack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
400
|
-
<span class="r">end</span>
|
401
|
-
<span class="r">end</span>
|
402
|
-
|
403
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/array2proc'">array2proc</h3>
|
404
|
-
<table class="cube_table"
|
405
|
-
id="array2proc_cube"
|
406
|
-
title="require 'zucker/array2proc'"> <tr><th>Summary</th> <td>Calls the method named by the first paramenter and passes the other elements as paramaters.</td></tr><tr><th>Why?</th> <td>When using <code>Symbol#to_proc</code>, you often wish to pass parameters.</td></tr><tr><th>Methods/Usage</th> <td><h5>Array#to_proc</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[<span class="sy">:*</span>, <span class="i">5</span>] <span class="c"># => [5, 10, 15, 20]</span>
|
407
|
-
<span class="c"># you can also chain them, if the first parameter is an Array</span>
|
408
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[[<span class="sy">:to_s</span>, <span class="i">2</span>],[<span class="sy">:+</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>]] <span class="c"># => ["1b", "10b", "11b", "100b"]</span>
|
409
|
-
</pre></td></tr><tr><th>Further information</th> <td>Inspired by <a href="http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby">this article</a>.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.<br/>Chaining inspired by <a href="http://github.com/eregon/Classes/blob/master/to_proc.rb">eregon</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_array2proc_spec">(<a href="javascript:show('array2proc_spec')">show</a>)</span>
|
410
|
-
<pre class="source" id="array2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array2proc</span><span class="dl">'</span></span>
|
411
|
-
|
412
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Array#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
413
|
-
it <span class="s"><span class="dl">'</span><span class="k">should call the method of the first symbol, using the remaining elements as paramaters</span><span class="dl">'</span></span> <span class="r">do</span>
|
414
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[<span class="sy">:to_s</span>, <span class="i">2</span>] ).should == [<span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">10</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">11</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">100</span><span class="dl">"</span></span>]
|
415
|
-
<span class="r">end</span>
|
416
|
-
|
417
|
-
it <span class="s"><span class="dl">"</span><span class="k">should convert each element to a proc and chain it, if the first parameter is an array</span><span class="dl">"</span></span> <span class="r">do</span>
|
418
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[[<span class="sy">:*</span>,<span class="i">2</span>],[<span class="sy">:+</span>,<span class="i">4</span>]] ).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map{|i| i*<span class="i">2</span> + <span class="i">4</span> }
|
419
|
-
<span class="r">end</span>
|
420
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_array2proc_source">(<a href="javascript:show('array2proc_source')">show</a>)</span>
|
421
|
-
<pre class="source" id="array2proc_source"><span class="r">class</span> <span class="cl">Array</span>
|
422
|
-
<span class="r">def</span> <span class="fu">to_proc</span>
|
423
|
-
<span class="co">Proc</span>.new{ |obj|
|
424
|
-
<span class="r">if</span> <span class="pc">self</span>.first.is_a? <span class="co">Array</span>
|
425
|
-
<span class="pc">self</span>.inject(obj){ |result, nested_array|
|
426
|
-
nested_array.to_proc.call result
|
427
|
-
}
|
428
|
-
<span class="r">else</span>
|
429
|
-
obj.send *<span class="pc">self</span>
|
430
|
-
<span class="r">end</span>
|
431
|
-
}
|
432
|
-
<span class="r">end</span>
|
433
|
-
<span class="r">end</span>
|
434
|
-
|
435
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/blank'">blank</h3>
|
436
|
-
<table class="cube_table"
|
437
|
-
id="blank_cube"
|
438
|
-
title="require 'zucker/blank'"> <tr><th>Summary</th> <td>Does pretty the same as in ActiveSupport (Every object can be asked if it is blank).</td></tr><tr><th>Why?</th> <td>It's too familiar ;)</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#blank?</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">an object</span><span class="dl">'</span></span>.blank? <span class="c"># => false</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_blank_spec">(<a href="javascript:show('blank_spec')">show</a>)</span>
|
439
|
-
<pre class="source" id="blank_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/blank</span><span class="dl">'</span></span>
|
440
|
-
|
441
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?</span><span class="dl">'</span></span> <span class="r">do</span>
|
442
|
-
it <span class="s"><span class="dl">'</span><span class="k">should be blank for blank values</span><span class="dl">'</span></span> <span class="r">do</span>
|
443
|
-
blank_values = [ <span class="pc">nil</span>, <span class="pc">false</span>, <span class="s"><span class="dl">'</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span>, <span class="s"><span class="dl">"</span><span class="k"> </span><span class="ch">\n</span><span class="ch">\t</span><span class="k"> </span><span class="ch">\r</span><span class="k"> </span><span class="dl">"</span></span>, [], {}, <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> ]
|
444
|
-
|
445
|
-
blank_values.each{ |blank|
|
446
|
-
blank.blank?.should == <span class="pc">true</span>
|
447
|
-
}
|
448
|
-
<span class="r">end</span>
|
449
|
-
|
450
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not be blank for non blank values</span><span class="dl">'</span></span> <span class="r">do</span>
|
451
|
-
present_values = [ <span class="co">Object</span>.new, <span class="pc">true</span>, <span class="i">0</span>, <span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, [<span class="pc">nil</span>], { <span class="pc">nil</span> => <span class="pc">nil</span> } ]
|
452
|
-
|
453
|
-
present_values.each{ |present|
|
454
|
-
present.blank?.should == <span class="pc">false</span>
|
455
|
-
}
|
456
|
-
<span class="r">end</span>
|
457
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_blank_source">(<a href="javascript:show('blank_source')">show</a>)</span>
|
458
|
-
<pre class="source" id="blank_source"><span class="r">class</span> <span class="cl">Object</span>
|
459
|
-
<span class="r">def</span> <span class="fu">blank?</span>
|
460
|
-
<span class="r">if</span> respond_to? <span class="sy">:empty?</span> <span class="r">then</span> empty? <span class="r">else</span> !<span class="pc">self</span> <span class="r">end</span>
|
461
|
-
<span class="r">end</span>
|
462
|
-
|
463
|
-
<span class="r">def</span> <span class="fu">present?</span>
|
464
|
-
!blank?
|
465
|
-
<span class="r">end</span>
|
466
|
-
<span class="r">end</span>
|
467
|
-
|
468
|
-
|
469
|
-
{ <span class="c"># what to do # for which classes</span>
|
470
|
-
lambda{ <span class="pc">true</span> } => [<span class="co">FalseClass</span>, <span class="co">NilClass</span>],
|
471
|
-
lambda{ <span class="pc">false</span> } => [<span class="co">TrueClass</span>, <span class="co">Numeric</span>],
|
472
|
-
lambda{ empty? } => [<span class="co">Array</span>, <span class="co">Hash</span>],
|
473
|
-
lambda{ <span class="pc">self</span> !~ <span class="rx"><span class="dl">/</span><span class="ch">\S</span><span class="dl">/</span></span> } => [<span class="co">String</span>],
|
474
|
-
lambda{ <span class="pc">self</span> == <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> } => [<span class="co">Regexp</span>],
|
475
|
-
|
476
|
-
}.each{ |action, klass_array|
|
477
|
-
klass_array.each{ |klass|
|
478
|
-
klass.send <span class="sy">:define_method</span>, <span class="sy">:blank?</span>, &action
|
479
|
-
}
|
480
|
-
}
|
481
|
-
|
482
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/class2proc'">class2proc</h3>
|
483
|
-
<table class="cube_table"
|
484
|
-
id="class2proc_cube"
|
485
|
-
title="require 'zucker/class2proc'"> <tr><th>Summary</th> <td>Creates a new instance of the class.</td></tr><tr><th>Methods/Usage</th> <td><h5>Class#to_proc</h5><pre class="usage source" style="display:block">[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&<span class="co">Set</span>) <span class="c"># => [ Set[1,2], Set[5,6,7,3] ]</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by Ruby Facets.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_class2proc_spec">(<a href="javascript:show('class2proc_spec')">show</a>)</span>
|
486
|
-
<pre class="source" id="class2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/class2proc</span><span class="dl">'</span></span>
|
487
|
-
require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
|
488
|
-
|
489
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Class#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
490
|
-
it <span class="s"><span class="dl">'</span><span class="k">should create new instances of the class</span><span class="dl">'</span></span> <span class="r">do</span>
|
491
|
-
[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&<span class="co">Set</span>).should == [ <span class="co">Set</span>[<span class="i">1</span>,<span class="i">2</span>], <span class="co">Set</span>[<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ]
|
492
|
-
<span class="r">end</span>
|
493
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_class2proc_source">(<a href="javascript:show('class2proc_source')">show</a>)</span>
|
494
|
-
<pre class="source" id="class2proc_source"><span class="r">class</span> <span class="cl">Class</span>
|
495
|
-
<span class="r">def</span> <span class="fu">to_proc</span>
|
496
|
-
<span class="co">Proc</span>.new <span class="r">do</span> |*args|
|
497
|
-
<span class="pc">self</span>.new *args
|
498
|
-
<span class="r">end</span>
|
499
|
-
<span class="r">end</span>
|
500
|
-
<span class="r">end</span>
|
501
|
-
|
502
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/egonil'">egonil</h3>
|
503
|
-
<table class="cube_table"
|
504
|
-
id="egonil_cube"
|
505
|
-
title="require 'zucker/egonil'"> <tr><th>Summary</th> <td>Creates a block, where <code>nil</code> does not raise <code>NoMethodError</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>egonil, nn</h5><pre class="usage source" style="display:block">egonil <span class="r">do</span>
|
506
|
-
<span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist
|
507
|
-
<span class="r">end</span> <span class="c"># => nil</span>
|
508
|
-
|
509
|
-
nn(<span class="i">5</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
510
|
-
<span class="c"># => 5</span>
|
511
|
-
</pre></td></tr><tr><th>Further information</th> <td>See <a href="http://rbjl.net/26-the-28-bytes-of-ruby-joy">this post</a> for more information and discussion.</td></tr><tr><th>Specification</th> <td> <span id="show_egonil_spec">(<a href="javascript:show('egonil_spec')">show</a>)</span>
|
512
|
-
<pre class="source" id="egonil_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/egonil</span><span class="dl">'</span></span>
|
513
|
-
|
514
|
-
describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span> <span class="r">do</span>
|
515
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not raise nil exceptions in the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
516
|
-
proc <span class="r">do</span>
|
517
|
-
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
518
|
-
<span class="r">end</span>.should_not raise_exception
|
519
|
-
<span class="r">end</span>
|
520
|
-
|
521
|
-
it <span class="s"><span class="dl">'</span><span class="k">should return the nil_value if given</span><span class="dl">'</span></span> <span class="r">do</span>
|
522
|
-
egonil(<span class="i">9</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }.should == <span class="i">9</span>
|
523
|
-
<span class="r">end</span>
|
524
|
-
|
525
|
-
it <span class="s"><span class="dl">'</span><span class="k">should restore default behaviour after the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
526
|
-
proc <span class="r">do</span>
|
527
|
-
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
528
|
-
<span class="r">end</span>.should_not raise_exception
|
529
|
-
|
530
|
-
proc <span class="r">do</span>
|
531
|
-
<span class="pc">nil</span>.a_method
|
532
|
-
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
533
|
-
<span class="r">end</span>
|
534
|
-
|
535
|
-
it <span class="s"><span class="dl">'</span><span class="k">raise NoMethodError for non-nil objects</span><span class="dl">'</span></span> <span class="r">do</span>
|
536
|
-
proc <span class="r">do</span>
|
537
|
-
egonil{ <span class="i">5</span>.a_method }
|
538
|
-
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
539
|
-
<span class="r">end</span>
|
540
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_egonil_source">(<a href="javascript:show('egonil_source')">show</a>)</span>
|
541
|
-
<pre class="source" id="egonil_source"><span class="r">def</span> <span class="fu">egonil</span>(nil_value = <span class="pc">nil</span>)
|
542
|
-
<span class="r">yield</span>
|
543
|
-
<span class="r">rescue</span> <span class="co">NoMethodError</span> => e
|
544
|
-
<span class="r">if</span> e.message =~ <span class="rx"><span class="dl">/</span><span class="k">NilClass$</span><span class="dl">/</span></span>
|
545
|
-
nil_value
|
546
|
-
<span class="r">else</span>
|
547
|
-
raise <span class="co">NoMethodError</span>
|
548
|
-
<span class="r">end</span>
|
549
|
-
<span class="r">end</span>
|
550
|
-
|
551
|
-
<span class="r">alias</span> <span class="fu">nn</span> <span class="fu">egonil</span>
|
552
|
-
|
553
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/enumerable'">enumerable</h3>
|
554
|
-
<table class="cube_table"
|
555
|
-
id="enumerable_cube"
|
556
|
-
title="require 'zucker/enumerable'"> <tr><th>Summary</th> <td><code>Enumerable</code> extensions.</td></tr><tr><th>Methods/Usage</th> <td><h5>Enumerable#mash</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] } <span class="c"># => {1=>'1',2=>'2',3=>'3'}</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by Ruby Facets' <code>mash</code>.</td></tr><tr><th>Specification</th> <td> <span id="show_enumerable_spec">(<a href="javascript:show('enumerable_spec')">show</a>)</span>
|
557
|
-
<pre class="source" id="enumerable_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/enumerable</span><span class="dl">'</span></span>
|
558
|
-
|
559
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mash</span><span class="dl">'</span></span> <span class="r">do</span>
|
560
|
-
it <span class="s"><span class="dl">'</span><span class="k">should "map" a hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
561
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] }.should == {<span class="i">1</span>=><span class="s"><span class="dl">'</span><span class="k">1</span><span class="dl">'</span></span>,<span class="i">2</span>=><span class="s"><span class="dl">'</span><span class="k">2</span><span class="dl">'</span></span>,<span class="i">3</span>=><span class="s"><span class="dl">'</span><span class="k">3</span><span class="dl">'</span></span>,}
|
562
|
-
<span class="r">end</span>
|
563
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_enumerable_source">(<a href="javascript:show('enumerable_source')">show</a>)</span>
|
564
|
-
<pre class="source" id="enumerable_source"><span class="r">module</span> <span class="cl">Enumerable</span>
|
565
|
-
<span class="r">def</span> <span class="fu">mash</span>
|
566
|
-
ret = {}
|
567
|
-
each{ |kv|
|
568
|
-
ret.store *( <span class="r">yield</span>(kv)[<span class="i">0</span>,<span class="i">2</span>] )
|
569
|
-
}
|
570
|
-
ret
|
571
|
-
<span class="r">end</span>
|
572
|
-
<span class="r">end</span>
|
573
|
-
|
574
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/hash'">hash</h3>
|
575
|
-
<table class="cube_table"
|
576
|
-
id="hash_cube"
|
577
|
-
title="require 'zucker/hash'"> <tr><th>Summary</th> <td>Some sugar for dealing with hashs.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash.zip</h5><pre class="usage source" style="display:block"><span class="co">Hash</span>.zip [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => {1 => 4, 2 => 5, 3 => 6}</span></pre><h5>Hash#<<</h5><pre class="usage source" style="display:block">{<span class="i">1</span> => <span class="i">2</span>} << [<span class="i">3</span>, <span class="i">4</span>] <span class="c"># => { 1 => 2, 3 => 4 }</span>
|
578
|
-
{<span class="i">1</span> => <span class="i">2</span>} << { <span class="i">5</span>=><span class="i">6</span> } <span class="c"># => { 1 => 2, 5 => 6 }</span>
|
579
|
-
</pre></td></tr><tr><th>Further information</th> <td>Some of the operators are inspired by Ruby Facets.</td></tr><tr><th>Specification</th> <td> <span id="show_hash_spec">(<a href="javascript:show('hash_spec')">show</a>)</span>
|
580
|
-
<pre class="source" id="hash_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash</span><span class="dl">'</span></span>
|
581
|
-
|
582
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Hash.zip</span><span class="dl">'</span></span> <span class="r">do</span>
|
583
|
-
it <span class="s"><span class="dl">'</span><span class="k">should zip together both given enumerables and take them as key=>values for a new hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
584
|
-
<span class="co">Hash</span>.zip( [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] ).should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
585
|
-
<span class="r">end</span>
|
586
|
-
<span class="r">end</span>
|
587
|
-
|
588
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Hash#<<</span><span class="dl">'</span></span> <span class="r">do</span>
|
589
|
-
it <span class="s"><span class="dl">'</span><span class="k">should append new elements to the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
590
|
-
a = { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
591
|
-
a << { <span class="i">4</span>=><span class="i">7</span> }
|
592
|
-
a << [<span class="i">5</span>, <span class="i">8</span>]
|
593
|
-
a.should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span>, <span class="i">4</span>=><span class="i">7</span>, <span class="i">5</span>=><span class="i">8</span> }
|
594
|
-
<span class="r">end</span>
|
595
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash_source">(<a href="javascript:show('hash_source')">show</a>)</span>
|
596
|
-
<pre class="source" id="hash_source"><span class="r">class</span> <span class="cl">Hash</span>
|
597
|
-
<span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
|
598
|
-
<span class="co">Hash</span>[ *keys.zip( values ).flatten ]
|
599
|
-
<span class="r">end</span>
|
600
|
-
|
601
|
-
<span class="r">def</span> <span class="fu"><<</span>(other)
|
602
|
-
<span class="r">case</span>
|
603
|
-
<span class="r">when</span> other.is_a?(<span class="co">Hash</span>)
|
604
|
-
merge! other
|
605
|
-
<span class="r">when</span> other.is_a?(<span class="co">Enumerable</span>) || other.respond_to?(<span class="sy">:to_splat</span>)
|
606
|
-
merge! <span class="co">Hash</span>[*other]
|
607
|
-
<span class="r">else</span>
|
608
|
-
raise <span class="co">TypeError</span>, <span class="s"><span class="dl">'</span><span class="k">can only append other Hashs and Enumerables (or Classes that implement to_splat)</span><span class="dl">'</span></span>
|
609
|
-
<span class="r">end</span>
|
610
|
-
<span class="r">end</span>
|
611
|
-
<span class="r">end</span>
|
612
|
-
|
613
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/hash2proc'">hash2proc</h3>
|
614
|
-
<table class="cube_table"
|
615
|
-
id="hash2proc_cube"
|
616
|
-
title="require 'zucker/hash2proc'"> <tr><th>Summary</th> <td>Use a hash to apply procs to specific objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash#to_proc</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&{
|
617
|
-
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>},
|
618
|
-
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
619
|
-
}) <span class="c"># => [1, 1002, 3, '4']</span></pre></td></tr><tr><th>Further information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_hash2proc_spec">(<a href="javascript:show('hash2proc_spec')">show</a>)</span>
|
620
|
-
<pre class="source" id="hash2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash2proc</span><span class="dl">'</span></span>
|
621
|
-
|
622
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
623
|
-
it <span class="s"><span class="dl">'</span><span class="k">should run the proc given in the value for a key in the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
624
|
-
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&{
|
625
|
-
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
626
|
-
<span class="c"># 3 => [:to_s, 2] # "11" => if array2proc is used</span>
|
627
|
-
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>}
|
628
|
-
}).should == [<span class="i">1</span>, <span class="i">1002</span>, <span class="i">3</span>, <span class="s"><span class="dl">"</span><span class="k">4</span><span class="dl">"</span></span>]
|
629
|
-
<span class="r">end</span>
|
630
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash2proc_source">(<a href="javascript:show('hash2proc_source')">show</a>)</span>
|
631
|
-
<pre class="source" id="hash2proc_source"><span class="r">class</span> <span class="cl">Hash</span>
|
632
|
-
<span class="r">def</span> <span class="fu">to_proc</span>
|
633
|
-
<span class="co">Proc</span>.new{ |obj|
|
634
|
-
<span class="r">if</span> <span class="pc">self</span>.member? obj
|
635
|
-
<span class="pc">self</span>[obj].to_proc.call obj
|
636
|
-
<span class="r">else</span>
|
637
|
-
obj
|
638
|
-
<span class="r">end</span>
|
639
|
-
}
|
640
|
-
<span class="r">end</span>
|
641
|
-
<span class="r">end</span>
|
642
|
-
|
643
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/info'">info</h3>
|
644
|
-
<table class="cube_table"
|
645
|
-
id="info_cube"
|
646
|
-
title="require 'zucker/info'"> <tr><th>Summary</th> <td>Access environment information with the <code>Info</code> module.</td></tr><tr><th>Why?</th> <td>You don't need to remember in which global variable, constant or special method the information you are searching for is hidden.</td></tr><tr><th>Methods/Usage</th> <td><h5>Info</h5><pre class="usage source" style="display:block"><span class="c"># see the source file for the list of accessors</span>
|
647
|
-
<span class="c"># you could also add them to the global namespace with: include Info</span>
|
648
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_info_spec">(<a href="javascript:show('info_spec')">show</a>)</span>
|
649
|
-
<pre class="source" id="info_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/info</span><span class="dl">'</span></span>
|
650
|
-
|
651
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Info</span><span class="dl">'</span></span> <span class="r">do</span>
|
652
|
-
it <span class="s"><span class="dl">'</span><span class="k">should define accessors for global variables, constants and some special methods/keywords</span><span class="dl">'</span></span> <span class="r">do</span>
|
653
|
-
<span class="c"># see sourcefile for the list</span>
|
654
|
-
<span class="r">end</span>
|
655
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_info_source">(<a href="javascript:show('info_source')">show</a>)</span>
|
656
|
-
<pre class="source" id="info_source"><span class="r">module</span> <span class="cl">Info</span>
|
657
|
-
<span class="r">class</span> << <span class="cl">Info</span>
|
658
|
-
<span class="c"># hash like access</span>
|
659
|
-
<span class="r">def</span> <span class="fu">[]</span>(what)
|
660
|
-
send what
|
661
|
-
<span class="r">end</span>
|
662
|
-
|
663
|
-
<span class="c"># TODO: maybe add a descriptions method</span>
|
664
|
-
<span class="r">end</span>
|
665
|
-
|
666
|
-
module_function
|
667
|
-
|
668
|
-
<span class="c"># current script file</span>
|
669
|
-
<span class="r">def</span> <span class="fu">current_file</span>
|
670
|
-
<span class="pc">__FILE__</span>
|
671
|
-
<span class="r">end</span>
|
672
|
-
|
673
|
-
<span class="r">def</span> <span class="fu">current_file_directory</span>
|
674
|
-
<span class="co">File</span>.dirname(<span class="pc">__FILE__</span>)
|
675
|
-
<span class="r">end</span>
|
676
|
-
|
677
|
-
<span class="c"># input</span>
|
678
|
-
<span class="r">def</span> <span class="fu">last_input_file</span>
|
679
|
-
<span class="gv">$FILENAME</span>
|
680
|
-
<span class="r">end</span>
|
681
|
-
|
682
|
-
<span class="r">def</span> <span class="fu">last_input_line_number</span>
|
683
|
-
<span class="gv">$.</span>
|
684
|
-
<span class="r">end</span>
|
685
|
-
|
686
|
-
<span class="r">def</span> <span class="fu">last_input</span>
|
687
|
-
<span class="gv">$_</span>
|
688
|
-
<span class="r">end</span>
|
689
|
-
|
690
|
-
<span class="c"># program</span>
|
691
|
-
<span class="r">def</span> <span class="fu">program_name</span>
|
692
|
-
<span class="gv">$0</span>
|
693
|
-
<span class="r">end</span>
|
694
|
-
|
695
|
-
<span class="r">def</span> <span class="fu">program_arguments</span>
|
696
|
-
<span class="gv">$:</span>
|
697
|
-
<span class="r">end</span>
|
698
|
-
|
699
|
-
<span class="r">def</span> <span class="fu">loaded_programs</span>
|
700
|
-
<span class="gv">$"</span>
|
701
|
-
<span class="r">end</span>
|
702
|
-
|
703
|
-
<span class="r">def</span> <span class="fu">program_data</span>
|
704
|
-
::<span class="co">DATA</span>
|
705
|
-
<span class="r">end</span>
|
706
|
-
|
707
|
-
<span class="r">def</span> <span class="fu">child_program_status</span>
|
708
|
-
<span class="gv">$CHILD_STATUS</span>
|
709
|
-
<span class="r">end</span>
|
710
|
-
|
711
|
-
<span class="c"># system info</span>
|
712
|
-
<span class="r">def</span> <span class="fu">environment</span>
|
713
|
-
::<span class="co">ENV</span>
|
714
|
-
<span class="r">end</span>
|
715
|
-
<span class="r">alias</span> <span class="fu">env</span> <span class="fu">environment</span>
|
716
|
-
|
717
|
-
<span class="r">def</span> <span class="fu">working_directory</span>
|
718
|
-
<span class="co">Dir</span>.pwd
|
719
|
-
<span class="r">end</span>
|
720
|
-
|
721
|
-
<span class="r">def</span> <span class="fu">platform</span>
|
722
|
-
::<span class="co">RUBY_PLATFORM</span>
|
723
|
-
<span class="r">end</span>
|
724
|
-
|
725
|
-
<span class="r">def</span> <span class="fu">process_id</span>
|
726
|
-
<span class="gv">$$</span>
|
727
|
-
<span class="r">end</span>
|
728
|
-
|
729
|
-
<span class="r">def</span> <span class="fu">load_path</span>
|
730
|
-
<span class="gv">$:</span>
|
731
|
-
<span class="r">end</span>
|
732
|
-
|
733
|
-
<span class="c"># current</span>
|
734
|
-
<span class="r">def</span> <span class="fu">current_line</span>
|
735
|
-
<span class="pc">__LINE__</span>
|
736
|
-
<span class="r">end</span>
|
737
|
-
|
738
|
-
<span class="r">def</span> <span class="fu">current_method</span> <span class="c"># 1.9</span>
|
739
|
-
__method__
|
740
|
-
<span class="r">end</span>
|
741
|
-
|
742
|
-
<span class="r">def</span> <span class="fu">current_callstack</span>
|
743
|
-
caller
|
744
|
-
<span class="r">end</span>
|
745
|
-
|
746
|
-
<span class="c"># dealing with strings</span>
|
747
|
-
<span class="r">def</span> <span class="fu">gets_separator</span>
|
748
|
-
<span class="gv">$/</span>
|
749
|
-
<span class="r">end</span>
|
750
|
-
|
751
|
-
<span class="r">def</span> <span class="fu">join_separator</span>
|
752
|
-
<span class="gv">$,</span>
|
753
|
-
<span class="r">end</span>
|
754
|
-
|
755
|
-
<span class="r">def</span> <span class="fu">print_separator</span>
|
756
|
-
<span class="gv">$,</span>
|
757
|
-
<span class="r">end</span>
|
758
|
-
|
759
|
-
<span class="r">def</span> <span class="fu">split_separator</span>
|
760
|
-
<span class="gv">$;</span>
|
761
|
-
<span class="r">end</span>
|
762
|
-
|
763
|
-
<span class="c"># misc</span>
|
764
|
-
<span class="r">def</span> <span class="fu">security_level</span>
|
765
|
-
<span class="gv">$SAFE</span>
|
766
|
-
<span class="r">end</span>
|
767
|
-
|
768
|
-
<span class="r">def</span> <span class="fu">warnings_activated?</span>
|
769
|
-
<span class="gv">$VERBOSE</span>
|
770
|
-
<span class="r">end</span>
|
771
|
-
|
772
|
-
<span class="r">def</span> <span class="fu">debug_activated?</span>
|
773
|
-
<span class="gv">$DEBUG</span>
|
774
|
-
<span class="r">end</span>
|
775
|
-
|
776
|
-
<span class="r">def</span> <span class="fu">last_exception</span>
|
777
|
-
<span class="gv">$!</span>
|
778
|
-
<span class="r">end</span>
|
779
|
-
|
780
|
-
<span class="c"># defined objects</span>
|
781
|
-
<span class="r">def</span> <span class="fu">global_variables</span>
|
782
|
-
global_variables
|
783
|
-
<span class="r">end</span>
|
784
|
-
|
785
|
-
<span class="r">def</span> <span class="fu">global_constants</span>
|
786
|
-
<span class="co">Object</span>.constants
|
787
|
-
<span class="r">end</span>
|
788
|
-
|
789
|
-
<span class="c"># encoding (1.9)</span>
|
790
|
-
<span class="r">def</span> <span class="fu">source_encoding</span>
|
791
|
-
__ENCODING__
|
792
|
-
<span class="r">end</span>
|
793
|
-
|
794
|
-
<span class="r">def</span> <span class="fu">external_encoding</span>
|
795
|
-
<span class="co">Encoding</span>.default_external
|
796
|
-
<span class="r">end</span>
|
797
|
-
|
798
|
-
<span class="r">def</span> <span class="fu">internal_encoding</span>
|
799
|
-
<span class="co">Encoding</span>.default_internal
|
800
|
-
<span class="r">end</span>
|
801
|
-
|
802
|
-
<span class="c"># ruby version info</span>
|
803
|
-
<span class="r">def</span> <span class="fu">ruby_version</span>
|
804
|
-
::<span class="co">RUBY_VERSION</span>
|
805
|
-
<span class="r">end</span>
|
806
|
-
|
807
|
-
<span class="r">def</span> <span class="fu">ruby_patchlevel</span>
|
808
|
-
::<span class="co">RUBY_PATCHLEVEL</span>
|
809
|
-
<span class="r">end</span>
|
810
|
-
|
811
|
-
<span class="r">def</span> <span class="fu">ruby_description</span>
|
812
|
-
::<span class="co">RUBY_DESCRIPTION</span>
|
813
|
-
<span class="r">end</span>
|
814
|
-
|
815
|
-
<span class="r">def</span> <span class="fu">ruby_release_date</span>
|
816
|
-
::<span class="co">RUBY_RELEASE_DATE</span>
|
817
|
-
<span class="r">end</span>
|
818
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (not all information)</td></tr></table> <h3 title="require 'zucker/iterate'">iterate</h3>
|
819
|
-
<table class="cube_table"
|
820
|
-
id="iterate_cube"
|
821
|
-
title="require 'zucker/iterate'"> <tr><th>Summary</th> <td>Iterate over one or more collections.</td></tr><tr><th>Why?</th> <td>It's like <code>.each</code> with two differences: It feels more like a control structure and you can easily iterate over multiple objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>iterate</h5><pre class="usage source" style="display:block">iterate [<span class="i">1</span>,<span class="i">2</span>], [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>] <span class="r">do</span> |e,f|
|
822
|
-
puts <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="k">,</span><span class="il"><span class="idl">#{</span>f<span class="idl">}</span></span><span class="dl">"</span></span>
|
823
|
-
<span class="r">end</span>
|
824
|
-
<span class="c"># outputs</span>
|
825
|
-
<span class="c"># 1,3</span>
|
826
|
-
<span class="c"># 2,4</span>
|
827
|
-
<span class="c"># ,5</span>
|
828
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_spec">(<a href="javascript:show('iterate_spec')">show</a>)</span>
|
829
|
-
<pre class="source" id="iterate_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/iterate</span><span class="dl">'</span></span>
|
830
|
-
|
831
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
|
832
|
-
let <span class="sy">:a</span> <span class="r">do</span> [<span class="i">1</span>, <span class="i">2</span>, <span class="i">3</span>] <span class="r">end</span>
|
833
|
-
let <span class="sy">:b</span> <span class="r">do</span> <span class="s"><span class="dl">%w|</span><span class="k">a b c d</span><span class="dl">|</span></span> <span class="r">end</span>
|
834
|
-
let <span class="sy">:res</span> <span class="r">do</span> <span class="co">Hash</span>.new {[]} <span class="r">end</span>
|
835
|
-
|
836
|
-
it <span class="s"><span class="dl">'</span><span class="k">should behave like Enumerable#each for a single argument</span><span class="dl">'</span></span> <span class="r">do</span>
|
837
|
-
iterate a <span class="r">do</span> |ele|
|
838
|
-
res[<span class="sy">:iter</span>] << ele
|
839
|
-
<span class="r">end</span>
|
840
|
-
|
841
|
-
a.each <span class="r">do</span> |ele|
|
842
|
-
res[<span class="sy">:each</span>] << ele
|
843
|
-
<span class="r">end</span>
|
844
|
-
|
845
|
-
res[<span class="sy">:iter</span>].should == res[<span class="sy">:each</span>]
|
846
|
-
<span class="r">end</span>
|
847
|
-
|
848
|
-
it <span class="s"><span class="dl">'</span><span class="k">should pass the right params to the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
849
|
-
res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
|
850
|
-
res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
|
851
|
-
res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
|
852
|
-
|
853
|
-
|
854
|
-
iterate a, b <span class="r">do</span> |e,f|
|
855
|
-
res[<span class="sy">:iter_a_b</span>] << [e, f]
|
856
|
-
<span class="c"># p res[:iter_a_b], e, f</span>
|
857
|
-
<span class="r">end</span>
|
858
|
-
|
859
|
-
res[<span class="sy">:iter_a_b</span>].should == [
|
860
|
-
[<span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>],
|
861
|
-
[<span class="i">2</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>],
|
862
|
-
[<span class="i">3</span>, <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>],
|
863
|
-
[<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>],
|
864
|
-
]
|
865
|
-
|
866
|
-
iterate b, a <span class="r">do</span> |e,f|
|
867
|
-
res[<span class="sy">:iter_b_a</span>] << [e, f]
|
868
|
-
<span class="r">end</span>
|
869
|
-
|
870
|
-
res[<span class="sy">:iter_b_a</span>].should == [
|
871
|
-
[<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, <span class="i">1</span>],
|
872
|
-
[<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>, <span class="i">2</span>],
|
873
|
-
[<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>, <span class="i">3</span>],
|
874
|
-
[<span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>, <span class="pc">nil</span>],
|
875
|
-
]
|
876
|
-
|
877
|
-
<span class="r">end</span>
|
878
|
-
|
879
|
-
it <span class="s"><span class="dl">'</span><span class="k">should return enumerators if no block is applied</span><span class="dl">'</span></span> <span class="r">do</span>
|
880
|
-
<span class="r">end</span>
|
881
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_iterate_source">(<a href="javascript:show('iterate_source')">show</a>)</span>
|
882
|
-
<pre class="source" id="iterate_source"><span class="r">def</span> <span class="fu">iterate</span>(*params)
|
883
|
-
<span class="c"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
|
884
|
-
|
885
|
-
first = params.shift
|
886
|
-
<span class="r">if</span> params.empty? <span class="c"># single param - like each</span>
|
887
|
-
<span class="r">if</span> block_given?
|
888
|
-
first.map{|e| <span class="r">yield</span> e }
|
889
|
-
<span class="r">else</span>
|
890
|
-
first.map
|
891
|
-
<span class="r">end</span>
|
892
|
-
<span class="r">else</span>
|
893
|
-
padded_first = <span class="co">Array</span>.new( [first, *params].max_by(&<span class="sy">:size</span>).size ){|i| first[i] } <span class="c"># append nils</span>
|
894
|
-
obj = padded_first.zip *params
|
895
|
-
<span class="r">if</span> block_given?
|
896
|
-
obj.map{|es| <span class="r">yield</span> *es }
|
897
|
-
<span class="r">else</span>
|
898
|
-
obj.map.to_enum
|
899
|
-
<span class="r">end</span>
|
900
|
-
<span class="r">end</span>
|
901
|
-
<span class="r">end</span>
|
902
|
-
|
903
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/ivars'">ivars</h3>
|
904
|
-
<table class="cube_table"
|
905
|
-
id="ivars_cube"
|
906
|
-
title="require 'zucker/ivars'"> <tr><th>Summary</th> <td>This method lets you easily assign lots of instance variables.</td></tr><tr><th>Why?</th> <td>Often, you have to write boilerplate code for assigning instance varialbles, for example this one:<br/><code>def initialize(variable1, variable2)<br/> @variable1, @variable2 = variable1, variable2<br/>end</code><br/></td></tr><tr><th>Methods/Usage</th> <td><h5>instance_variables_from, ivars</h5><pre class="usage source" style="display:block"><span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
907
|
-
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
908
|
-
|
909
|
-
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
910
|
-
instance_variables_from params <span class="c"># # assigns @c and @d</span>
|
911
|
-
<span class="r">end</span>
|
912
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_spec">(<a href="javascript:show('ivars_spec')">show</a>)</span>
|
913
|
-
<pre class="source" id="ivars_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/ivars</span><span class="dl">'</span></span>
|
914
|
-
|
915
|
-
describe <span class="s"><span class="dl">'</span><span class="k">instance_variables_from</span><span class="dl">'</span></span> <span class="r">do</span>
|
916
|
-
it <span class="s"><span class="dl">'</span><span class="k">should tansform the given parameter to instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
917
|
-
<span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
918
|
-
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
919
|
-
|
920
|
-
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
921
|
-
ivars params <span class="c"># # assigns @c and @d</span>
|
922
|
-
<span class="r">end</span>
|
923
|
-
|
924
|
-
a_method
|
925
|
-
<span class="iv">@a</span>.should == <span class="i">1</span>
|
926
|
-
<span class="iv">@b</span>.should == <span class="i">2</span>
|
927
|
-
<span class="iv">@c</span>.should == <span class="i">3</span>
|
928
|
-
<span class="iv">@d</span>.should == <span class="i">4</span>
|
929
|
-
|
930
|
-
<span class="r">end</span>
|
931
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_ivars_source">(<a href="javascript:show('ivars_source')">show</a>)</span>
|
932
|
-
<pre class="source" id="ivars_source"><span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
|
933
|
-
iter =
|
934
|
-
<span class="r">if</span> obj.is_a? <span class="co">Binding</span>
|
935
|
-
obj.eval(<span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span>).map{|e| [obj.eval(<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="dl">"</span></span>), e] }
|
936
|
-
<span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
|
937
|
-
obj.map{|k,v| [v,k] }
|
938
|
-
<span class="r">else</span>
|
939
|
-
<span class="c"># elsif obj.is_a? Enumerable</span>
|
940
|
-
obj.each.with_index
|
941
|
-
<span class="r">end</span>
|
942
|
-
|
943
|
-
ret = []
|
944
|
-
iter.each{ |value, arg|
|
945
|
-
arg = arg.to_s
|
946
|
-
<span class="r">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
947
|
-
arg = <span class="s"><span class="dl">'</span><span class="k">_</span><span class="dl">'</span></span> + arg <span class="r">if</span> (<span class="i">48</span>..<span class="i">57</span>).member? arg.unpack(<span class="s"><span class="dl">'</span><span class="k">C</span><span class="dl">'</span></span>)[<span class="i">0</span>] <span class="c"># 1.8+1.9</span>
|
948
|
-
ret << ivar = <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>arg<span class="idl">}</span></span><span class="dl">"</span></span>
|
949
|
-
<span class="pc">self</span>.instance_variable_set ivar, value
|
950
|
-
<span class="r">end</span>
|
951
|
-
}
|
952
|
-
ret
|
953
|
-
<span class="r">end</span>
|
954
|
-
<span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
|
955
|
-
|
956
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/kernel'">kernel</h3>
|
957
|
-
<table class="cube_table"
|
958
|
-
id="kernel_cube"
|
959
|
-
title="require 'zucker/kernel'"> <tr><th>Summary</th> <td>Some useful general shortcut methods.</td></tr><tr><th>Why?</th> <td>Readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>activate_warnings!</h5><pre class="usage source" style="display:block">activate_warnings! <span class="c"># sets $VERBOSE to 1</span></pre><h5>deactivate_warnings!</h5><pre class="usage source" style="display:block">deactivate_warnings! <span class="c"># sets $VERBOSE to 0</span></pre><h5>library?</h5><pre class="usage source" style="display:block">library? <span class="c"># checks, if the current file is ran directly</span></pre><h5>ignore_sigint!</h5><pre class="usage source" style="display:block">ignore_sigint! <span class="c"># blocks CTRL+C</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_kernel_spec">(<a href="javascript:show('kernel_spec')">show</a>)</span>
|
960
|
-
<pre class="source" id="kernel_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/kernel</span><span class="dl">'</span></span>
|
961
|
-
|
962
|
-
describe <span class="s"><span class="dl">'</span><span class="k">activate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
963
|
-
it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to true</span><span class="dl">'</span></span> <span class="r">do</span>
|
964
|
-
activate_warnings!
|
965
|
-
<span class="gv">$VERBOSE</span>.should == <span class="pc">true</span>
|
966
|
-
<span class="r">end</span>
|
967
|
-
<span class="r">end</span>
|
968
|
-
|
969
|
-
describe <span class="s"><span class="dl">'</span><span class="k">deactivate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
970
|
-
it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to false</span><span class="dl">'</span></span> <span class="r">do</span>
|
971
|
-
deactivate_warnings!
|
972
|
-
<span class="gv">$VERBOSE</span>.should == <span class="pc">false</span>
|
973
|
-
<span class="r">end</span>
|
974
|
-
<span class="r">end</span>
|
975
|
-
|
976
|
-
describe <span class="s"><span class="dl">'</span><span class="k">library?</span><span class="dl">'</span></span> <span class="r">do</span>
|
977
|
-
it <span class="s"><span class="dl">'</span><span class="k">should return false if the file is invoked directly</span><span class="dl">'</span></span> <span class="r">do</span>
|
978
|
-
library?.should == ( <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span> )
|
979
|
-
<span class="r">end</span>
|
980
|
-
<span class="r">end</span>
|
981
|
-
|
982
|
-
describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!</span><span class="dl">'</span></span> <span class="r">do</span>
|
983
|
-
it <span class="s"><span class="dl">'</span><span class="k">should catch ctrl+c signals</span><span class="dl">'</span></span> <span class="r">do</span>
|
984
|
-
<span class="c"># ...</span>
|
985
|
-
<span class="r">end</span>
|
986
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_kernel_source">(<a href="javascript:show('kernel_source')">show</a>)</span>
|
987
|
-
<pre class="source" id="kernel_source"><span class="r">def</span> <span class="fu">activate_warnings!</span>
|
988
|
-
<span class="gv">$VERBOSE</span> = <span class="pc">true</span>
|
989
|
-
<span class="r">end</span>
|
990
|
-
|
991
|
-
<span class="r">def</span> <span class="fu">deactivate_warnings!</span>
|
992
|
-
<span class="gv">$VERBOSE</span> = <span class="pc">false</span>
|
993
|
-
<span class="r">end</span>
|
994
|
-
|
995
|
-
<span class="r">def</span> <span class="fu">library?</span>
|
996
|
-
<span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span>
|
997
|
-
<span class="r">end</span>
|
998
|
-
|
999
|
-
<span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
|
1000
|
-
<span class="co">Signal</span>.trap *<span class="s"><span class="dl">%w|</span><span class="k">SIGINT IGNORE</span><span class="dl">|</span></span>
|
1001
|
-
<span class="r">end</span>
|
1002
|
-
|
1003
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mcopy'">mcopy</h3>
|
1004
|
-
<table class="cube_table"
|
1005
|
-
id="mcopy_cube"
|
1006
|
-
title="require 'zucker/mcopy'"> <tr><th>Summary</th> <td>Adds <code>Object#mcopy</code> to create a deep copy using <code>Marshal</code>.</td></tr><tr><th>Why?</th> <td>Every Ruby book describes, you can do <code>Marshal.load Marshal.dump object</code> to create a deep copy... But who needs this verbose syntax in everyday coding?</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#mcopy</h5><pre class="usage source" style="display:block">a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
1007
|
-
b = a.mcopy
|
1008
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_spec">(<a href="javascript:show('mcopy_spec')">show</a>)</span>
|
1009
|
-
<pre class="source" id="mcopy_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mcopy</span><span class="dl">'</span></span>
|
1010
|
-
|
1011
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
|
1012
|
-
it <span class="s"><span class="dl">'</span><span class="k">create a (deep) copy via marshalling</span><span class="dl">'</span></span> <span class="r">do</span>
|
1013
|
-
a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
1014
|
-
b = a.mcopy
|
1015
|
-
b.should == a
|
1016
|
-
|
1017
|
-
b[<span class="i">0</span>][<span class="i">1</span>,<span class="i">1</span>] = <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1018
|
-
b.should_not == a
|
1019
|
-
<span class="r">end</span>
|
1020
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mcopy_source">(<a href="javascript:show('mcopy_source')">show</a>)</span>
|
1021
|
-
<pre class="source" id="mcopy_source"><span class="r">class</span> <span class="cl">Object</span>
|
1022
|
-
<span class="r">def</span> <span class="fu">mcopy</span>
|
1023
|
-
<span class="co">Marshal</span>.load <span class="co">Marshal</span>.dump <span class="pc">self</span>
|
1024
|
-
<span class="r">end</span>
|
1025
|
-
<span class="c"># alias copy mcopy</span>
|
1026
|
-
<span class="r">end</span>
|
1027
|
-
|
1028
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/regexp2proc'">regexp2proc</h3>
|
1029
|
-
<table class="cube_table"
|
1030
|
-
id="regexp2proc_cube"
|
1031
|
-
title="require 'zucker/regexp2proc'"> <tr><th>Summary</th> <td>Use &/regex/ to match it against strings.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#to_proc</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["j", nil, nil, "y"]</span>
|
1032
|
-
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["just", "array"]</span>
|
1033
|
-
</pre></td></tr><tr><th>Further information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_regexp2proc_spec">(<a href="javascript:show('regexp2proc_spec')">show</a>)</span>
|
1034
|
-
<pre class="source" id="regexp2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/regexp2proc</span><span class="dl">'</span></span>
|
1035
|
-
|
1036
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1037
|
-
it <span class="s"><span class="dl">'</span><span class="k">should match the regex</span><span class="dl">'</span></span> <span class="r">do</span>
|
1038
|
-
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map( &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">"</span><span class="k">j</span><span class="dl">"</span></span>, <span class="pc">nil</span>, <span class="pc">nil</span>, <span class="s"><span class="dl">"</span><span class="k">y</span><span class="dl">"</span></span>]
|
1039
|
-
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select( &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">"</span><span class="k">just</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">array</span><span class="dl">"</span></span>]
|
1040
|
-
<span class="r">end</span>
|
1041
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_regexp2proc_source">(<a href="javascript:show('regexp2proc_source')">show</a>)</span>
|
1042
|
-
<pre class="source" id="regexp2proc_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
1043
|
-
<span class="r">def</span> <span class="fu">to_proc</span>
|
1044
|
-
proc <span class="r">do</span> |e|
|
1045
|
-
e.to_s[<span class="pc">self</span>]
|
1046
|
-
<span class="r">end</span>
|
1047
|
-
<span class="r">end</span>
|
1048
|
-
<span class="r">end</span>
|
1049
|
-
|
1050
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/sandbox'">sandbox</h3>
|
1051
|
-
<table class="cube_table"
|
1052
|
-
id="sandbox_cube"
|
1053
|
-
title="require 'zucker/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (-> integrated) in the <code>$SAFE</code> concept.</td></tr><tr><th>Methods/Usage</th> <td><h5>sandbox</h5><pre class="usage source" style="display:block">sandbox <span class="r">do</span>
|
1054
|
-
<span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
|
1055
|
-
<span class="r">end</span>
|
1056
|
-
<span class="c"># everything's normal again</span>
|
1057
|
-
|
1058
|
-
sandbox( lambda{} ) <span class="r">do</span>
|
1059
|
-
<span class="c"># no Exception is thrown, if non-nil parameter is passed</span>
|
1060
|
-
<span class="r">end</span>
|
1061
|
-
<span class="c"># if it is a proc, it will be run instead, if an SecurityError gets raised</span>
|
1062
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_sandbox_spec">(<a href="javascript:show('sandbox_spec')">show</a>)</span>
|
1063
|
-
<pre class="source" id="sandbox_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/sandbox</span><span class="dl">'</span></span>
|
1064
|
-
|
1065
|
-
describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
|
1066
|
-
it <span class="s"><span class="dl">'</span><span class="k">should throw a SecurityError if bad commands are issued</span><span class="dl">'</span></span> <span class="r">do</span>
|
1067
|
-
proc <span class="r">do</span>
|
1068
|
-
sandbox <span class="r">do</span>
|
1069
|
-
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1070
|
-
<span class="r">end</span>
|
1071
|
-
<span class="r">end</span>.should raise_exception <span class="co">SecurityError</span>
|
1072
|
-
<span class="r">end</span>
|
1073
|
-
|
1074
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not throw an exception for errors, if the given parameter is not nil</span><span class="dl">'</span></span> <span class="r">do</span>
|
1075
|
-
proc <span class="r">do</span>
|
1076
|
-
sandbox( <span class="pc">true</span> ) <span class="r">do</span>
|
1077
|
-
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1078
|
-
<span class="r">end</span>
|
1079
|
-
<span class="r">end</span>.should_not raise_exception
|
1080
|
-
<span class="r">end</span>
|
1081
|
-
|
1082
|
-
it <span class="s"><span class="dl">'</span><span class="k">should run the proc passed as parameter for errors, if it is given</span><span class="dl">'</span></span> <span class="r">do</span>
|
1083
|
-
sandbox( lambda{|e| e.class } ) <span class="r">do</span>
|
1084
|
-
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1085
|
-
<span class="r">end</span>.should == <span class="co">SecurityError</span>
|
1086
|
-
<span class="r">end</span>
|
1087
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_sandbox_source">(<a href="javascript:show('sandbox_source')">show</a>)</span>
|
1088
|
-
<pre class="source" id="sandbox_source"><span class="r">def</span> <span class="fu">sandbox</span>(rescueblock_or_default=<span class="pc">nil</span>)
|
1089
|
-
<span class="co">Thread</span>.start <span class="r">do</span>
|
1090
|
-
<span class="gv">$SAFE</span> = <span class="i">4</span>
|
1091
|
-
<span class="r">yield</span>
|
1092
|
-
<span class="r">end</span>.value
|
1093
|
-
<span class="r">rescue</span> <span class="co">SecurityError</span> => e
|
1094
|
-
<span class="r">if</span> !rescueblock_or_default.nil?
|
1095
|
-
<span class="r">if</span> rescueblock_or_default.is_a? <span class="co">Proc</span>
|
1096
|
-
rescueblock_or_default.call e
|
1097
|
-
<span class="r">else</span>
|
1098
|
-
rescueblock_or_default
|
1099
|
-
<span class="r">end</span>
|
1100
|
-
<span class="r">else</span>
|
1101
|
-
raise e
|
1102
|
-
<span class="r">end</span>
|
1103
|
-
<span class="r">end</span>
|
1104
|
-
|
1105
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/square_brackets_for'">square_brackets_for</h3>
|
1106
|
-
<table class="cube_table"
|
1107
|
-
id="square_brackets_for_cube"
|
1108
|
-
title="require 'zucker/square_brackets_for'"> <tr><th>Summary</th> <td>This helper methods defines <code>[]</code> and <code>[]=</code> for accesing an instance variable.</td></tr><tr><th>Methods/Usage</th> <td><h5>square_brackets_for</h5><pre class="usage source" style="display:block"><span class="r">class</span> <span class="cl">Klass</span>
|
1109
|
-
<span class="r">def</span> <span class="fu">initialize</span>
|
1110
|
-
<span class="iv">@var</span> = {
|
1111
|
-
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
1112
|
-
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
1113
|
-
}
|
1114
|
-
<span class="r">end</span>
|
1115
|
-
|
1116
|
-
square_brackets_for <span class="sy">:var</span> <span class="c"># creates [] and []=</span>
|
1117
|
-
<span class="c"># square_brackets_for :var, false # would create only []</span>
|
1118
|
-
<span class="r">end</span>
|
1119
|
-
|
1120
|
-
a = <span class="co">Klass</span>.new
|
1121
|
-
a[<span class="sy">:a_key</span>] <span class="c"># => 1</span>
|
1122
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_spec">(<a href="javascript:show('square_brackets_for_spec')">show</a>)</span>
|
1123
|
-
<pre class="source" id="square_brackets_for_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/square_brackets_for</span><span class="dl">'</span></span>
|
1124
|
-
|
1125
|
-
describe <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span> <span class="r">do</span>
|
1126
|
-
|
1127
|
-
before <span class="r">do</span>
|
1128
|
-
<span class="r">class</span> <span class="cl">Klass</span>
|
1129
|
-
<span class="r">def</span> <span class="fu">initialize</span>
|
1130
|
-
<span class="iv">@var</span> = {
|
1131
|
-
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
1132
|
-
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
1133
|
-
}
|
1134
|
-
<span class="r">end</span>
|
1135
|
-
|
1136
|
-
<span class="iv">@eigenvar</span> = {
|
1137
|
-
<span class="sy">:a_key</span> => <span class="i">99</span>
|
1138
|
-
}
|
1139
|
-
<span class="r">end</span>
|
1140
|
-
<span class="r">end</span>
|
1141
|
-
|
1142
|
-
it <span class="s"><span class="dl">'</span><span class="k">should define a [] getter (not a setter) for an instance var, if the second parameter is false</span><span class="dl">'</span></span> <span class="r">do</span>
|
1143
|
-
|
1144
|
-
<span class="r">class</span> <span class="cl">Klass</span>
|
1145
|
-
square_brackets_for <span class="sy">:var</span>, <span class="pc">nil</span>
|
1146
|
-
<span class="r">end</span>
|
1147
|
-
|
1148
|
-
a = <span class="co">Klass</span>.new
|
1149
|
-
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
1150
|
-
|
1151
|
-
proc <span class="r">do</span>
|
1152
|
-
a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">not possible</span><span class="dl">'</span></span>
|
1153
|
-
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
1154
|
-
<span class="r">end</span>
|
1155
|
-
|
1156
|
-
it <span class="s"><span class="dl">'</span><span class="k">should define [] and []= for accessing an instance variable</span><span class="dl">'</span></span> <span class="r">do</span>
|
1157
|
-
|
1158
|
-
<span class="r">class</span> <span class="cl">Klass</span>
|
1159
|
-
square_brackets_for <span class="sy">:var</span>
|
1160
|
-
<span class="r">end</span>
|
1161
|
-
|
1162
|
-
a = <span class="co">Klass</span>.new
|
1163
|
-
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
1164
|
-
|
1165
|
-
a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
|
1166
|
-
a[<span class="sy">:this_is</span>].should == <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
|
1167
|
-
<span class="r">end</span>
|
1168
|
-
|
1169
|
-
it <span class="s"><span class="dl">'</span><span class="k">should also work for class-instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
1170
|
-
|
1171
|
-
<span class="r">class</span> <span class="cl">Klass</span>
|
1172
|
-
<span class="r">class</span> << <span class="cl">Klass</span>
|
1173
|
-
square_brackets_for <span class="sy">:eigenvar</span>
|
1174
|
-
<span class="r">end</span>
|
1175
|
-
<span class="r">end</span>
|
1176
|
-
|
1177
|
-
<span class="co">Klass</span>[<span class="sy">:a_key</span>].should == <span class="i">99</span>
|
1178
|
-
<span class="r">end</span>
|
1179
|
-
|
1180
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_square_brackets_for_source">(<a href="javascript:show('square_brackets_for_source')">show</a>)</span>
|
1181
|
-
<pre class="source" id="square_brackets_for_source"><span class="r">def</span> <span class="fu">square_brackets_for</span>(ivar, assignment = <span class="pc">true</span>)
|
1182
|
-
<span class="c"># undef [] if respond_to? :[]</span>
|
1183
|
-
<span class="c"># undef []= if respond_to? :[]=</span>
|
1184
|
-
|
1185
|
-
<span class="c">#instance_eval do</span>
|
1186
|
-
define_method <span class="sy">:[]</span> <span class="r">do</span> |key|
|
1187
|
-
(instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">"</span></span>)[key]
|
1188
|
-
<span class="r">end</span>
|
1189
|
-
|
1190
|
-
<span class="r">if</span> assignment
|
1191
|
-
define_method <span class="sy">:[]=</span> <span class="r">do</span> |key, value|
|
1192
|
-
(instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">"</span></span>)[key] = value
|
1193
|
-
<span class="r">end</span>
|
1194
|
-
<span class="r">end</span>
|
1195
|
-
<span class="c">#end</span>
|
1196
|
-
|
1197
|
-
<span class="r">end</span>
|
1198
|
-
|
1199
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/string'">string</h3>
|
1200
|
-
<table class="cube_table"
|
1201
|
-
id="string_cube"
|
1202
|
-
title="require 'zucker/string'"> <tr><th>Summary</th> <td><code>String</code> extensions.</td></tr><tr><th>Why?</th> <td>Strings cannot be comfortable enough ;).</td></tr><tr><th>Methods/Usage</th> <td><h5>String#^</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Yes vs No</span><span class="dl">'</span></span>^<span class="i">3</span> <span class="c"># => 'Yes'</span></pre><h5>String#lchomp</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k"> Yes</span><span class="dl">'</span></span>.lchomp <span class="c"># => 'Yes'</span></pre><h5>String#lchomp!</h5><pre class="usage source" style="display:block"><span class="c"># mutable lchomp version</span></pre><h5>String#ords</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>.ords <span class="c"># => [72, 97, 108, 108, 111]</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_string_spec">(<a href="javascript:show('string_spec')">show</a>)</span>
|
1203
|
-
<pre class="source" id="string_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/string</span><span class="dl">'</span></span>
|
1204
|
-
|
1205
|
-
describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1206
|
-
it <span class="s"><span class="dl">'</span><span class="k">should give C-like substring access to strings</span><span class="dl">'</span></span> <span class="r">do</span>
|
1207
|
-
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1208
|
-
|
1209
|
-
(string^<span class="i">0</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1210
|
-
(string^<span class="i">1</span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1211
|
-
(string^<span class="i">13</span>).should == <span class="s"><span class="dl">'</span><span class="k">Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1212
|
-
(string^<span class="i">-1</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil</span><span class="dl">'</span></span>
|
1213
|
-
(string^<span class="i">38</span>).should == <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1214
|
-
(string^<span class="i">99</span>).should == <span class="pc">nil</span>
|
1215
|
-
<span class="r">end</span>
|
1216
|
-
<span class="r">end</span>
|
1217
|
-
|
1218
|
-
describe <span class="s"><span class="dl">'</span><span class="k">String#lchomp</span><span class="dl">'</span></span> <span class="r">do</span>
|
1219
|
-
it <span class="s"><span class="dl">'</span><span class="k">should chomp on the left side</span><span class="dl">'</span></span> <span class="r">do</span>
|
1220
|
-
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1221
|
-
string.lchomp(<span class="s"><span class="dl">'</span><span class="k">T</span><span class="dl">'</span></span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1222
|
-
<span class="r">end</span>
|
1223
|
-
<span class="r">end</span>
|
1224
|
-
|
1225
|
-
describe <span class="s"><span class="dl">'</span><span class="k">String#ords</span><span class="dl">'</span></span> <span class="r">do</span>
|
1226
|
-
it <span class="s"><span class="dl">'</span><span class="k">should unpack characters</span><span class="dl">'</span></span> <span class="r">do</span>
|
1227
|
-
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1228
|
-
string.ords.should == [<span class="i">84</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">101</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">99</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">32</span>, <span class="i">73</span>, <span class="i">110</span>, <span class="i">102</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">109</span>, <span class="i">97</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">107</span>, <span class="i">32</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">116</span>, <span class="i">32</span>, <span class="i">118</span>, <span class="i">111</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">32</span>, <span class="i">103</span>, <span class="i">101</span>, <span class="i">105</span>, <span class="i">108</span>, <span class="i">33</span>]
|
1229
|
-
<span class="r">end</span>
|
1230
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_string_source">(<a href="javascript:show('string_source')">show</a>)</span>
|
1231
|
-
<pre class="source" id="string_source"><span class="r">class</span> <span class="cl">String</span>
|
1232
|
-
<span class="r">def</span> <span class="fu">^</span>(pos)
|
1233
|
-
pos = pos.to_i
|
1234
|
-
<span class="r">if</span> pos >= <span class="i">0</span>
|
1235
|
-
<span class="pc">self</span>[pos..<span class="i">-1</span>]
|
1236
|
-
<span class="r">else</span>
|
1237
|
-
<span class="pc">self</span>[<span class="i">0</span>...pos]
|
1238
|
-
<span class="r">end</span>
|
1239
|
-
<span class="r">end</span>
|
1240
|
-
|
1241
|
-
<span class="r">def</span> <span class="fu">lchomp</span>(arg=<span class="gv">$/</span>)
|
1242
|
-
<span class="pc">self</span>.reverse.chomp(arg).reverse
|
1243
|
-
<span class="r">end</span>
|
1244
|
-
|
1245
|
-
<span class="r">def</span> <span class="fu">lchomp!</span>(arg=<span class="gv">$/</span>)
|
1246
|
-
<span class="pc">self</span>.reverse.chomp!(arg).reverse
|
1247
|
-
<span class="r">end</span>
|
1248
|
-
|
1249
|
-
<span class="r">def</span> <span class="fu">ords</span>
|
1250
|
-
<span class="pc">self</span>.unpack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
1251
|
-
<span class="r">end</span>
|
1252
|
-
<span class="r">end</span>
|
1253
|
-
|
1254
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/unary'">unary</h3>
|
1255
|
-
<table class="cube_table"
|
1256
|
-
id="unary_cube"
|
1257
|
-
title="require 'zucker/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Sometimes, you do not care if you get a <code>String</code> or <code>Symbol</code> as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators <code>String#-@</code> and <code>Symbol#+@</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#+@</h5><pre class="usage source" style="display:block">+<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => 'was_string'</span></pre><h5>String#-@</h5><pre class="usage source" style="display:block">-<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => :was_string</span></pre><h5>Symbol#+@</h5><pre class="usage source" style="display:block">+<span class="sy">:was_symbol</span> <span class="c"># => 'was_symbol'</span></pre><h5>Symbol#-@</h5><pre class="usage source" style="display:block">-<span class="sy">:was_symbol</span> <span class="c"># => :was_symbol</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)</td></tr><tr><th>Specification</th> <td> <span id="show_unary_spec">(<a href="javascript:show('unary_spec')">show</a>)</span>
|
1258
|
-
<pre class="source" id="unary_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/unary</span><span class="dl">'</span></span>
|
1259
|
-
|
1260
|
-
describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1261
|
-
it <span class="s"><span class="dl">'</span><span class="k">should convert to_s</span><span class="dl">'</span></span> <span class="r">do</span>
|
1262
|
-
+(<span class="sy">:matz</span>) == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> <span class="c"># () 1.8 bug</span>
|
1263
|
-
<span class="r">end</span>
|
1264
|
-
<span class="r">end</span>
|
1265
|
-
|
1266
|
-
describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1267
|
-
it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
|
1268
|
-
-<span class="sy">:matz</span> == <span class="sy">:matz</span>
|
1269
|
-
<span class="r">end</span>
|
1270
|
-
<span class="r">end</span>
|
1271
|
-
|
1272
|
-
describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1273
|
-
it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
|
1274
|
-
+<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span>
|
1275
|
-
<span class="r">end</span>
|
1276
|
-
<span class="r">end</span>
|
1277
|
-
|
1278
|
-
describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1279
|
-
it <span class="s"><span class="dl">'</span><span class="k">should convert to_sym</span><span class="dl">'</span></span> <span class="r">do</span>
|
1280
|
-
-<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="sy">:matz</span>
|
1281
|
-
<span class="r">end</span>
|
1282
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_unary_source">(<a href="javascript:show('unary_source')">show</a>)</span>
|
1283
|
-
<pre class="source" id="unary_source"><span class="r">class</span> <span class="cl">String</span>
|
1284
|
-
<span class="r">def</span> <span class="fu">+@</span>
|
1285
|
-
<span class="pc">self</span>
|
1286
|
-
<span class="r">end</span>
|
1287
|
-
|
1288
|
-
<span class="r">def</span> <span class="fu">-@</span>
|
1289
|
-
to_sym
|
1290
|
-
<span class="r">end</span>
|
1291
|
-
<span class="r">end</span>
|
1292
|
-
|
1293
|
-
<span class="r">class</span> <span class="cl">Symbol</span>
|
1294
|
-
<span class="r">def</span> <span class="fu">+@</span>
|
1295
|
-
to_s
|
1296
|
-
<span class="r">end</span>
|
1297
|
-
|
1298
|
-
<span class="r">def</span> <span class="fu">-@</span>
|
1299
|
-
<span class="pc">self</span>
|
1300
|
-
<span class="r">end</span>
|
1301
|
-
<span class="r">end</span>
|
1302
|
-
|
1303
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (<code>+:literal</code> not possible)</td></tr></table> <h3 title="require 'zucker/union'">union</h3>
|
1304
|
-
<table class="cube_table"
|
1305
|
-
id="union_cube"
|
1306
|
-
title="require 'zucker/union'"> <tr><th>Summary</th> <td>Easy creation of <code>Regexp.union</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#|, String#|</h5><pre class="usage source" style="display:block"><span class="rx"><span class="dl">/</span><span class="k">Ruby</span><span class="ch">\d</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="k">test</span><span class="dl">/</span><span class="mod">i</span></span> | <span class="s"><span class="dl">"</span><span class="k">cheat</span><span class="dl">"</span></span>
|
1307
|
-
<span class="c"># creates a Regexp similar to:</span>
|
1308
|
-
<span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
|
1309
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_union_spec">(<a href="javascript:show('union_spec')">show</a>)</span>
|
1310
|
-
<pre class="source" id="union_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/union</span><span class="dl">'</span></span>
|
1311
|
-
|
1312
|
-
shared_examples_for <span class="s"><span class="dl">"</span><span class="k">Regexp.union operator</span><span class="dl">"</span></span> <span class="r">do</span>
|
1313
|
-
it <span class="s"><span class="dl">"</span><span class="k">should create an Regexp.union of both operands</span><span class="dl">"</span></span> <span class="r">do</span>
|
1314
|
-
<span class="r">end</span>
|
1315
|
-
<span class="r">end</span>
|
1316
|
-
|
1317
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1318
|
-
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1319
|
-
<span class="r">end</span>
|
1320
|
-
|
1321
|
-
describe <span class="s"><span class="dl">'</span><span class="k">String#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1322
|
-
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1323
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_union_source">(<a href="javascript:show('union_source')">show</a>)</span>
|
1324
|
-
<pre class="source" id="union_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
1325
|
-
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1326
|
-
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1327
|
-
<span class="r">end</span>
|
1328
|
-
<span class="r">end</span>
|
1329
|
-
|
1330
|
-
<span class="r">class</span> <span class="cl">String</span>
|
1331
|
-
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1332
|
-
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1333
|
-
<span class="r">end</span>
|
1334
|
-
<span class="r">end</span>
|
1335
|
-
|
1336
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table>
|
1337
|
-
</div>
|
1338
|
-
<h2 title="require 'zucker/debug'">Cubes[debug]</h2>
|
1339
|
-
<div class="cubes">
|
1340
|
-
<h3 title="require 'zucker/D'">D</h3>
|
1341
|
-
<table class="cube_table"
|
1342
|
-
id="D_cube"
|
1343
|
-
title="require 'zucker/D'"> <tr><th>Summary</th> <td>Easy debug printing with the <code>p</code> alternative <code>.D</code>. It outputs to stdout and returns <code>self</code>. Accepts a block.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#D</h5><pre class="usage source" style="display:block">some.<span class="co">D</span>.methods.<span class="co">D</span>.noone.<span class="co">D</span>.knows.<span class="co">D</span>
|
1344
|
-
<span class="c"># ...outputs 4 lines with the inspected objects</span>
|
1345
|
-
<span class="c"># => (result)</span>
|
1346
|
-
|
1347
|
-
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
|
1348
|
-
<span class="c"># outputs -0.916521547915634</span>
|
1349
|
-
<span class="c"># => 20.0834784520844</span>
|
1350
|
-
|
1351
|
-
name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
|
1352
|
-
<span class="s"><span class="dl">'</span><span class="k">Hello </span><span class="dl">'</span></span> + name.<span class="co">D</span>{|e| <span class="s"><span class="dl">"</span><span class="k">The length is: </span><span class="il"><span class="idl">#{</span>e.size<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1353
|
-
<span class="c"># outputs "The length is: 5"</span>
|
1354
|
-
<span class="c"># => 'Hello Earth'</span>
|
1355
|
-
</pre></td></tr><tr><th>Further information</th> <td>This is inspired by the funny <a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions"><code>.tap</code> method</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_D_spec">(<a href="javascript:show('D_spec')">show</a>)</span>
|
1356
|
-
<pre class="source" id="D_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/D</span><span class="dl">'</span></span>
|
1357
|
-
|
1358
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span><span class="dl">'</span></span> <span class="r">do</span>
|
1359
|
-
|
1360
|
-
let <span class="sy">:a</span> <span class="r">do</span>
|
1361
|
-
[ <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span> ]
|
1362
|
-
<span class="r">end</span>
|
1363
|
-
|
1364
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not change the object"s value</span><span class="dl">'</span></span> <span class="r">do</span>
|
1365
|
-
a.each{ |e|
|
1366
|
-
(e.<span class="co">D</span>).should == e
|
1367
|
-
}
|
1368
|
-
|
1369
|
-
a.each{ |e|
|
1370
|
-
(e.<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}).should == e
|
1371
|
-
}
|
1372
|
-
<span class="r">end</span>
|
1373
|
-
|
1374
|
-
it <span class="s"><span class="dl">"</span><span class="k">should puts .inspect if no block is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
1375
|
-
capture_stdout <span class="r">do</span>
|
1376
|
-
a[<span class="i">0</span>].<span class="co">D</span>
|
1377
|
-
a[<span class="i">1</span>].<span class="co">D</span>
|
1378
|
-
a[<span class="i">6</span>].<span class="co">D</span>
|
1379
|
-
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">1</span><span class="ch">\n</span><span class="k">"1"</span><span class="ch">\n</span><span class="k">true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
1380
|
-
<span class="r">end</span>
|
1381
|
-
|
1382
|
-
it <span class="s"><span class="dl">"</span><span class="k">should puts the block if it is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
1383
|
-
capture_stdout <span class="r">do</span>
|
1384
|
-
a[<span class="i">0</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1385
|
-
a[<span class="i">1</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1386
|
-
a[<span class="i">6</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1387
|
-
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
1388
|
-
<span class="r">end</span>
|
1389
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_D_source">(<a href="javascript:show('D_source')">show</a>)</span>
|
1390
|
-
<pre class="source" id="D_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
1391
|
-
<span class="r">def</span> <span class="fu">D</span>(*args, &block)
|
1392
|
-
<span class="r">if</span> args.empty?
|
1393
|
-
tap{
|
1394
|
-
<span class="r">if</span> block_given?
|
1395
|
-
puts <span class="r">yield</span> <span class="pc">self</span>
|
1396
|
-
<span class="r">else</span>
|
1397
|
-
puts <span class="pc">self</span>.inspect
|
1398
|
-
<span class="r">end</span>
|
1399
|
-
}
|
1400
|
-
<span class="r">else</span>
|
1401
|
-
raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">"</span><span class="k">.D - The parser thought that the code after .D are method arguments... Please don't put a space after D or use .D() or .D{} in this case!</span><span class="dl">"</span></span>
|
1402
|
-
<span class="c"># eval ...</span>
|
1403
|
-
<span class="r">end</span>
|
1404
|
-
<span class="r">end</span>
|
1405
|
-
<span class="r">end</span>
|
1406
|
-
|
1407
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/binding'">binding</h3>
|
1408
|
-
<table class="cube_table"
|
1409
|
-
id="binding_cube"
|
1410
|
-
title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds a verbose <code>Binding#inspect</code>.</td></tr><tr><th>Why?</th> <td>It gives you information about the current environment.</td></tr><tr><th>Methods/Usage</th> <td><h5>Binding#inspect</h5><pre class="usage source" style="display:block">binding.inspect</pre></td></tr><tr><th>Specification</th> <td> <span id="show_binding_spec">(<a href="javascript:show('binding_spec')">show</a>)</span>
|
1411
|
-
<pre class="source" id="binding_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/binding</span><span class="dl">'</span></span>
|
1412
|
-
|
1413
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Binding#inspect</span><span class="dl">'</span></span> <span class="r">do</span>
|
1414
|
-
it <span class="s"><span class="dl">'</span><span class="k">should output the current environment</span><span class="dl">'</span></span> <span class="r">do</span>
|
1415
|
-
pending <span class="s"><span class="dl">'</span><span class="k">#TODO: write spec</span><span class="dl">'</span></span>
|
1416
|
-
<span class="r">end</span>
|
1417
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_binding_source">(<a href="javascript:show('binding_source')">show</a>)</span>
|
1418
|
-
<pre class="source" id="binding_source"><span class="r">class</span> <span class="cl">Binding</span>
|
1419
|
-
<span class="r">def</span> <span class="fu">inspect</span>
|
1420
|
-
put_vars = lambda { |array|
|
1421
|
-
<span class="r">if</span> array.empty?
|
1422
|
-
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
1423
|
-
<span class="r">else</span>
|
1424
|
-
array.map{|e| <span class="s"><span class="dl">"</span><span class="k"> - </span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="k">: </span><span class="il"><span class="idl">#{</span> <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">#{e}</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>}*<span class="s"><span class="dl">"</span><span class="ch">\n</span><span class="dl">"</span></span>
|
1425
|
-
<span class="r">end</span>
|
1426
|
-
}
|
1427
|
-
|
1428
|
-
<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.to_s<span class="idl">}</span></span><span class="k">
|
1429
|
-
local vars
|
1430
|
-
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
1431
|
-
(instance vars)
|
1432
|
-
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">instance_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
1433
|
-
self
|
1434
|
-
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">self</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="k">
|
1435
|
-
block_given?
|
1436
|
-
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">block_given?</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>
|
1437
|
-
|
1438
|
-
<span class="r">end</span>
|
1439
|
-
<span class="r">end</span>
|
1440
|
-
|
1441
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mm'">mm</h3>
|
1442
|
-
<table class="cube_table"
|
1443
|
-
id="mm_cube"
|
1444
|
-
title="require 'zucker/mm'"> <tr><th>Summary</th> <td><code>mm</code> displays an ordered public method list.</td></tr><tr><th>Why?</th> <td>See one object's methods without those rarely used inherited methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#method_list, Object#mm</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">test</span><span class="dl">'</span></span>.mm <span class="i">2</span> <span class="c"># outputs the list (2 levels deep)</span></pre></td></tr><tr><th>Further information</th> <td>See <a href="http://rbjl.net/31-the-multi-mega-method-list">this article</a> for more information.</td></tr><tr><th>Specification</th> <td> <span id="show_mm_spec">(<a href="javascript:show('mm_spec')">show</a>)</span>
|
1445
|
-
<pre class="source" id="mm_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mm</span><span class="dl">'</span></span>
|
1446
|
-
|
1447
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#method_list</span><span class="dl">'</span></span> <span class="r">do</span>
|
1448
|
-
it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span> <span class="r">do</span>
|
1449
|
-
pending <span class="s"><span class="dl">'</span><span class="k">#TODO: write spec</span><span class="dl">'</span></span>
|
1450
|
-
<span class="r">end</span>
|
1451
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mm_source">(<a href="javascript:show('mm_source')">show</a>)</span>
|
1452
|
-
<pre class="source" id="mm_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
1453
|
-
<span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
|
1454
|
-
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
1455
|
-
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
1456
|
-
<span class="r">else</span>
|
1457
|
-
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
1458
|
-
|
1459
|
-
eigen = <span class="pc">self</span>.singleton_methods
|
1460
|
-
<span class="r">if</span> !eigen.empty?
|
1461
|
-
puts <span class="sy">:Eigenclass</span> <span class="c"># sorry for not being up to date, I just love the word</span>
|
1462
|
-
p <span class="pc">self</span>.singleton_methods
|
1463
|
-
<span class="r">end</span>
|
1464
|
-
<span class="r">end</span>
|
1465
|
-
|
1466
|
-
levels.times{ |level|
|
1467
|
-
<span class="r">if</span> cur = klass.ancestors[level]
|
1468
|
-
p cur <span class="c"># put class name</span>
|
1469
|
-
p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
|
1470
|
-
<span class="r">else</span>
|
1471
|
-
<span class="r">break</span>
|
1472
|
-
<span class="r">end</span>
|
1473
|
-
}
|
1474
|
-
|
1475
|
-
<span class="pc">self</span> <span class="c"># or whatever</span>
|
1476
|
-
<span class="r">end</span>
|
1477
|
-
|
1478
|
-
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
|
1479
|
-
<span class="r">end</span>
|
1480
|
-
|
1481
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (returns strings instead of symbols)</td></tr></table>
|
1482
|
-
</div>
|
1483
|
-
<br/>
|
1484
|
-
</div>
|
1485
|
-
<div id="foot">
|
1486
|
-
<div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
|
1487
|
-
This is the Ruby Zucker 2 documentation.
|
1488
|
-
The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
|
1489
|
-
Gem source can be found at <a href="http://github.com/janlelis/zucker">github</a>.
|
1490
|
-
</div>
|
1491
|
-
|
1492
|
-
</body>
|
1493
|
-
</html>
|
1494
|
-
|