zucker 0.9 → 1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/lib/zucker.rb +6 -1
- data/lib/zucker/1/{descriptions → desc}/D.yaml +2 -4
- data/lib/zucker/1/{descriptions → desc}/aliases.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/array.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/array2proc.yaml +2 -4
- data/lib/zucker/1/{descriptions → desc}/binding.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/blank.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/class2proc.yaml +2 -5
- data/lib/zucker/1/{descriptions → desc}/egonil.yaml +2 -5
- data/lib/zucker/1/{descriptions → desc}/enumerable.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/hash.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/hash2proc.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/iterate.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/ivars.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/kernel.yaml +0 -0
- data/lib/zucker/1/{descriptions → desc}/mcopy.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/mm.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/regexp2proc.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/sandbox.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/square_brackets_for.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/string.yaml +0 -2
- data/lib/zucker/1/desc/unary.yaml +15 -0
- data/lib/zucker/1/{descriptions → desc}/union.yaml +0 -0
- data/lib/zucker/1/{documentation → doc}/meta.yaml +0 -0
- data/lib/zucker/1/{documentation → doc}/zucker_doc.html +732 -604
- data/lib/zucker/1/doc/zucker_doc.rb +493 -0
- data/lib/zucker/1/hash2proc.rb +2 -0
- data/lib/zucker/1/{specification → spec}/D_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/aliases_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/array2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/array_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/binding_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/blank_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/class2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/egonil_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/enumerable_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/hash2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/hash_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/iterate_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/ivars_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/kernel_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/mcopy_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/mm_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/regexp2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/sandbox_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/square_brackets_for_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/string_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/unary_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/union_spec.rb +0 -0
- data/lib/zucker/D.rb +19 -0
- data/lib/zucker/aliases.rb +28 -0
- data/lib/zucker/all.rb +5 -1
- data/lib/zucker/array.rb +17 -0
- data/lib/zucker/array2proc.rb +10 -0
- data/lib/zucker/binding.rb +25 -0
- data/lib/zucker/blank.rb +26 -0
- data/lib/zucker/class2proc.rb +10 -0
- data/lib/zucker/debug.rb +3 -1
- data/lib/zucker/default.rb +24 -1
- data/lib/zucker/desc/D.yaml +23 -0
- data/lib/zucker/desc/aliases.yaml +16 -0
- data/lib/zucker/desc/array.yaml +14 -0
- data/lib/zucker/desc/array2proc.yaml +13 -0
- data/lib/zucker/desc/binding.yaml +10 -0
- data/lib/zucker/desc/blank.yaml +12 -0
- data/lib/zucker/desc/class2proc.yaml +12 -0
- data/lib/zucker/desc/egonil.yaml +17 -0
- data/lib/zucker/desc/enumerable.yaml +11 -0
- data/lib/zucker/desc/hash.yaml +12 -0
- data/lib/zucker/desc/hash2proc.yaml +11 -0
- data/lib/zucker/desc/iterate.yaml +17 -0
- data/lib/zucker/desc/ivars.yaml +20 -0
- data/lib/zucker/desc/kernel.yaml +13 -0
- data/lib/zucker/desc/mcopy.yaml +12 -0
- data/lib/zucker/desc/mm.yaml +12 -0
- data/lib/zucker/desc/regexp2proc.yaml +13 -0
- data/lib/zucker/desc/sandbox.yaml +14 -0
- data/lib/zucker/desc/square_brackets_for.yaml +23 -0
- data/lib/zucker/desc/string.yaml +13 -0
- data/lib/zucker/desc/unary.yaml +15 -0
- data/lib/zucker/desc/union.yaml +11 -0
- data/lib/zucker/doc/meta.yaml +4 -0
- data/lib/zucker/doc/zucker_doc.html +1286 -0
- data/lib/zucker/doc/zucker_doc.rb +493 -0
- data/lib/zucker/egonil.rb +14 -0
- data/lib/zucker/enumerable.rb +12 -0
- data/lib/zucker/hash.rb +19 -0
- data/lib/zucker/hash2proc.rb +14 -0
- data/lib/zucker/iterate.rb +23 -0
- data/lib/zucker/ivars.rb +26 -0
- data/lib/zucker/kernel.rb +20 -0
- data/lib/zucker/mcopy.rb +9 -0
- data/lib/zucker/mm.rb +31 -0
- data/lib/zucker/regexp2proc.rb +10 -0
- data/lib/zucker/sandbox.rb +15 -0
- data/lib/zucker/spec/D_spec.rb +27 -0
- data/lib/zucker/spec/aliases_spec.rb +34 -0
- data/lib/zucker/spec/array2proc_spec.rb +8 -0
- data/lib/zucker/spec/array_spec.rb +23 -0
- data/lib/zucker/spec/binding_spec.rb +6 -0
- data/lib/zucker/spec/blank_spec.rb +20 -0
- data/lib/zucker/spec/class2proc_spec.rb +9 -0
- data/lib/zucker/spec/egonil_spec.rb +30 -0
- data/lib/zucker/spec/enumerable_spec.rb +8 -0
- data/lib/zucker/spec/hash2proc_spec.rb +12 -0
- data/lib/zucker/spec/hash_spec.rb +10 -0
- data/lib/zucker/spec/iterate_spec.rb +54 -0
- data/lib/zucker/spec/ivars_spec.rb +20 -0
- data/lib/zucker/spec/kernel_spec.rb +34 -0
- data/lib/zucker/spec/mcopy_spec.rb +13 -0
- data/lib/zucker/spec/mm_spec.rb +6 -0
- data/lib/zucker/spec/regexp2proc_spec.rb +9 -0
- data/lib/zucker/spec/sandbox_spec.rb +7 -0
- data/lib/zucker/spec/square_brackets_for_spec.rb +59 -0
- data/lib/zucker/spec/string_spec.rb +29 -0
- data/lib/zucker/spec/unary_spec.rb +26 -0
- data/lib/zucker/spec/union_spec.rb +13 -0
- data/lib/zucker/square_brackets_for.rb +20 -0
- data/lib/zucker/string.rb +25 -0
- data/lib/zucker/unary.rb +22 -0
- data/lib/zucker/union.rb +14 -0
- metadata +119 -51
- data/lib/zucker/1/descriptions/unary.yaml +0 -15
- data/lib/zucker/1/documentation/zucker_doc.rb +0 -369
@@ -0,0 +1,13 @@
|
|
1
|
+
kernel:
|
2
|
+
summary: Some useful general shortcut methods
|
3
|
+
why: Readability
|
4
|
+
methods:
|
5
|
+
activate_warnings!: "activate_warnings! # sets $VERBOSE to 1"
|
6
|
+
deactivate_warnings!: "deactivate_warnings! # sets $VERBOSE to 0"
|
7
|
+
library?: "library? # checks, if the current file is ran directly"
|
8
|
+
ignore_sigint!: "ignore_sigint! # blocks CTRL+C"
|
9
|
+
package: default
|
10
|
+
versions:
|
11
|
+
- 1.9
|
12
|
+
- 1.8
|
13
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
mcopy:
|
2
|
+
summary: Adds ⇧Object#mcopy⇧ to create a deep copy using ⇧Marshal⇧.
|
3
|
+
why: Every Ruby book describes, you can do ⇧Marshal.load Marshal.dump object⇧ to create a deep copy... But who needs this verbose syntax in everyday coding?
|
4
|
+
methods:
|
5
|
+
Object#mcopy: |
|
6
|
+
a = %w[hello world]
|
7
|
+
b = a.mcopy
|
8
|
+
package: default
|
9
|
+
versions:
|
10
|
+
- 1.9
|
11
|
+
- 1.8
|
12
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
mm:
|
2
|
+
summary: ⇧mm⇧ displays an ordered public method list.
|
3
|
+
why: See some object's methods without those rarely used inherited methods
|
4
|
+
methods:
|
5
|
+
Object#method_list, Object#mm: "'test'.mm 2 # outputs the list (2 levels deep)"
|
6
|
+
package: debug
|
7
|
+
info:
|
8
|
+
- See →this article→http://rbjl.net/31-the-multi-mega-method-list→ for more information
|
9
|
+
versions:
|
10
|
+
- 1.9
|
11
|
+
- 1.8
|
12
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
regexp2proc:
|
2
|
+
summary: Use &/regex/ to match it against strings
|
3
|
+
methods:
|
4
|
+
Regexp#to_proc: |
|
5
|
+
%w|just another string array|.map &/[jy]/ # => ["j", nil, nil, "y"]
|
6
|
+
%w|just another string array|.select &/[jy]/ # => ["just", "array"]
|
7
|
+
package: default
|
8
|
+
info:
|
9
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
10
|
+
versions:
|
11
|
+
- 1.9
|
12
|
+
- 1.8
|
13
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
sandbox:
|
2
|
+
summary: Creates a sandbox area.
|
3
|
+
why: Ruby comes with sandboxes, but they are hidden (--> integrated) in the ⇧$SAFE⇧ concept
|
4
|
+
methods:
|
5
|
+
sandbox: |
|
6
|
+
sandbox do
|
7
|
+
# dangerous commands throw SecurityErrors ($SAFE=4)
|
8
|
+
end
|
9
|
+
# everything's normal again
|
10
|
+
package: default
|
11
|
+
versions:
|
12
|
+
- 1.9
|
13
|
+
- 1.8
|
14
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
square_brackets_for:
|
2
|
+
summary: This helper methods defines ⇧[]⇧ and ⇧[]=⇧ for accesing an instance variable.
|
3
|
+
methods:
|
4
|
+
square_brackets_for: |
|
5
|
+
class Klass
|
6
|
+
def initialize
|
7
|
+
@var = {
|
8
|
+
:a_key => 1,
|
9
|
+
:another_one => 2,
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
square_brackets_for :var # creates [] and []=
|
14
|
+
# square_brackets_for :var, false # would create only []
|
15
|
+
end
|
16
|
+
|
17
|
+
a = Klass.new
|
18
|
+
a[:a_key] # => 1
|
19
|
+
package: default
|
20
|
+
versions:
|
21
|
+
- 1.9
|
22
|
+
- 1.8
|
23
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
string:
|
2
|
+
summary: ⇧String⇧ extensions.
|
3
|
+
why: Strings cannot be comfortable enough ;)
|
4
|
+
methods:
|
5
|
+
String#^: "'Yes vs No'^3 # => 'Yes'"
|
6
|
+
String#lchomp: "' Yes'.lchomp # => 'Yes'"
|
7
|
+
String#lchomp!: "# mutable lchomp version"
|
8
|
+
String#ords: "'Hallo'.ords # => [72, 97, 108, 108, 111]"
|
9
|
+
package: default
|
10
|
+
versions:
|
11
|
+
- 1.9
|
12
|
+
- 1.8
|
13
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
unary:
|
2
|
+
summary: Easy conversion between strings and symbols.
|
3
|
+
why: "Sometimes, you do not care if you get a ⇧String⇧ or ⇧Symbol⇧ as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators ⇧String#-@⇧ and ⇧Symbol#+@⇧"
|
4
|
+
methods:
|
5
|
+
String#+@: "+'was_string' # => 'was_string'"
|
6
|
+
String#-@: "-'was_string' # => :was_string"
|
7
|
+
Symbol#+@: "+:was_symbol # => 'was_symbol'"
|
8
|
+
Symbol#-@: "-:was_symbol # => :was_symbol"
|
9
|
+
package: default
|
10
|
+
info:
|
11
|
+
- "Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)"
|
12
|
+
versions:
|
13
|
+
- 1.9
|
14
|
+
- 1.8 (+:literal not possible)
|
15
|
+
|
@@ -0,0 +1,1286 @@
|
|
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 1</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
|
+
border:1px solid #005;
|
138
|
+
# background:#111;
|
139
|
+
width:98%;
|
140
|
+
|
141
|
+
# background-color: #232323;
|
142
|
+
# border: 1px solid black;
|
143
|
+
# font-family: 'Courier New', 'Terminal', monospace;
|
144
|
+
# color: #E6E0DB;
|
145
|
+
padding: 3px 5px;
|
146
|
+
# margin-right:1em;
|
147
|
+
overflow: auto;
|
148
|
+
font-size: 12px;
|
149
|
+
background-color: #f8f8f8;
|
150
|
+
border: 1px solid silver;
|
151
|
+
font-family: 'Courier New', 'Terminal', monospace;
|
152
|
+
color: #000;
|
153
|
+
}
|
154
|
+
|
155
|
+
#/*railscasts*/
|
156
|
+
#.source .an { color:#E7BE69 } /* html attribute */
|
157
|
+
#.source .c { color:#BC9358; font-style: italic; } /* comment */
|
158
|
+
#.source .ch { color:#509E4F } /* escaped character */
|
159
|
+
#.source .cl { color:#FFF } /* class */
|
160
|
+
#.source .co { color:#FFF } /* constant */
|
161
|
+
#.source .fl { color:#A4C260 } /* float */
|
162
|
+
#.source .fu { color:#FFC56D } /* function */
|
163
|
+
#.source .gv { color:#D0CFFE } /* global variable */
|
164
|
+
#.source .i { color:#A4C260 } /* integer */
|
165
|
+
#.source .il { background:#151515 } /* inline code */
|
166
|
+
#.source .iv { color:#D0CFFE } /* instance variable */
|
167
|
+
#.source .pp { color:#E7BE69 } /* doctype */
|
168
|
+
#.source .r { color:#CB7832 } /* keyword */
|
169
|
+
#.source .rx { color:#A4C260 } /* regex */
|
170
|
+
#.source .s { color:#A4C260 } /* string */
|
171
|
+
#.source .sy { color:#6C9CBD } /* symbol */
|
172
|
+
#.source .ta { color:#E7BE69 } /* html tag */
|
173
|
+
#.source .pc { color:#6C9CBD } /* boolean */
|
174
|
+
|
175
|
+
# http://coderay.rubychan.de/
|
176
|
+
.source pre { margin: 0px; }
|
177
|
+
|
178
|
+
span.source { white-space: pre; border: 0px; padding: 2px; }
|
179
|
+
|
180
|
+
table.source { border-collapse: collapse; width: 100%; padding: 2px; }
|
181
|
+
table.source td { padding: 2px 4px; vertical-align: top; }
|
182
|
+
|
183
|
+
.source .line_numbers, .source .no {
|
184
|
+
background-color: #def;
|
185
|
+
color: gray;
|
186
|
+
text-align: right;
|
187
|
+
}
|
188
|
+
.source .line_numbers a:target, .source .no a:target { color: blue; }
|
189
|
+
.source .line_numbers .highlighted, .source .no .highlighted { color: red; }
|
190
|
+
.source .no { padding: 0px 4px; }
|
191
|
+
.source .code { width: 100%; }
|
192
|
+
.source .code pre { overflow: auto; }
|
193
|
+
|
194
|
+
.source .debug { color:white ! important; background:blue ! important; }
|
195
|
+
|
196
|
+
.source .an { color:#007 }
|
197
|
+
.source .at { color:#f08 }
|
198
|
+
.source .av { color:#700 }
|
199
|
+
.source .bi { color:#509; font-weight:bold }
|
200
|
+
.source .c { color:#888; }
|
201
|
+
.source .c .dl { color:#444; }
|
202
|
+
.source .c .ch { color:#444; }
|
203
|
+
|
204
|
+
.source .ch { color:#04D }
|
205
|
+
.source .ch .k { color:#04D }
|
206
|
+
.source .ch .dl { color:#039 }
|
207
|
+
|
208
|
+
.source .cl { color:#B06; font-weight:bold }
|
209
|
+
.source .cm { color:#A08; font-weight:bold }
|
210
|
+
.source .co { color:#036; font-weight:bold }
|
211
|
+
.source .cr { color:#0A0 }
|
212
|
+
.source .cv { color:#369 }
|
213
|
+
.source .de { color:#B0B; }
|
214
|
+
.source .df { color:#099; font-weight:bold }
|
215
|
+
.source .di { color:#088; font-weight:bold }
|
216
|
+
.source .dl { color:black }
|
217
|
+
.source .do { color:#970 }
|
218
|
+
.source .dt { color:#34b }
|
219
|
+
.source .ds { color:#D42; font-weight:bold }
|
220
|
+
.source .e { color:#666; font-weight:bold }
|
221
|
+
.source .en { color:#800; font-weight:bold }
|
222
|
+
.source .er { color:#F00; background-color:#FAA }
|
223
|
+
.source .ex { color:#C00; font-weight:bold }
|
224
|
+
.source .fl { color:#60E; font-weight:bold }
|
225
|
+
.source .fu { color:#06B; font-weight:bold }
|
226
|
+
.source .gv { color:#d70; font-weight:bold }
|
227
|
+
.source .hx { color:#058; font-weight:bold }
|
228
|
+
.source .i { color:#00D; font-weight:bold }
|
229
|
+
.source .ic { color:#B44; font-weight:bold }
|
230
|
+
|
231
|
+
.source .il { background-color: hsla(0,0%,0%,0.1); color: black }
|
232
|
+
.source .il .idl { font-weight: bold; color: #666 }
|
233
|
+
.source .idl { font-weight: bold; background-color: hsla(0,0%,0%,0.1); color: #666; }
|
234
|
+
|
235
|
+
.source .im { color:#f00; }
|
236
|
+
.source .in { color:#B2B; font-weight:bold }
|
237
|
+
.source .iv { color:#33B }
|
238
|
+
.source .la { color:#970; font-weight:bold }
|
239
|
+
.source .lv { color:#963 }
|
240
|
+
.source .ns { color:#707; font-weight:bold }
|
241
|
+
.source .oc { color:#40E; font-weight:bold }
|
242
|
+
.source .op { }
|
243
|
+
.source .pc { color:#058; font-weight:bold }
|
244
|
+
.source .pd { color:#369; font-weight:bold }
|
245
|
+
.source .pp { color:#579; }
|
246
|
+
.source .ps { color:#00C; font-weight:bold }
|
247
|
+
.source .pt { color:#074; font-weight:bold }
|
248
|
+
.source .r, .kw { color:#080; font-weight:bold }
|
249
|
+
|
250
|
+
.source .ke { color: #808; }
|
251
|
+
.source .ke .dl { color: #606; }
|
252
|
+
.source .ke .ch { color: #80f; }
|
253
|
+
.source .vl { color: #088; }
|
254
|
+
|
255
|
+
.source .rx { background-color:hsla(300,100%,50%,0.1); }
|
256
|
+
.source .rx .k { color:#808 }
|
257
|
+
.source .rx .dl { color:#404 }
|
258
|
+
.source .rx .mod { color:#C2C }
|
259
|
+
.source .rx .fu { color:#404; font-weight: bold }
|
260
|
+
|
261
|
+
.source .s { background-color:hsla(0,100%,50%,0.1); }
|
262
|
+
.source .s .k { color: #D20; }
|
263
|
+
.source .s .ch { color: #b0b; }
|
264
|
+
.source .s .dl { color: #710; }
|
265
|
+
|
266
|
+
.source .sh { background-color:hsla(120,100%,50%,0.1); }
|
267
|
+
.source .sh .k { color:#2B2 }
|
268
|
+
.source .sh .dl { color:#161 }
|
269
|
+
|
270
|
+
.source .sy { color:#A60 }
|
271
|
+
.source .sy .k { color:#A60 }
|
272
|
+
.source .sy .dl { color:#630 }
|
273
|
+
|
274
|
+
.source .ta { color:#070 }
|
275
|
+
.source .ts { color:#D70; font-weight:bold }
|
276
|
+
.source .ty { color:#339; font-weight:bold }
|
277
|
+
.source .v { color:#036 }
|
278
|
+
.source .xt { color:#444 }
|
279
|
+
|
280
|
+
.source .ins { background: hsla(120,100%,50%,0.2) }
|
281
|
+
.source .del { background: hsla(0,100%,50%,0.2) }
|
282
|
+
.source .chg { color: #aaf; background: #007; }
|
283
|
+
.source .head { color: #f8f; background: #505 }
|
284
|
+
.source .head .filename { color: white; }
|
285
|
+
|
286
|
+
.source .ins .eye { background-color: hsla(120,100%,50%,0.2) }
|
287
|
+
.source .del .eye { background-color: hsla(0,100%,50%,0.2) }
|
288
|
+
|
289
|
+
.source .ins .ins { color: #080; background:transparent; font-weight:bold }
|
290
|
+
.source .del .del { color: #800; background:transparent; font-weight:bold }
|
291
|
+
.source .chg .chg { color: #66f; }
|
292
|
+
.source .head .head { color: #f4f; }
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
</style>
|
297
|
+
</head>
|
298
|
+
<body>
|
299
|
+
<div id="world">
|
300
|
+
|
301
|
+
<h1>Ruby Zucker 1</h1>
|
302
|
+
<h2>What is it?</h2>
|
303
|
+
<p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>). This gem adds syntactical sugar in the form of independent, lightweight 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>
|
304
|
+
|
305
|
+
<h2>Install</h2>
|
306
|
+
<p class="text">
|
307
|
+
<code class="scode">gem install zucker # might need sudo</code>
|
308
|
+
</p>
|
309
|
+
|
310
|
+
<h2>Usage / Organisation</h2>
|
311
|
+
<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:
|
312
|
+
<code class="scode">require 'zucker/default'</code>
|
313
|
+
and
|
314
|
+
<code class="scode">require 'zucker/debug'</code>
|
315
|
+
|
316
|
+
Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
|
317
|
+
|
318
|
+
<code class="scode">require 'zucker/egonil'</code>
|
319
|
+
</p>
|
320
|
+
|
321
|
+
<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:
|
322
|
+
<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>
|
323
|
+
|
324
|
+
<h2 title="require 'zucker/all'">Cubes</h2>
|
325
|
+
<div class="cubes">
|
326
|
+
<h3 title="require 'zucker/D'">D</h3>
|
327
|
+
<table class="cube_table"
|
328
|
+
id="D_cube"
|
329
|
+
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>
|
330
|
+
<span class="c"># ...outputs 4 lines with the inspected objects</span>
|
331
|
+
<span class="c"># => (result)</span>
|
332
|
+
|
333
|
+
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
|
334
|
+
<span class="c"># outputs -0.916521547915634</span>
|
335
|
+
<span class="c"># => 20.0834784520844</span>
|
336
|
+
|
337
|
+
name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
|
338
|
+
<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>}
|
339
|
+
<span class="c"># outputs "The length is: 5"</span>
|
340
|
+
<span class="c"># => 'Hello Earth'</span>
|
341
|
+
</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>
|
342
|
+
<pre class="source" id="D_spec">require <span class="s"><span class="dl">'</span><span class="k">D</span><span class="dl">'</span></span>
|
343
|
+
|
344
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span><span class="dl">'</span></span> <span class="r">do</span>
|
345
|
+
|
346
|
+
let <span class="sy">:a</span> <span class="r">do</span>
|
347
|
+
[ <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> ]
|
348
|
+
<span class="r">end</span>
|
349
|
+
|
350
|
+
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>
|
351
|
+
a.each{ |e|
|
352
|
+
(e.<span class="co">D</span>).should == e
|
353
|
+
}
|
354
|
+
<span class="r">end</span>
|
355
|
+
|
356
|
+
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>
|
357
|
+
<span class="c"># a.each{ |e|</span>
|
358
|
+
<span class="c"># (e.D).should == e</span>
|
359
|
+
<span class="c"># }</span>
|
360
|
+
<span class="r">end</span>
|
361
|
+
|
362
|
+
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>
|
363
|
+
a.each{ |e|
|
364
|
+
(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
|
365
|
+
}
|
366
|
+
<span class="r">end</span>
|
367
|
+
<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>
|
368
|
+
<pre class="source" id="D_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
369
|
+
<span class="r">def</span> <span class="fu">D</span>(*args, &block)
|
370
|
+
<span class="r">if</span> args.empty?
|
371
|
+
tap{
|
372
|
+
<span class="r">if</span> block_given?
|
373
|
+
puts <span class="r">yield</span> <span class="pc">self</span>
|
374
|
+
<span class="r">else</span>
|
375
|
+
puts <span class="pc">self</span>.inspect
|
376
|
+
<span class="r">end</span>
|
377
|
+
}
|
378
|
+
<span class="r">else</span>
|
379
|
+
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>
|
380
|
+
<span class="c"># eval ...</span>
|
381
|
+
<span class="r">end</span>
|
382
|
+
<span class="r">end</span>
|
383
|
+
<span class="r">end</span>
|
384
|
+
|
385
|
+
<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/aliases'">aliases</h3>
|
386
|
+
<table class="cube_table"
|
387
|
+
id="aliases_cube"
|
388
|
+
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>
|
389
|
+
<pre class="source" id="aliases_spec">require <span class="s"><span class="dl">'</span><span class="k">aliases</span><span class="dl">'</span></span>
|
390
|
+
|
391
|
+
describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</span><span class="dl">'</span></span> <span class="r">do</span>
|
392
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create these aliases [ sorry, no real spec for this one :P ] </span><span class="dl">'</span></span> <span class="r">do</span>
|
393
|
+
<span class="c"># alias is_an? is_a?</span>
|
394
|
+
|
395
|
+
<span class="c"># module Enumerable</span>
|
396
|
+
<span class="c"># alias with zip</span>
|
397
|
+
<span class="c"># alias % zip</span>
|
398
|
+
<span class="c"># end</span>
|
399
|
+
|
400
|
+
<span class="c"># class Array</span>
|
401
|
+
<span class="c"># alias ** product</span>
|
402
|
+
<span class="c"># end</span>
|
403
|
+
|
404
|
+
<span class="c"># class Hash</span>
|
405
|
+
<span class="c"># alias + merge</span>
|
406
|
+
<span class="c"># end</span>
|
407
|
+
|
408
|
+
<span class="c"># class Binding</span>
|
409
|
+
<span class="c"># #alias [] eval</span>
|
410
|
+
<span class="c"># def [](expr)</span>
|
411
|
+
<span class="c"># self.eval "#{expr}"</span>
|
412
|
+
<span class="c"># end</span>
|
413
|
+
<span class="c"># end</span>
|
414
|
+
|
415
|
+
<span class="r">end</span>
|
416
|
+
|
417
|
+
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>
|
418
|
+
<span class="co">Infinity</span>.finite?.should == <span class="pc">false</span>
|
419
|
+
<span class="co">NaN</span>.nan?.should == <span class="pc">true</span>
|
420
|
+
<span class="r">end</span>
|
421
|
+
<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>
|
422
|
+
<pre class="source" id="aliases_source"><span class="r">alias</span> <span class="fu">is_an?</span> <span class="fu">is_a?</span>
|
423
|
+
|
424
|
+
<span class="r">module</span> <span class="cl">Enumerable</span>
|
425
|
+
<span class="r">alias</span> <span class="fu">with</span> <span class="fu">zip</span>
|
426
|
+
<span class="r">alias</span> <span class="fu">%</span> <span class="fu">zip</span>
|
427
|
+
<span class="r">end</span>
|
428
|
+
|
429
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
430
|
+
<span class="r">alias</span> <span class="fu">**</span> <span class="fu">product</span>
|
431
|
+
<span class="r">end</span>
|
432
|
+
|
433
|
+
<span class="r">class</span> <span class="cl">Hash</span>
|
434
|
+
<span class="r">alias</span> <span class="fu">+</span> <span class="fu">merge</span>
|
435
|
+
<span class="r">end</span>
|
436
|
+
|
437
|
+
<span class="r">class</span> <span class="cl">Binding</span>
|
438
|
+
<span class="c">#alias [] eval</span>
|
439
|
+
<span class="r">def</span> <span class="fu">[]</span>(expr)
|
440
|
+
<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>
|
441
|
+
<span class="r">end</span>
|
442
|
+
<span class="r">end</span>
|
443
|
+
|
444
|
+
<span class="c"># constants - who would use these in real-world code for other things?</span>
|
445
|
+
<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>
|
446
|
+
<span class="co">NaN</span> = <span class="fl">0.0</span> / <span class="fl">0.0</span>
|
447
|
+
|
448
|
+
<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>
|
449
|
+
<table class="cube_table"
|
450
|
+
id="array_cube"
|
451
|
+
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 an alias for <code>inject(:+)</code>, but it is just 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>
|
452
|
+
<pre class="source" id="array_spec">require <span class="s"><span class="dl">'</span><span class="k">array</span><span class="dl">'</span></span>
|
453
|
+
|
454
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
455
|
+
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>
|
456
|
+
a = [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>]
|
457
|
+
b = [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>]
|
458
|
+
(a^b).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">5</span>,<span class="i">6</span>]
|
459
|
+
<span class="r">end</span>
|
460
|
+
<span class="r">end</span>
|
461
|
+
|
462
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#sum</span><span class="dl">'</span></span> <span class="r">do</span>
|
463
|
+
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>
|
464
|
+
[<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>
|
465
|
+
<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>
|
466
|
+
<span class="r">end</span>
|
467
|
+
<span class="r">end</span>
|
468
|
+
|
469
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</span><span class="dl">'</span></span> <span class="r">do</span>
|
470
|
+
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>
|
471
|
+
[<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>
|
472
|
+
<span class="r">end</span>
|
473
|
+
<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>
|
474
|
+
<pre class="source" id="array_source"><span class="r">class</span> <span class="cl">Array</span>
|
475
|
+
<span class="r">def</span> <span class="fu">^</span>(other) <span class="c"># TODO: more efficient</span>
|
476
|
+
(<span class="pc">self</span> - other) +
|
477
|
+
(other - <span class="pc">self</span>)
|
478
|
+
<span class="r">end</span>
|
479
|
+
|
480
|
+
<span class="r">def</span> <span class="fu">sum</span> <span class="c"># sry, you just need this one too often</span>
|
481
|
+
inject <span class="sy">:+</span>
|
482
|
+
<span class="r">end</span>
|
483
|
+
|
484
|
+
<span class="r">def</span> <span class="fu">chrs</span>
|
485
|
+
<span class="pc">self</span>.pack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
486
|
+
<span class="r">end</span>
|
487
|
+
<span class="r">end</span>
|
488
|
+
|
489
|
+
<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>
|
490
|
+
<table class="cube_table"
|
491
|
+
id="array2proc_cube"
|
492
|
+
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">:to_s</span>, <span class="i">2</span>] ) <span class="c"># => ["1", "10", "11", "100"]</span></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>.</td></tr><tr><th>Specification</th> <td> <span id="show_array2proc_spec">(<a href="javascript:show('array2proc_spec')">show</a>)</span>
|
493
|
+
<pre class="source" id="array2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">array2proc</span><span class="dl">'</span></span>
|
494
|
+
|
495
|
+
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>
|
496
|
+
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>
|
497
|
+
[<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>]
|
498
|
+
<span class="r">end</span>
|
499
|
+
<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>
|
500
|
+
<pre class="source" id="array2proc_source"><span class="r">class</span> <span class="cl">Array</span>
|
501
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
502
|
+
<span class="co">Proc</span>.new{ |obj|
|
503
|
+
obj.send *<span class="pc">self</span>
|
504
|
+
}
|
505
|
+
<span class="r">end</span>
|
506
|
+
<span class="r">end</span>
|
507
|
+
|
508
|
+
<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>
|
509
|
+
<table class="cube_table"
|
510
|
+
id="binding_cube"
|
511
|
+
title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds a verbose Binding#inspect</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>
|
512
|
+
<pre class="source" id="binding_spec">require <span class="s"><span class="dl">'</span><span class="k">binding</span><span class="dl">'</span></span>
|
513
|
+
|
514
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Binding#inspect</span><span class="dl">'</span></span> <span class="r">do</span>
|
515
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output the current environment</span><span class="dl">'</span></span>
|
516
|
+
<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>
|
517
|
+
<pre class="source" id="binding_source"><span class="r">class</span> <span class="cl">Binding</span>
|
518
|
+
<span class="r">def</span> <span class="fu">inspect</span>
|
519
|
+
put_vars = lambda { |array|
|
520
|
+
<span class="r">if</span> array.empty?
|
521
|
+
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
522
|
+
<span class="r">else</span>
|
523
|
+
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>
|
524
|
+
<span class="r">end</span>
|
525
|
+
}
|
526
|
+
|
527
|
+
<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">
|
528
|
+
local vars
|
529
|
+
</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">
|
530
|
+
(instance vars)
|
531
|
+
</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">
|
532
|
+
self
|
533
|
+
- </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">
|
534
|
+
block_given?
|
535
|
+
- </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>
|
536
|
+
|
537
|
+
<span class="r">end</span>
|
538
|
+
<span class="r">end</span>
|
539
|
+
|
540
|
+
<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>
|
541
|
+
<table class="cube_table"
|
542
|
+
id="blank_cube"
|
543
|
+
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>Further information</th> <td>See ActiveSupport</td></tr><tr><th>Specification</th> <td> <span id="show_blank_spec">(<a href="javascript:show('blank_spec')">show</a>)</span>
|
544
|
+
<pre class="source" id="blank_spec">require <span class="s"><span class="dl">'</span><span class="k">blank</span><span class="dl">'</span></span>
|
545
|
+
|
546
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?</span><span class="dl">'</span></span> <span class="r">do</span>
|
547
|
+
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>
|
548
|
+
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> ]
|
549
|
+
|
550
|
+
blank_values.each{ |blank|
|
551
|
+
blank.blank?.should == <span class="pc">true</span>
|
552
|
+
}
|
553
|
+
<span class="r">end</span>
|
554
|
+
|
555
|
+
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>
|
556
|
+
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> } ]
|
557
|
+
|
558
|
+
present_values.each{ |present|
|
559
|
+
present.blank?.should == <span class="pc">false</span>
|
560
|
+
}
|
561
|
+
<span class="r">end</span>
|
562
|
+
<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>
|
563
|
+
<pre class="source" id="blank_source"><span class="r">class</span> <span class="cl">Object</span>
|
564
|
+
<span class="r">def</span> <span class="fu">blank?</span>
|
565
|
+
<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>
|
566
|
+
<span class="r">end</span>
|
567
|
+
|
568
|
+
<span class="r">def</span> <span class="fu">present?</span>
|
569
|
+
!blank?
|
570
|
+
<span class="r">end</span>
|
571
|
+
<span class="r">end</span>
|
572
|
+
|
573
|
+
|
574
|
+
{ <span class="c"># what to do # for which classes</span>
|
575
|
+
lambda{ <span class="pc">true</span> } => [<span class="co">FalseClass</span>, <span class="co">NilClass</span>],
|
576
|
+
lambda{ <span class="pc">false</span> } => [<span class="co">TrueClass</span>, <span class="co">Numeric</span>],
|
577
|
+
lambda{ empty? } => [<span class="co">Array</span>, <span class="co">Hash</span>],
|
578
|
+
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>],
|
579
|
+
lambda{ <span class="pc">self</span> == <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> } => [<span class="co">Regexp</span>],
|
580
|
+
|
581
|
+
}.each{ |action, klass_array|
|
582
|
+
klass_array.each{ |klass|
|
583
|
+
klass.send <span class="sy">:define_method</span>, <span class="sy">:blank?</span>, &action
|
584
|
+
}
|
585
|
+
}
|
586
|
+
|
587
|
+
<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>
|
588
|
+
<table class="cube_table"
|
589
|
+
id="class2proc_cube"
|
590
|
+
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>
|
591
|
+
<pre class="source" id="class2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">class2proc</span><span class="dl">'</span></span>
|
592
|
+
require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
|
593
|
+
|
594
|
+
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>
|
595
|
+
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>
|
596
|
+
[ [<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>] ]
|
597
|
+
<span class="r">end</span>
|
598
|
+
<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>
|
599
|
+
<pre class="source" id="class2proc_source"><span class="r">class</span> <span class="cl">Class</span>
|
600
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
601
|
+
<span class="co">Proc</span>.new <span class="r">do</span> |*args|
|
602
|
+
<span class="pc">self</span>.new *args
|
603
|
+
<span class="r">end</span>
|
604
|
+
<span class="r">end</span>
|
605
|
+
<span class="r">end</span>
|
606
|
+
|
607
|
+
<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>
|
608
|
+
<table class="cube_table"
|
609
|
+
id="egonil_cube"
|
610
|
+
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>
|
611
|
+
<span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist
|
612
|
+
<span class="r">end</span> <span class="c"># => nil</span>
|
613
|
+
|
614
|
+
nn(<span class="i">5</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
615
|
+
<span class="c"># => 5</span>
|
616
|
+
</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>
|
617
|
+
<pre class="source" id="egonil_spec">require <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span>
|
618
|
+
|
619
|
+
describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span> <span class="r">do</span>
|
620
|
+
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>
|
621
|
+
proc <span class="r">do</span>
|
622
|
+
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
623
|
+
<span class="r">end</span>.should_not raise_exception
|
624
|
+
<span class="r">end</span>
|
625
|
+
|
626
|
+
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>
|
627
|
+
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>
|
628
|
+
<span class="r">end</span>
|
629
|
+
|
630
|
+
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>
|
631
|
+
proc <span class="r">do</span>
|
632
|
+
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
633
|
+
<span class="r">end</span>.should_not raise_exception
|
634
|
+
|
635
|
+
proc <span class="r">do</span>
|
636
|
+
<span class="pc">nil</span>.a_method
|
637
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
638
|
+
<span class="r">end</span>
|
639
|
+
|
640
|
+
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>
|
641
|
+
proc <span class="r">do</span>
|
642
|
+
egonil{ <span class="i">5</span>.a_method }
|
643
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
644
|
+
<span class="r">end</span>
|
645
|
+
<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>
|
646
|
+
<pre class="source" id="egonil_source"><span class="r">def</span> <span class="fu">egonil</span>(nil_value = <span class="pc">nil</span>)
|
647
|
+
<span class="r">yield</span>
|
648
|
+
<span class="r">rescue</span> <span class="co">NoMethodError</span> => e
|
649
|
+
<span class="r">if</span> e.message =~ <span class="rx"><span class="dl">/</span><span class="k">NilClass$</span><span class="dl">/</span></span>
|
650
|
+
nil_value
|
651
|
+
<span class="r">else</span>
|
652
|
+
raise <span class="co">NoMethodError</span>
|
653
|
+
<span class="r">end</span>
|
654
|
+
<span class="r">end</span>
|
655
|
+
|
656
|
+
<span class="r">alias</span> <span class="fu">nn</span> <span class="fu">egonil</span>
|
657
|
+
|
658
|
+
<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>
|
659
|
+
<table class="cube_table"
|
660
|
+
id="enumerable_cube"
|
661
|
+
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' mash</td></tr><tr><th>Specification</th> <td> <span id="show_enumerable_spec">(<a href="javascript:show('enumerable_spec')">show</a>)</span>
|
662
|
+
<pre class="source" id="enumerable_spec">require <span class="s"><span class="dl">'</span><span class="k">enumerable</span><span class="dl">'</span></span>
|
663
|
+
|
664
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mash</span><span class="dl">'</span></span> <span class="r">do</span>
|
665
|
+
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>
|
666
|
+
[<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>,}
|
667
|
+
<span class="r">end</span>
|
668
|
+
<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>
|
669
|
+
<pre class="source" id="enumerable_source"><span class="r">module</span> <span class="cl">Enumerable</span>
|
670
|
+
<span class="r">def</span> <span class="fu">mash</span>
|
671
|
+
ret = {}
|
672
|
+
each{ |kv|
|
673
|
+
ret.store *( <span class="r">yield</span>(kv)[<span class="i">0</span>,<span class="i">2</span>] )
|
674
|
+
}
|
675
|
+
ret
|
676
|
+
<span class="r">end</span>
|
677
|
+
<span class="r">end</span>
|
678
|
+
|
679
|
+
<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>
|
680
|
+
<table class="cube_table"
|
681
|
+
id="hash_cube"
|
682
|
+
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></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>
|
683
|
+
<pre class="source" id="hash_spec">require <span class="s"><span class="dl">'</span><span class="k">hash</span><span class="dl">'</span></span>
|
684
|
+
|
685
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash.zip</span><span class="dl">'</span></span> <span class="r">do</span>
|
686
|
+
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>
|
687
|
+
<span class="r">end</span>
|
688
|
+
|
689
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash#<<</span><span class="dl">'</span></span> <span class="r">do</span>
|
690
|
+
it <span class="s"><span class="dl">'</span><span class="k">should append new elements to the hash</span><span class="dl">'</span></span>
|
691
|
+
<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>
|
692
|
+
<pre class="source" id="hash_source"><span class="r">class</span> <span class="cl">Hash</span>
|
693
|
+
<span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
|
694
|
+
<span class="co">Hash</span>[ *[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].zip( [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] ).flatten ]
|
695
|
+
<span class="r">end</span>
|
696
|
+
|
697
|
+
<span class="r">def</span> <span class="fu"><<</span>(other)
|
698
|
+
<span class="r">case</span>
|
699
|
+
<span class="r">when</span> other.is_a?(<span class="co">Hash</span>)
|
700
|
+
merge! other
|
701
|
+
<span class="r">when</span> other.respond_to?(<span class="sy">:to_splat</span>)
|
702
|
+
merge! <span class="co">Hash</span>[*other]
|
703
|
+
<span class="r">else</span>
|
704
|
+
raise <span class="co">TypeError</span>, <span class="s"><span class="dl">'</span><span class="k">can only append other Hashs and Enumerables</span><span class="dl">'</span></span>
|
705
|
+
<span class="r">end</span>
|
706
|
+
<span class="r">end</span>
|
707
|
+
<span class="r">end</span>
|
708
|
+
|
709
|
+
<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>
|
710
|
+
<table class="cube_table"
|
711
|
+
id="hash2proc_cube"
|
712
|
+
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(&{
|
713
|
+
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>},
|
714
|
+
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
715
|
+
}) <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>
|
716
|
+
<pre class="source" id="hash2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">hash2proc</span><span class="dl">'</span></span>
|
717
|
+
|
718
|
+
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>
|
719
|
+
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>
|
720
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&{
|
721
|
+
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
722
|
+
<span class="c"># 3 => [:to_s, 2] # "11" => if array2proc is used</span>
|
723
|
+
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>}
|
724
|
+
}).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>]
|
725
|
+
<span class="r">end</span>
|
726
|
+
<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>
|
727
|
+
<pre class="source" id="hash2proc_source"><span class="r">class</span> <span class="cl">Hash</span>
|
728
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
729
|
+
<span class="co">Proc</span>.new{ |obj|
|
730
|
+
<span class="r">if</span> <span class="pc">self</span>.member? obj
|
731
|
+
<span class="pc">self</span>[obj].to_proc.call obj
|
732
|
+
<span class="r">else</span>
|
733
|
+
obj
|
734
|
+
<span class="r">end</span>
|
735
|
+
}
|
736
|
+
<span class="r">end</span>
|
737
|
+
<span class="r">end</span>
|
738
|
+
|
739
|
+
<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/iterate'">iterate</h3>
|
740
|
+
<table class="cube_table"
|
741
|
+
id="iterate_cube"
|
742
|
+
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|
|
743
|
+
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>
|
744
|
+
<span class="r">end</span>
|
745
|
+
<span class="c"># outputs</span>
|
746
|
+
<span class="c"># 1,3</span>
|
747
|
+
<span class="c"># 2,4</span>
|
748
|
+
<span class="c"># ,5</span>
|
749
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_spec">(<a href="javascript:show('iterate_spec')">show</a>)</span>
|
750
|
+
<pre class="source" id="iterate_spec">require <span class="s"><span class="dl">'</span><span class="k">iterate</span><span class="dl">'</span></span>
|
751
|
+
|
752
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
|
753
|
+
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>
|
754
|
+
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>
|
755
|
+
let <span class="sy">:res</span> <span class="r">do</span> <span class="co">Hash</span>.new {[]} <span class="r">end</span>
|
756
|
+
|
757
|
+
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>
|
758
|
+
iterate a <span class="r">do</span> |ele|
|
759
|
+
res[<span class="sy">:iter</span>] << ele
|
760
|
+
<span class="r">end</span>
|
761
|
+
|
762
|
+
a.each <span class="r">do</span> |ele|
|
763
|
+
res[<span class="sy">:each</span>] << ele
|
764
|
+
<span class="r">end</span>
|
765
|
+
|
766
|
+
res[<span class="sy">:iter</span>].should == res[<span class="sy">:each</span>]
|
767
|
+
<span class="r">end</span>
|
768
|
+
|
769
|
+
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>
|
770
|
+
res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
|
771
|
+
res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
|
772
|
+
res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
|
773
|
+
|
774
|
+
|
775
|
+
iterate a, b <span class="r">do</span> |e,f|
|
776
|
+
res[<span class="sy">:iter_a_b</span>] << [e, f]
|
777
|
+
<span class="c"># p res[:iter_a_b], e, f</span>
|
778
|
+
<span class="r">end</span>
|
779
|
+
|
780
|
+
res[<span class="sy">:iter_a_b</span>].should == [
|
781
|
+
[<span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>],
|
782
|
+
[<span class="i">2</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>],
|
783
|
+
[<span class="i">3</span>, <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>],
|
784
|
+
[<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>],
|
785
|
+
]
|
786
|
+
|
787
|
+
iterate b, a <span class="r">do</span> |e,f|
|
788
|
+
res[<span class="sy">:iter_b_a</span>] << [e, f]
|
789
|
+
<span class="r">end</span>
|
790
|
+
|
791
|
+
res[<span class="sy">:iter_b_a</span>].should == [
|
792
|
+
[<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, <span class="i">1</span>],
|
793
|
+
[<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>, <span class="i">2</span>],
|
794
|
+
[<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>, <span class="i">3</span>],
|
795
|
+
[<span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>, <span class="pc">nil</span>],
|
796
|
+
]
|
797
|
+
|
798
|
+
<span class="r">end</span>
|
799
|
+
|
800
|
+
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>
|
801
|
+
<span class="r">end</span>
|
802
|
+
<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>
|
803
|
+
<pre class="source" id="iterate_source"><span class="r">def</span> <span class="fu">iterate</span>(*params)
|
804
|
+
<span class="c"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
|
805
|
+
|
806
|
+
first = params.shift
|
807
|
+
<span class="r">if</span> params.empty? <span class="c"># single param - like each</span>
|
808
|
+
<span class="r">if</span> block_given?
|
809
|
+
first.map{|e| <span class="r">yield</span> e }
|
810
|
+
<span class="r">else</span>
|
811
|
+
first.map
|
812
|
+
<span class="r">end</span>
|
813
|
+
<span class="r">else</span>
|
814
|
+
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>
|
815
|
+
obj = padded_first.zip *params
|
816
|
+
<span class="r">if</span> block_given?
|
817
|
+
obj.map{|es| <span class="r">yield</span> *es }
|
818
|
+
<span class="r">else</span>
|
819
|
+
obj.map.to_enum
|
820
|
+
<span class="r">end</span>
|
821
|
+
<span class="r">end</span>
|
822
|
+
<span class="r">end</span>
|
823
|
+
|
824
|
+
<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>
|
825
|
+
<table class="cube_table"
|
826
|
+
id="ivars_cube"
|
827
|
+
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>)
|
828
|
+
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
829
|
+
|
830
|
+
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
831
|
+
instance_variables_from params <span class="c"># # assigns @c and @d</span>
|
832
|
+
<span class="r">end</span>
|
833
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_spec">(<a href="javascript:show('ivars_spec')">show</a>)</span>
|
834
|
+
<pre class="source" id="ivars_spec">require <span class="s"><span class="dl">'</span><span class="k">ivars</span><span class="dl">'</span></span>
|
835
|
+
|
836
|
+
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>
|
837
|
+
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>
|
838
|
+
<span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
839
|
+
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
840
|
+
|
841
|
+
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
842
|
+
ivars params <span class="c"># # assigns @c and @d</span>
|
843
|
+
<span class="r">end</span>
|
844
|
+
|
845
|
+
a_method
|
846
|
+
<span class="iv">@a</span>.should == <span class="i">1</span>
|
847
|
+
<span class="iv">@b</span>.should == <span class="i">2</span>
|
848
|
+
<span class="iv">@c</span>.should == <span class="i">3</span>
|
849
|
+
<span class="iv">@d</span>.should == <span class="i">4</span>
|
850
|
+
|
851
|
+
<span class="r">end</span>
|
852
|
+
<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>
|
853
|
+
<pre class="source" id="ivars_source"><span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
|
854
|
+
iter =
|
855
|
+
<span class="r">if</span> obj.is_a? <span class="co">Binding</span>
|
856
|
+
obj.eval(<span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span>).map{|e| [obj.eval(e), e] }
|
857
|
+
<span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
|
858
|
+
obj.map{|k,v| [v,k] }
|
859
|
+
<span class="r">else</span>
|
860
|
+
<span class="c"># elsif obj.is_a? Enumerable</span>
|
861
|
+
obj.each.with_index
|
862
|
+
<span class="r">end</span>
|
863
|
+
|
864
|
+
ret = []
|
865
|
+
iter.each{ |value, arg|
|
866
|
+
arg = arg.to_s
|
867
|
+
<span class="r">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
868
|
+
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>
|
869
|
+
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>
|
870
|
+
<span class="pc">self</span>.instance_variable_set ivar, value
|
871
|
+
<span class="r">end</span>
|
872
|
+
}
|
873
|
+
ret
|
874
|
+
<span class="r">end</span>
|
875
|
+
<span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
|
876
|
+
|
877
|
+
<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>
|
878
|
+
<table class="cube_table"
|
879
|
+
id="kernel_cube"
|
880
|
+
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>
|
881
|
+
<pre class="source" id="kernel_spec">require <span class="s"><span class="dl">'</span><span class="k">kernel</span><span class="dl">'</span></span>
|
882
|
+
|
883
|
+
describe <span class="s"><span class="dl">'</span><span class="k">activate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
884
|
+
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>
|
885
|
+
activate_warnings!
|
886
|
+
<span class="gv">$VERBOSE</span>.should == <span class="pc">true</span>
|
887
|
+
<span class="r">end</span>
|
888
|
+
<span class="r">end</span>
|
889
|
+
|
890
|
+
describe <span class="s"><span class="dl">'</span><span class="k">deactivate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
891
|
+
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>
|
892
|
+
deactivate_warnings!
|
893
|
+
<span class="gv">$VERBOSE</span>.should == <span class="pc">false</span>
|
894
|
+
<span class="r">end</span>
|
895
|
+
<span class="r">end</span>
|
896
|
+
|
897
|
+
describe <span class="s"><span class="dl">'</span><span class="k">warnings_activated?</span><span class="dl">'</span></span> <span class="r">do</span>
|
898
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return $VERBOSE</span><span class="dl">'</span></span> <span class="r">do</span>
|
899
|
+
warnings_activated?.should == <span class="gv">$VERBOSE</span>
|
900
|
+
<span class="r">end</span>
|
901
|
+
<span class="r">end</span>
|
902
|
+
|
903
|
+
describe <span class="s"><span class="dl">'</span><span class="k">library?</span><span class="dl">'</span></span> <span class="r">do</span>
|
904
|
+
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>
|
905
|
+
library?.should == ( <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span> )
|
906
|
+
<span class="r">end</span>
|
907
|
+
<span class="r">end</span>
|
908
|
+
|
909
|
+
describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!</span><span class="dl">'</span></span> <span class="r">do</span>
|
910
|
+
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>
|
911
|
+
<span class="c"># ...</span>
|
912
|
+
<span class="r">end</span>
|
913
|
+
<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>
|
914
|
+
<pre class="source" id="kernel_source"><span class="r">def</span> <span class="fu">activate_warnings!</span>
|
915
|
+
<span class="gv">$VERBOSE</span> = <span class="pc">true</span>
|
916
|
+
<span class="r">end</span>
|
917
|
+
|
918
|
+
<span class="r">def</span> <span class="fu">deactivate_warnings!</span>
|
919
|
+
<span class="gv">$VERBOSE</span> = <span class="pc">false</span>
|
920
|
+
<span class="r">end</span>
|
921
|
+
|
922
|
+
<span class="r">def</span> <span class="fu">warnings_activated?</span>
|
923
|
+
<span class="gv">$VERBOSE</span>
|
924
|
+
<span class="r">end</span>
|
925
|
+
|
926
|
+
<span class="r">def</span> <span class="fu">library?</span>
|
927
|
+
<span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span>
|
928
|
+
<span class="r">end</span>
|
929
|
+
|
930
|
+
<span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
|
931
|
+
<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>
|
932
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mcopy'">mcopy</h3>
|
933
|
+
<table class="cube_table"
|
934
|
+
id="mcopy_cube"
|
935
|
+
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>
|
936
|
+
b = a.mcopy
|
937
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_spec">(<a href="javascript:show('mcopy_spec')">show</a>)</span>
|
938
|
+
<pre class="source" id="mcopy_spec">require <span class="s"><span class="dl">'</span><span class="k">mcopy</span><span class="dl">'</span></span>
|
939
|
+
|
940
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
|
941
|
+
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>
|
942
|
+
a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
943
|
+
b = a.mcopy
|
944
|
+
b.should == a
|
945
|
+
|
946
|
+
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>
|
947
|
+
b.should_not == a
|
948
|
+
<span class="r">end</span>
|
949
|
+
<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>
|
950
|
+
<pre class="source" id="mcopy_source"><span class="r">class</span> <span class="cl">Object</span>
|
951
|
+
<span class="r">def</span> <span class="fu">mcopy</span>
|
952
|
+
<span class="co">Marshal</span>.load <span class="co">Marshal</span>.dump <span class="pc">self</span>
|
953
|
+
<span class="r">end</span>
|
954
|
+
<span class="c"># alias copy mcopy</span>
|
955
|
+
<span class="r">end</span>
|
956
|
+
|
957
|
+
<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>
|
958
|
+
<table class="cube_table"
|
959
|
+
id="mm_cube"
|
960
|
+
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 some 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>
|
961
|
+
<pre class="source" id="mm_spec">require <span class="s"><span class="dl">'</span><span class="k">mm</span><span class="dl">'</span></span>
|
962
|
+
|
963
|
+
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>
|
964
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span>
|
965
|
+
<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>
|
966
|
+
<pre class="source" id="mm_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
967
|
+
<span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
|
968
|
+
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
969
|
+
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
970
|
+
<span class="r">else</span>
|
971
|
+
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
972
|
+
|
973
|
+
eigen = <span class="pc">self</span>.singleton_methods
|
974
|
+
<span class="r">if</span> !eigen.empty?
|
975
|
+
puts <span class="sy">:Eigenclass</span> <span class="c"># sorry for not being up to date, I just love the word</span>
|
976
|
+
p <span class="pc">self</span>.singleton_methods
|
977
|
+
<span class="r">end</span>
|
978
|
+
<span class="r">end</span>
|
979
|
+
|
980
|
+
levels.times{ |level|
|
981
|
+
<span class="r">if</span> cur = klass.ancestors[level]
|
982
|
+
p cur <span class="c"># put class name</span>
|
983
|
+
p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
|
984
|
+
<span class="r">else</span>
|
985
|
+
<span class="r">break</span>
|
986
|
+
<span class="r">end</span>
|
987
|
+
}
|
988
|
+
|
989
|
+
<span class="pc">self</span> <span class="c"># or whatever</span>
|
990
|
+
<span class="r">end</span>
|
991
|
+
|
992
|
+
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
|
993
|
+
<span class="r">end</span>
|
994
|
+
|
995
|
+
<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>
|
996
|
+
<table class="cube_table"
|
997
|
+
id="regexp2proc_cube"
|
998
|
+
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>
|
999
|
+
<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>
|
1000
|
+
</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>
|
1001
|
+
<pre class="source" id="regexp2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">regexp2proc</span><span class="dl">'</span></span>
|
1002
|
+
|
1003
|
+
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>
|
1004
|
+
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>
|
1005
|
+
<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>]
|
1006
|
+
<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>]
|
1007
|
+
<span class="r">end</span>
|
1008
|
+
<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>
|
1009
|
+
<pre class="source" id="regexp2proc_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
1010
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
1011
|
+
proc <span class="r">do</span> |e|
|
1012
|
+
e.to_s[<span class="pc">self</span>]
|
1013
|
+
<span class="r">end</span>
|
1014
|
+
<span class="r">end</span>
|
1015
|
+
<span class="r">end</span>
|
1016
|
+
|
1017
|
+
<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>
|
1018
|
+
<table class="cube_table"
|
1019
|
+
id="sandbox_cube"
|
1020
|
+
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>
|
1021
|
+
<span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
|
1022
|
+
<span class="r">end</span>
|
1023
|
+
<span class="c"># everything's normal again</span>
|
1024
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_sandbox_spec">(<a href="javascript:show('sandbox_spec')">show</a>)</span>
|
1025
|
+
<pre class="source" id="sandbox_spec">require <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span>
|
1026
|
+
|
1027
|
+
describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
|
1028
|
+
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>
|
1029
|
+
it <span class="s"><span class="dl">'</span><span class="k">should run the lambda passed as first parameter for errors, if it is given</span><span class="dl">'</span></span>
|
1030
|
+
<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>
|
1031
|
+
<pre class="source" id="sandbox_source"><span class="r">def</span> <span class="fu">sandbox</span>(rescueblock=<span class="pc">nil</span>)
|
1032
|
+
<span class="co">Thread</span>.start <span class="r">do</span>
|
1033
|
+
<span class="gv">$SAFE</span> = <span class="i">4</span>
|
1034
|
+
<span class="r">yield</span>
|
1035
|
+
<span class="r">end</span>.value
|
1036
|
+
<span class="r">rescue</span> <span class="co">SecurityError</span> => e
|
1037
|
+
<span class="r">if</span> rescueblock
|
1038
|
+
rescueblock.call e
|
1039
|
+
<span class="r">else</span>
|
1040
|
+
raise e
|
1041
|
+
<span class="r">end</span>
|
1042
|
+
<span class="r">end</span>
|
1043
|
+
|
1044
|
+
<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>
|
1045
|
+
<table class="cube_table"
|
1046
|
+
id="square_brackets_for_cube"
|
1047
|
+
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>
|
1048
|
+
<span class="r">def</span> <span class="fu">initialize</span>
|
1049
|
+
<span class="iv">@var</span> = {
|
1050
|
+
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
1051
|
+
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
1052
|
+
}
|
1053
|
+
<span class="r">end</span>
|
1054
|
+
|
1055
|
+
square_brackets_for <span class="sy">:var</span> <span class="c"># creates [] and []=</span>
|
1056
|
+
<span class="c"># square_brackets_for :var, false # would create only []</span>
|
1057
|
+
<span class="r">end</span>
|
1058
|
+
|
1059
|
+
a = <span class="co">Klass</span>.new
|
1060
|
+
a[<span class="sy">:a_key</span>] <span class="c"># => 1</span>
|
1061
|
+
</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>
|
1062
|
+
<pre class="source" id="square_brackets_for_spec">require <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span>
|
1063
|
+
|
1064
|
+
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>
|
1065
|
+
|
1066
|
+
before <span class="r">do</span>
|
1067
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
1068
|
+
<span class="r">def</span> <span class="fu">initialize</span>
|
1069
|
+
<span class="iv">@var</span> = {
|
1070
|
+
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
1071
|
+
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
1072
|
+
}
|
1073
|
+
<span class="r">end</span>
|
1074
|
+
|
1075
|
+
<span class="iv">@eigenvar</span> = {
|
1076
|
+
<span class="sy">:a_key</span> => <span class="i">99</span>
|
1077
|
+
}
|
1078
|
+
<span class="r">end</span>
|
1079
|
+
<span class="r">end</span>
|
1080
|
+
|
1081
|
+
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>
|
1082
|
+
|
1083
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
1084
|
+
square_brackets_for <span class="sy">:var</span>, <span class="pc">nil</span>
|
1085
|
+
<span class="r">end</span>
|
1086
|
+
|
1087
|
+
a = <span class="co">Klass</span>.new
|
1088
|
+
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
1089
|
+
|
1090
|
+
proc <span class="r">do</span>
|
1091
|
+
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>
|
1092
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
1093
|
+
<span class="r">end</span>
|
1094
|
+
|
1095
|
+
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>
|
1096
|
+
|
1097
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
1098
|
+
square_brackets_for <span class="sy">:var</span>
|
1099
|
+
<span class="r">end</span>
|
1100
|
+
|
1101
|
+
a = <span class="co">Klass</span>.new
|
1102
|
+
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
1103
|
+
|
1104
|
+
a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
|
1105
|
+
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>
|
1106
|
+
<span class="r">end</span>
|
1107
|
+
|
1108
|
+
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>
|
1109
|
+
|
1110
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
1111
|
+
<span class="r">class</span> << <span class="cl">Klass</span>
|
1112
|
+
square_brackets_for <span class="sy">:eigenvar</span>
|
1113
|
+
<span class="r">end</span>
|
1114
|
+
<span class="r">end</span>
|
1115
|
+
|
1116
|
+
<span class="co">Klass</span>[<span class="sy">:a_key</span>].should == <span class="i">99</span>
|
1117
|
+
<span class="r">end</span>
|
1118
|
+
|
1119
|
+
<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>
|
1120
|
+
<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>)
|
1121
|
+
<span class="c"># undef [] if respond_to? :[]</span>
|
1122
|
+
<span class="c"># undef []= if respond_to? :[]=</span>
|
1123
|
+
|
1124
|
+
<span class="c">#instance_eval do</span>
|
1125
|
+
define_method <span class="sy">:[]</span> <span class="r">do</span> |key|
|
1126
|
+
(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]
|
1127
|
+
<span class="r">end</span>
|
1128
|
+
|
1129
|
+
<span class="r">if</span> assignment
|
1130
|
+
define_method <span class="sy">:[]=</span> <span class="r">do</span> |key, value|
|
1131
|
+
(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
|
1132
|
+
<span class="r">end</span>
|
1133
|
+
<span class="r">end</span>
|
1134
|
+
<span class="c">#end</span>
|
1135
|
+
|
1136
|
+
<span class="r">end</span>
|
1137
|
+
|
1138
|
+
<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>
|
1139
|
+
<table class="cube_table"
|
1140
|
+
id="string_cube"
|
1141
|
+
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>
|
1142
|
+
<pre class="source" id="string_spec">require <span class="s"><span class="dl">'</span><span class="k">string</span><span class="dl">'</span></span>
|
1143
|
+
|
1144
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1145
|
+
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>
|
1146
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1147
|
+
|
1148
|
+
(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>
|
1149
|
+
(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>
|
1150
|
+
(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>
|
1151
|
+
(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>
|
1152
|
+
(string|<span class="i">38</span>).should == <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1153
|
+
(string|<span class="i">99</span>).should == <span class="pc">nil</span>
|
1154
|
+
<span class="r">end</span>
|
1155
|
+
<span class="r">end</span>
|
1156
|
+
|
1157
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#lchomp</span><span class="dl">'</span></span> <span class="r">do</span>
|
1158
|
+
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>
|
1159
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1160
|
+
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>
|
1161
|
+
<span class="r">end</span>
|
1162
|
+
<span class="r">end</span>
|
1163
|
+
|
1164
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#ords</span><span class="dl">'</span></span> <span class="r">do</span>
|
1165
|
+
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>
|
1166
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1167
|
+
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>]
|
1168
|
+
<span class="r">end</span>
|
1169
|
+
<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>
|
1170
|
+
<pre class="source" id="string_source"><span class="r">class</span> <span class="cl">String</span>
|
1171
|
+
<span class="r">def</span> <span class="fu">^</span>(pos)
|
1172
|
+
pos = pos.to_i
|
1173
|
+
<span class="r">if</span> pos >= <span class="i">0</span>
|
1174
|
+
<span class="pc">self</span>[pos..<span class="i">-1</span>]
|
1175
|
+
<span class="r">else</span>
|
1176
|
+
<span class="pc">self</span>[<span class="i">0</span>...pos]
|
1177
|
+
<span class="r">end</span>
|
1178
|
+
<span class="r">end</span>
|
1179
|
+
|
1180
|
+
<span class="r">def</span> <span class="fu">lchomp</span>(arg=<span class="gv">$/</span>)
|
1181
|
+
<span class="pc">self</span>.reverse.chomp(arg).reverse
|
1182
|
+
<span class="r">end</span>
|
1183
|
+
|
1184
|
+
<span class="r">def</span> <span class="fu">lchomp!</span>(arg=<span class="gv">$/</span>)
|
1185
|
+
<span class="pc">self</span>.reverse.chomp!(arg).reverse
|
1186
|
+
<span class="r">end</span>
|
1187
|
+
|
1188
|
+
<span class="r">def</span> <span class="fu">ords</span>
|
1189
|
+
<span class="pc">self</span>.unpack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
1190
|
+
<span class="r">end</span>
|
1191
|
+
<span class="r">end</span>
|
1192
|
+
|
1193
|
+
<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>
|
1194
|
+
<table class="cube_table"
|
1195
|
+
id="unary_cube"
|
1196
|
+
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>
|
1197
|
+
<pre class="source" id="unary_spec">require <span class="s"><span class="dl">'</span><span class="k">unary</span><span class="dl">'</span></span>
|
1198
|
+
|
1199
|
+
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>
|
1200
|
+
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>
|
1201
|
+
+(<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>
|
1202
|
+
<span class="r">end</span>
|
1203
|
+
<span class="r">end</span>
|
1204
|
+
|
1205
|
+
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>
|
1206
|
+
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>
|
1207
|
+
-<span class="sy">:matz</span> == <span class="sy">:matz</span>
|
1208
|
+
<span class="r">end</span>
|
1209
|
+
<span class="r">end</span>
|
1210
|
+
|
1211
|
+
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>
|
1212
|
+
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>
|
1213
|
+
+<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>
|
1214
|
+
<span class="r">end</span>
|
1215
|
+
<span class="r">end</span>
|
1216
|
+
|
1217
|
+
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>
|
1218
|
+
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>
|
1219
|
+
-<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="sy">:matz</span>
|
1220
|
+
<span class="r">end</span>
|
1221
|
+
<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>
|
1222
|
+
<pre class="source" id="unary_source"><span class="r">class</span> <span class="cl">String</span>
|
1223
|
+
<span class="r">def</span> <span class="fu">+@</span>
|
1224
|
+
<span class="pc">self</span>
|
1225
|
+
<span class="r">end</span>
|
1226
|
+
|
1227
|
+
<span class="r">def</span> <span class="fu">-@</span>
|
1228
|
+
to_sym
|
1229
|
+
<span class="r">end</span>
|
1230
|
+
<span class="r">end</span>
|
1231
|
+
|
1232
|
+
<span class="r">class</span> <span class="cl">Symbol</span>
|
1233
|
+
<span class="r">def</span> <span class="fu">+@</span>
|
1234
|
+
to_s
|
1235
|
+
<span class="r">end</span>
|
1236
|
+
|
1237
|
+
<span class="r">def</span> <span class="fu">-@</span>
|
1238
|
+
<span class="pc">self</span>
|
1239
|
+
<span class="r">end</span>
|
1240
|
+
<span class="r">end</span>
|
1241
|
+
|
1242
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (+:literal not possible)</td></tr></table> <h3 title="require 'zucker/union'">union</h3>
|
1243
|
+
<table class="cube_table"
|
1244
|
+
id="union_cube"
|
1245
|
+
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>
|
1246
|
+
<span class="c"># creates a Regexp similar to:</span>
|
1247
|
+
<span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
|
1248
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_union_spec">(<a href="javascript:show('union_spec')">show</a>)</span>
|
1249
|
+
<pre class="source" id="union_spec">require <span class="s"><span class="dl">'</span><span class="k">union</span><span class="dl">'</span></span>
|
1250
|
+
|
1251
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1252
|
+
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>
|
1253
|
+
(<span class="rx"><span class="dl">/</span><span class="k">hallo</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="ch">\d</span><span class="k">+</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="k">.</span><span class="dl">/</span></span>).should ==
|
1254
|
+
<span class="co">Regexp</span>.union( <span class="co">Regexp</span>.union(<span class="rx"><span class="dl">/</span><span class="k">hallo</span><span class="dl">/</span></span>, <span class="rx"><span class="dl">/</span><span class="ch">\d</span><span class="k">+</span><span class="dl">/</span></span>), <span class="rx"><span class="dl">/</span><span class="k">.</span><span class="dl">/</span></span> )
|
1255
|
+
<span class="r">end</span>
|
1256
|
+
<span class="r">end</span>
|
1257
|
+
|
1258
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1259
|
+
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span>
|
1260
|
+
<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>
|
1261
|
+
<pre class="source" id="union_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
1262
|
+
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1263
|
+
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1264
|
+
<span class="r">end</span>
|
1265
|
+
<span class="r">end</span>
|
1266
|
+
|
1267
|
+
<span class="r">class</span> <span class="cl">String</span>
|
1268
|
+
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1269
|
+
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1270
|
+
<span class="r">end</span>
|
1271
|
+
<span class="r">end</span>
|
1272
|
+
|
1273
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table>
|
1274
|
+
</div>
|
1275
|
+
<br/>
|
1276
|
+
</div>
|
1277
|
+
<div id="foot">
|
1278
|
+
<div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
|
1279
|
+
This is the Ruby Zucker 1 documentation.
|
1280
|
+
The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
|
1281
|
+
Gem source can be found at <a href="http://github.com/janlelis/zucker">github</a>.
|
1282
|
+
</div>
|
1283
|
+
|
1284
|
+
</body>
|
1285
|
+
</html>
|
1286
|
+
|