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
data/Rakefile
CHANGED
@@ -3,10 +3,11 @@ require 'rake/gempackagetask'
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'spec/rake/spectask'
|
5
5
|
|
6
|
+
# gem
|
6
7
|
PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*' ]
|
7
8
|
spec = Gem::Specification.new do |s|
|
8
9
|
s.name = 'zucker'
|
9
|
-
s.version = '
|
10
|
+
s.version = '1'
|
10
11
|
s.date = '2010-08-05'
|
11
12
|
s.authors = ['Jan Lelis']
|
12
13
|
s.email = 'mail@janlelis.de'
|
@@ -16,7 +17,6 @@ spec = Gem::Specification.new do |s|
|
|
16
17
|
s.require_paths = [".","lib"]
|
17
18
|
end
|
18
19
|
|
19
|
-
desc 'build gem'
|
20
20
|
Rake::GemPackageTask.new(spec) do |pkg|
|
21
21
|
pkg.gem_spec = spec
|
22
22
|
end
|
data/lib/zucker.rb
CHANGED
@@ -3,7 +3,7 @@ D:
|
|
3
3
|
methods:
|
4
4
|
Object#D: |
|
5
5
|
some.D.methods.D.noone.D.knows.D
|
6
|
-
# ...outputs 4 lines with the inspected
|
6
|
+
# ...outputs 4 lines with the inspected objects
|
7
7
|
# => (result)
|
8
8
|
|
9
9
|
21+Math.sin(42).D
|
@@ -16,10 +16,8 @@ D:
|
|
16
16
|
# => 'Hello Earth'
|
17
17
|
package: debug
|
18
18
|
info:
|
19
|
-
- "This is inspired by the funny
|
19
|
+
- "This is inspired by the funny →⇧.tap⇧ method→http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions→"
|
20
20
|
versions:
|
21
21
|
- 1.9
|
22
22
|
- 1.8
|
23
|
-
authors:
|
24
|
-
- Jan Lelis
|
25
23
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
array:
|
2
|
-
summary: Methods one could miss for ⇧Array
|
2
|
+
summary: Methods one could miss for ⇧Array⇧.
|
3
3
|
why:
|
4
4
|
- ⇧Array⇧ has ⇧&⇧ and ⇧|⇧, but why does it not have ⇧^⇧?
|
5
5
|
- ⇧sum⇧ is an alias for ⇧inject(:+)⇧, but it is just needed pretty often!
|
@@ -11,6 +11,4 @@ array:
|
|
11
11
|
versions:
|
12
12
|
- 1.9
|
13
13
|
- 1.8
|
14
|
-
authors:
|
15
|
-
- Jan Lelis
|
16
14
|
|
@@ -4,12 +4,10 @@ array2proc:
|
|
4
4
|
methods:
|
5
5
|
Array#to_proc: '[1,2,3,4].map( &[:to_s, 2] ) # => ["1", "10", "11", "100"]'
|
6
6
|
info:
|
7
|
-
- Inspired by http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby
|
8
|
-
- "More about to_proc
|
7
|
+
- Inspired by →this article→http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby→.
|
8
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
9
9
|
package: default
|
10
10
|
versions:
|
11
11
|
- 1.9
|
12
12
|
- 1.8
|
13
|
-
authors:
|
14
|
-
- Jan Lelis
|
15
13
|
|
@@ -4,12 +4,9 @@ class2proc:
|
|
4
4
|
Class#to_proc: "[ [1,2],[3,5,6,7,3] ].map(&Set) # => [ Set[1,2], Set[5,6,7,3] ]"
|
5
5
|
package: default
|
6
6
|
info:
|
7
|
-
- Inspired by Ruby Facets
|
8
|
-
- "More about to_proc
|
7
|
+
- Inspired by Ruby Facets.
|
8
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
9
9
|
versions:
|
10
10
|
- 1.9
|
11
11
|
- 1.8
|
12
|
-
authors:
|
13
|
-
- Facet authors
|
14
|
-
- Jan Lelis (this implementation)
|
15
12
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
egonil:
|
2
|
-
summary: Creates a block, where ⇧nil⇧ does not raise ⇧NoMethodError⇧s
|
2
|
+
summary: Creates a block, where ⇧nil⇧ does not raise ⇧NoMethodError⇧s.
|
3
3
|
methods:
|
4
4
|
egonil, nn: |
|
5
5
|
egonil do
|
@@ -10,11 +10,8 @@ egonil:
|
|
10
10
|
# => 5
|
11
11
|
package: default
|
12
12
|
info:
|
13
|
-
- See http://rbjl.net/26-the-28-bytes-of-ruby-joy for more information and discussion
|
13
|
+
- See →this post→http://rbjl.net/26-the-28-bytes-of-ruby-joy→ for more information and discussion
|
14
14
|
versions:
|
15
15
|
- 1.9
|
16
16
|
- 1.8
|
17
|
-
authors:
|
18
|
-
- Jan Lelis
|
19
|
-
- Yohan
|
20
17
|
|
@@ -4,10 +4,8 @@ hash2proc:
|
|
4
4
|
Hash#to_proc: "[1,2,3,4].map(&{\n2 => lambda {|e| e + 1000},\n4 => :to_s,\n}) # => [1, 1002, 3, '4']"
|
5
5
|
package: default
|
6
6
|
info:
|
7
|
-
- "More about to_proc
|
7
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
8
8
|
versions:
|
9
9
|
- 1.9
|
10
10
|
- 1.8
|
11
|
-
authors:
|
12
|
-
- Jan Lelis
|
13
11
|
|
@@ -11,12 +11,10 @@ ivars:
|
|
11
11
|
instance_variables_from binding # assigns @a and @b
|
12
12
|
|
13
13
|
params = {:c => 3, :d => 4}
|
14
|
-
|
14
|
+
instance_variables_from params # # assigns @c and @d
|
15
15
|
end
|
16
16
|
package: default
|
17
17
|
versions:
|
18
18
|
- 1.9
|
19
19
|
- 1.8
|
20
|
-
authors:
|
21
|
-
- Jan Lelis
|
22
20
|
|
File without changes
|
@@ -5,10 +5,8 @@ mm:
|
|
5
5
|
Object#method_list, Object#mm: "'test'.mm 2 # outputs the list (2 levels deep)"
|
6
6
|
package: debug
|
7
7
|
info:
|
8
|
-
- See http://rbjl.net/31-the-multi-mega-method-list for more information
|
8
|
+
- See →this article→http://rbjl.net/31-the-multi-mega-method-list→ for more information
|
9
9
|
versions:
|
10
10
|
- 1.9
|
11
11
|
- 1.8
|
12
|
-
authors:
|
13
|
-
- Jan Lelis (this implementation)
|
14
12
|
|
@@ -6,10 +6,8 @@ regexp2proc:
|
|
6
6
|
%w|just another string array|.select &/[jy]/ # => ["just", "array"]
|
7
7
|
package: default
|
8
8
|
info:
|
9
|
-
- "More about to_proc
|
9
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
10
10
|
versions:
|
11
11
|
- 1.9
|
12
12
|
- 1.8
|
13
|
-
authors:
|
14
|
-
- Jan Lelis
|
15
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
|
+
|
File without changes
|
File without changes
|
@@ -135,39 +135,163 @@ code, pre{
|
|
135
135
|
.source{
|
136
136
|
display:none;
|
137
137
|
border:1px solid #005;
|
138
|
-
background:#111;
|
139
|
-
padding:5px;
|
138
|
+
# background:#111;
|
140
139
|
width:98%;
|
141
140
|
|
142
|
-
background-color: #232323;
|
141
|
+
# background-color: #232323;
|
143
142
|
# border: 1px solid black;
|
144
|
-
font-family: 'Courier New', 'Terminal', monospace;
|
145
|
-
color: #E6E0DB;
|
143
|
+
# font-family: 'Courier New', 'Terminal', monospace;
|
144
|
+
# color: #E6E0DB;
|
146
145
|
padding: 3px 5px;
|
147
146
|
# margin-right:1em;
|
148
147
|
overflow: auto;
|
149
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;
|
150
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
|
+
|
151
294
|
|
152
|
-
/*railscasts*/
|
153
|
-
.source .an { color:#E7BE69 } /* html attribute */
|
154
|
-
.source .c { color:#BC9358; font-style: italic; } /* comment */
|
155
|
-
.source .ch { color:#509E4F } /* escaped character */
|
156
|
-
.source .cl { color:#FFF } /* class */
|
157
|
-
.source .co { color:#FFF } /* constant */
|
158
|
-
.source .fl { color:#A4C260 } /* float */
|
159
|
-
.source .fu { color:#FFC56D } /* function */
|
160
|
-
.source .gv { color:#D0CFFE } /* global variable */
|
161
|
-
.source .i { color:#A4C260 } /* integer */
|
162
|
-
.source .il { background:#151515 } /* inline code */
|
163
|
-
.source .iv { color:#D0CFFE } /* instance variable */
|
164
|
-
.source .pp { color:#E7BE69 } /* doctype */
|
165
|
-
.source .r { color:#CB7832 } /* keyword */
|
166
|
-
.source .rx { color:#A4C260 } /* regex */
|
167
|
-
.source .s { color:#A4C260 } /* string */
|
168
|
-
.source .sy { color:#6C9CBD } /* symbol */
|
169
|
-
.source .ta { color:#E7BE69 } /* html tag */
|
170
|
-
.source .pc { color:#6C9CBD } /* boolean */
|
171
295
|
|
172
296
|
</style>
|
173
297
|
</head>
|
@@ -176,7 +300,7 @@ code, pre{
|
|
176
300
|
|
177
301
|
<h1>Ruby Zucker 1</h1>
|
178
302
|
<h2>What is it?</h2>
|
179
|
-
<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
|
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>
|
180
304
|
|
181
305
|
<h2>Install</h2>
|
182
306
|
<p class="text">
|
@@ -191,224 +315,19 @@ code, pre{
|
|
191
315
|
|
192
316
|
Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
|
193
317
|
|
194
|
-
<code class="scode">require 'zucker/
|
318
|
+
<code class="scode">require 'zucker/egonil'</code>
|
195
319
|
</p>
|
196
|
-
|
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>
|
197
323
|
|
198
324
|
<h2 title="require 'zucker/all'">Cubes</h2>
|
199
325
|
<div class="cubes">
|
200
|
-
<h3 title="require 'zucker/
|
201
|
-
<table class="cube_table"
|
202
|
-
id="class2proc_cube"
|
203
|
-
title="require 'zucker/1/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 to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_class2proc_specification">(<a href="javascript:show('class2proc_specification')">show</a>)</span>
|
204
|
-
<pre class="source" id="class2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">class2proc</span><span class="dl">'</span></span>
|
205
|
-
require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
|
206
|
-
|
207
|
-
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>
|
208
|
-
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>
|
209
|
-
[ [<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>] ]
|
210
|
-
<span class="r">end</span>
|
211
|
-
<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>
|
212
|
-
<pre class="source" id="class2proc_source"><span class="r">class</span> <span class="cl">Class</span>
|
213
|
-
<span class="r">def</span> <span class="fu">to_proc</span>
|
214
|
-
<span class="co">Proc</span>.new <span class="r">do</span> |*args|
|
215
|
-
<span class="pc">self</span>.new *args
|
216
|
-
<span class="r">end</span>
|
217
|
-
<span class="r">end</span>
|
218
|
-
<span class="r">end</span>
|
219
|
-
|
220
|
-
<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/1/iterate'">iterate</h3>
|
221
|
-
<table class="cube_table"
|
222
|
-
id="iterate_cube"
|
223
|
-
title="require 'zucker/1/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|
|
224
|
-
puts <span class="er">”</span><span class="c">#{e},#{f}”</span>
|
225
|
-
<span class="r">end</span>
|
226
|
-
<span class="c"># outputs</span>
|
227
|
-
<span class="c"># 1,3</span>
|
228
|
-
<span class="c"># 2,4</span>
|
229
|
-
<span class="c"># ,5</span>
|
230
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_specification">(<a href="javascript:show('iterate_specification')">show</a>)</span>
|
231
|
-
<pre class="source" id="iterate_specification">require <span class="s"><span class="dl">'</span><span class="k">iterate</span><span class="dl">'</span></span>
|
232
|
-
|
233
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
|
234
|
-
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>
|
235
|
-
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>
|
236
|
-
let <span class="sy">:res</span> <span class="r">do</span> <span class="co">Hash</span>.new {[]} <span class="r">end</span>
|
237
|
-
|
238
|
-
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>
|
239
|
-
iterate a <span class="r">do</span> |ele|
|
240
|
-
res[<span class="sy">:iter</span>] << ele
|
241
|
-
<span class="r">end</span>
|
242
|
-
|
243
|
-
a.each <span class="r">do</span> |ele|
|
244
|
-
res[<span class="sy">:each</span>] << ele
|
245
|
-
<span class="r">end</span>
|
246
|
-
|
247
|
-
res[<span class="sy">:iter</span>].should == res[<span class="sy">:each</span>]
|
248
|
-
<span class="r">end</span>
|
249
|
-
|
250
|
-
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>
|
251
|
-
res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
|
252
|
-
res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
|
253
|
-
res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
|
254
|
-
|
255
|
-
|
256
|
-
iterate a, b <span class="r">do</span> |e,f|
|
257
|
-
res[<span class="sy">:iter_a_b</span>] << [e, f]
|
258
|
-
<span class="c"># p res[:iter_a_b], e, f</span>
|
259
|
-
<span class="r">end</span>
|
260
|
-
|
261
|
-
res[<span class="sy">:iter_a_b</span>].should == [
|
262
|
-
[<span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>],
|
263
|
-
[<span class="i">2</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>],
|
264
|
-
[<span class="i">3</span>, <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>],
|
265
|
-
[<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>],
|
266
|
-
]
|
267
|
-
|
268
|
-
iterate b, a <span class="r">do</span> |e,f|
|
269
|
-
res[<span class="sy">:iter_b_a</span>] << [e, f]
|
270
|
-
<span class="r">end</span>
|
271
|
-
|
272
|
-
res[<span class="sy">:iter_b_a</span>].should == [
|
273
|
-
[<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, <span class="i">1</span>],
|
274
|
-
[<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>, <span class="i">2</span>],
|
275
|
-
[<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>, <span class="i">3</span>],
|
276
|
-
[<span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>, <span class="pc">nil</span>],
|
277
|
-
]
|
278
|
-
|
279
|
-
<span class="r">end</span>
|
280
|
-
|
281
|
-
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>
|
282
|
-
<span class="r">end</span>
|
283
|
-
<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>
|
284
|
-
<pre class="source" id="iterate_source"><span class="r">def</span> <span class="fu">iterate</span>(*params)
|
285
|
-
<span class="c"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
|
286
|
-
|
287
|
-
first = params.shift
|
288
|
-
<span class="r">if</span> params.empty? <span class="c"># single param - like each</span>
|
289
|
-
<span class="r">if</span> block_given?
|
290
|
-
first.map{|e| <span class="r">yield</span> e }
|
291
|
-
<span class="r">else</span>
|
292
|
-
first.map
|
293
|
-
<span class="r">end</span>
|
294
|
-
<span class="r">else</span>
|
295
|
-
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>
|
296
|
-
obj = padded_first.zip *params
|
297
|
-
<span class="r">if</span> block_given?
|
298
|
-
obj.map{|es| <span class="r">yield</span> *es }
|
299
|
-
<span class="r">else</span>
|
300
|
-
obj.map.to_enum
|
301
|
-
<span class="r">end</span>
|
302
|
-
<span class="r">end</span>
|
303
|
-
<span class="r">end</span>
|
304
|
-
|
305
|
-
<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/1/unary'">unary</h3>
|
306
|
-
<table class="cube_table"
|
307
|
-
id="unary_cube"
|
308
|
-
title="require 'zucker/1/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Often, you do not care if you get a <code>String</code> or a <code>Symbol</code> as input - but you need one of both when analysing it.</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</td></tr><tr><th>Specification</th> <td> <span id="show_unary_specification">(<a href="javascript:show('unary_specification')">show</a>)</span>
|
309
|
-
<pre class="source" id="unary_specification">require <span class="s"><span class="dl">'</span><span class="k">unary</span><span class="dl">'</span></span>
|
310
|
-
|
311
|
-
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>
|
312
|
-
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>
|
313
|
-
+(<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>
|
314
|
-
<span class="r">end</span>
|
315
|
-
<span class="r">end</span>
|
316
|
-
|
317
|
-
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>
|
318
|
-
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>
|
319
|
-
-<span class="sy">:matz</span> == <span class="sy">:matz</span>
|
320
|
-
<span class="r">end</span>
|
321
|
-
<span class="r">end</span>
|
322
|
-
|
323
|
-
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>
|
324
|
-
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>
|
325
|
-
+<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>
|
326
|
-
<span class="r">end</span>
|
327
|
-
<span class="r">end</span>
|
328
|
-
|
329
|
-
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>
|
330
|
-
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>
|
331
|
-
-<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="sy">:matz</span>
|
332
|
-
<span class="r">end</span>
|
333
|
-
<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>
|
334
|
-
<pre class="source" id="unary_source"><span class="r">class</span> <span class="cl">String</span>
|
335
|
-
<span class="r">def</span> <span class="fu">+@</span>
|
336
|
-
<span class="pc">self</span>
|
337
|
-
<span class="r">end</span>
|
338
|
-
|
339
|
-
<span class="r">def</span> <span class="fu">-@</span>
|
340
|
-
to_sym
|
341
|
-
<span class="r">end</span>
|
342
|
-
<span class="r">end</span>
|
343
|
-
|
344
|
-
<span class="r">class</span> <span class="cl">Symbol</span>
|
345
|
-
<span class="r">def</span> <span class="fu">+@</span>
|
346
|
-
to_s
|
347
|
-
<span class="r">end</span>
|
348
|
-
|
349
|
-
<span class="r">def</span> <span class="fu">-@</span>
|
350
|
-
<span class="pc">self</span>
|
351
|
-
<span class="r">end</span>
|
352
|
-
<span class="r">end</span>
|
353
|
-
|
354
|
-
<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/1/ivars'">ivars</h3>
|
355
|
-
<table class="cube_table"
|
356
|
-
id="ivars_cube"
|
357
|
-
title="require 'zucker/1/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>)
|
358
|
-
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
359
|
-
|
360
|
-
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
361
|
-
ivars params <span class="c"># # assigns @c and @d</span>
|
362
|
-
<span class="r">end</span>
|
363
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_specification">(<a href="javascript:show('ivars_specification')">show</a>)</span>
|
364
|
-
<pre class="source" id="ivars_specification">require <span class="s"><span class="dl">'</span><span class="k">ivars</span><span class="dl">'</span></span>
|
365
|
-
|
366
|
-
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>
|
367
|
-
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>
|
368
|
-
<span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
369
|
-
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
370
|
-
|
371
|
-
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
372
|
-
ivars params <span class="c"># # assigns @c and @d</span>
|
373
|
-
<span class="r">end</span>
|
374
|
-
|
375
|
-
a_method
|
376
|
-
<span class="iv">@a</span>.should == <span class="i">1</span>
|
377
|
-
<span class="iv">@b</span>.should == <span class="i">2</span>
|
378
|
-
<span class="iv">@c</span>.should == <span class="i">3</span>
|
379
|
-
<span class="iv">@d</span>.should == <span class="i">4</span>
|
380
|
-
|
381
|
-
<span class="r">end</span>
|
382
|
-
<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>
|
383
|
-
<pre class="source" id="ivars_source"><span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
|
384
|
-
iter =
|
385
|
-
<span class="r">if</span> obj.is_a? <span class="co">Binding</span>
|
386
|
-
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] }
|
387
|
-
<span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
|
388
|
-
obj.map{|k,v| [v,k] }
|
389
|
-
<span class="r">else</span>
|
390
|
-
<span class="c"># elsif obj.is_a? Enumerable</span>
|
391
|
-
obj.each.with_index
|
392
|
-
<span class="r">end</span>
|
393
|
-
|
394
|
-
ret = []
|
395
|
-
iter.each{ |value, arg|
|
396
|
-
arg = arg.to_s
|
397
|
-
<span class="r">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
398
|
-
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>
|
399
|
-
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>
|
400
|
-
<span class="pc">self</span>.instance_variable_set ivar, value
|
401
|
-
<span class="r">end</span>
|
402
|
-
}
|
403
|
-
ret
|
404
|
-
<span class="r">end</span>
|
405
|
-
<span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
|
406
|
-
|
407
|
-
<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/1/D'">D</h3>
|
326
|
+
<h3 title="require 'zucker/D'">D</h3>
|
408
327
|
<table class="cube_table"
|
409
328
|
id="D_cube"
|
410
|
-
title="require 'zucker/
|
411
|
-
<span class="c"># ...outputs 4 lines with the inspected
|
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>
|
412
331
|
<span class="c"># => (result)</span>
|
413
332
|
|
414
333
|
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
|
@@ -419,8 +338,8 @@ name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><spa
|
|
419
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>}
|
420
339
|
<span class="c"># outputs "The length is: 5"</span>
|
421
340
|
<span class="c"># => 'Hello Earth'</span>
|
422
|
-
</pre></td></tr><tr><th>Further information</th> <td>This is inspired by the funny <
|
423
|
-
<pre class="source" id="
|
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>
|
424
343
|
|
425
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>
|
426
345
|
|
@@ -463,11 +382,74 @@ describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span
|
|
463
382
|
<span class="r">end</span>
|
464
383
|
<span class="r">end</span>
|
465
384
|
|
466
|
-
<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/
|
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>
|
467
449
|
<table class="cube_table"
|
468
450
|
id="array_cube"
|
469
|
-
title="require 'zucker/
|
470
|
-
<pre class="source" id="
|
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>
|
471
453
|
|
472
454
|
describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
473
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>
|
@@ -504,11 +486,11 @@ describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</sp
|
|
504
486
|
<span class="r">end</span>
|
505
487
|
<span class="r">end</span>
|
506
488
|
|
507
|
-
<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/
|
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>
|
508
490
|
<table class="cube_table"
|
509
491
|
id="array2proc_cube"
|
510
|
-
title="require 'zucker/
|
511
|
-
<pre class="source" id="
|
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>
|
512
494
|
|
513
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>
|
514
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>
|
@@ -523,193 +505,215 @@ describe <span class="s"><span class="dl">'</span><span class="k">Array#to_proc<
|
|
523
505
|
<span class="r">end</span>
|
524
506
|
<span class="r">end</span>
|
525
507
|
|
526
|
-
<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/
|
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>
|
527
509
|
<table class="cube_table"
|
528
|
-
id="
|
529
|
-
title="require 'zucker/
|
530
|
-
|
531
|
-
<span class="iv">@var</span> = {
|
532
|
-
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
533
|
-
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
534
|
-
}
|
535
|
-
<span class="r">end</span>
|
536
|
-
|
537
|
-
square_brackets_for <span class="sy">:var</span> <span class="c"># creates [] and []=</span>
|
538
|
-
<span class="c"># square_brackets_for :var, false # would create only []</span>
|
539
|
-
<span class="r">end</span>
|
540
|
-
|
541
|
-
a = <span class="co">Klass</span>.new
|
542
|
-
a[<span class="sy">:a_key</span>] <span class="c"># => 1</span>
|
543
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_specification">(<a href="javascript:show('square_brackets_for_specification')">show</a>)</span>
|
544
|
-
<pre class="source" id="square_brackets_for_specification">require <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span>
|
545
|
-
|
546
|
-
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>
|
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>
|
547
513
|
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
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>
|
555
524
|
<span class="r">end</span>
|
525
|
+
}
|
556
526
|
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
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>
|
562
536
|
|
563
|
-
|
537
|
+
<span class="r">end</span>
|
538
|
+
<span class="r">end</span>
|
564
539
|
|
565
|
-
|
566
|
-
|
567
|
-
|
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>
|
568
545
|
|
569
|
-
|
570
|
-
|
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> ]
|
571
549
|
|
572
|
-
|
573
|
-
|
574
|
-
|
550
|
+
blank_values.each{ |blank|
|
551
|
+
blank.blank?.should == <span class="pc">true</span>
|
552
|
+
}
|
575
553
|
<span class="r">end</span>
|
576
554
|
|
577
|
-
it <span class="s"><span class="dl">'</span><span class="k">should
|
578
|
-
|
579
|
-
<span class="r">class</span> <span class="cl">Klass</span>
|
580
|
-
square_brackets_for <span class="sy">:var</span>
|
581
|
-
<span class="r">end</span>
|
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> } ]
|
582
557
|
|
583
|
-
|
584
|
-
|
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>
|
585
567
|
|
586
|
-
|
587
|
-
|
568
|
+
<span class="r">def</span> <span class="fu">present?</span>
|
569
|
+
!blank?
|
588
570
|
<span class="r">end</span>
|
571
|
+
<span class="r">end</span>
|
589
572
|
|
590
|
-
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>
|
591
573
|
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
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>],
|
597
580
|
|
598
|
-
|
599
|
-
|
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
|
+
}
|
600
586
|
|
601
|
-
<span class="
|
602
|
-
<
|
603
|
-
|
604
|
-
<span class="c">#
|
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>
|
605
593
|
|
606
|
-
<span class="
|
607
|
-
|
608
|
-
|
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>] ]
|
609
597
|
<span class="r">end</span>
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
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
|
614
603
|
<span class="r">end</span>
|
615
604
|
<span class="r">end</span>
|
616
|
-
<span class="c">#end</span>
|
617
|
-
|
618
605
|
<span class="r">end</span>
|
619
606
|
|
620
|
-
<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/
|
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>
|
621
608
|
<table class="cube_table"
|
622
|
-
id="
|
623
|
-
title="require 'zucker/
|
624
|
-
|
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>
|
625
613
|
|
626
|
-
|
627
|
-
|
628
|
-
<
|
629
|
-
<pre class="source" id="
|
630
|
-
<span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
|
631
|
-
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
632
|
-
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
633
|
-
<span class="r">else</span>
|
634
|
-
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
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>
|
635
618
|
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
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>
|
642
625
|
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
|
647
|
-
<span class="r">else</span>
|
648
|
-
<span class="r">break</span>
|
649
|
-
<span class="r">end</span>
|
650
|
-
}
|
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>
|
651
629
|
|
652
|
-
|
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>
|
653
638
|
<span class="r">end</span>
|
654
639
|
|
655
|
-
<span class="
|
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>
|
656
654
|
<span class="r">end</span>
|
657
655
|
|
658
|
-
<span class="
|
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
659
|
<table class="cube_table"
|
660
|
-
id="
|
661
|
-
title="require 'zucker/
|
662
|
-
<
|
663
|
-
</pre></td></tr><tr><th>Further information</th> <td>More about to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_regexp2proc_specification">(<a href="javascript:show('regexp2proc_specification')">show</a>)</span>
|
664
|
-
<pre class="source" id="regexp2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">regexp2proc</span><span class="dl">'</span></span>
|
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>
|
665
663
|
|
666
|
-
describe <span class="s"><span class="dl">'</span><span class="k">
|
667
|
-
it <span class="s"><span class="dl">'</span><span class="k">should
|
668
|
-
<span class="
|
669
|
-
<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>]
|
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>,}
|
670
667
|
<span class="r">end</span>
|
671
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="
|
672
|
-
<pre class="source" id="
|
673
|
-
<span class="r">def</span> <span class="fu">
|
674
|
-
|
675
|
-
|
676
|
-
|
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
|
677
676
|
<span class="r">end</span>
|
678
677
|
<span class="r">end</span>
|
679
678
|
|
680
|
-
<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/
|
681
|
-
<table class="cube_table"
|
682
|
-
id="
|
683
|
-
title="require 'zucker/
|
684
|
-
|
685
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_specification">(<a href="javascript:show('mcopy_specification')">show</a>)</span>
|
686
|
-
<pre class="source" id="mcopy_specification">require <span class="s"><span class="dl">'</span><span class="k">mcopy</span><span class="dl">'</span></span>
|
687
|
-
|
688
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
|
689
|
-
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>
|
690
|
-
a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
691
|
-
b = a.mcopy
|
692
|
-
b.should == a
|
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>
|
693
684
|
|
694
|
-
|
695
|
-
|
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 ]
|
696
695
|
<span class="r">end</span>
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
<span class="
|
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>
|
701
706
|
<span class="r">end</span>
|
702
|
-
<span class="c"># alias copy mcopy</span>
|
703
707
|
<span class="r">end</span>
|
704
708
|
|
705
|
-
<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/
|
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>
|
706
710
|
<table class="cube_table"
|
707
711
|
id="hash2proc_cube"
|
708
|
-
title="require 'zucker/
|
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(&{
|
709
713
|
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>},
|
710
714
|
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
711
|
-
}) <span class="c"># => [1, 1002, 3, '4']</span></pre></td></tr><tr><th>Further information</th> <td>More about
|
712
|
-
<pre class="source" id="
|
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>
|
713
717
|
|
714
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>
|
715
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>
|
@@ -730,63 +734,151 @@ describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</
|
|
730
734
|
<span class="r">end</span>
|
731
735
|
}
|
732
736
|
<span class="r">end</span>
|
733
|
-
<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>
|
734
740
|
<table class="cube_table"
|
735
|
-
id="
|
736
|
-
title="require 'zucker/
|
737
|
-
|
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>
|
738
751
|
|
739
|
-
describe <span class="s"><span class="dl">'</span><span class="k">
|
740
|
-
|
741
|
-
|
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>]
|
742
767
|
<span class="r">end</span>
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
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
|
+
|
751
798
|
<span class="r">end</span>
|
752
|
-
<span class="r">end</span>
|
753
799
|
|
754
|
-
<span class="
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
<span class="c">#
|
759
|
-
<span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
|
760
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_union_specification">(<a href="javascript:show('union_specification')">show</a>)</span>
|
761
|
-
<pre class="source" id="union_specification">require <span class="s"><span class="dl">'</span><span class="k">union</span><span class="dl">'</span></span>
|
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>
|
762
805
|
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
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>
|
767
821
|
<span class="r">end</span>
|
768
822
|
<span class="r">end</span>
|
769
823
|
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
<span class="
|
775
|
-
|
776
|
-
<span class="
|
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>
|
777
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>
|
778
835
|
|
779
|
-
<span class="
|
780
|
-
<span class="
|
781
|
-
<span class="
|
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
|
782
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
|
783
874
|
<span class="r">end</span>
|
875
|
+
<span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
|
784
876
|
|
785
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table> <h3 title="require 'zucker/
|
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>
|
786
878
|
<table class="cube_table"
|
787
879
|
id="kernel_cube"
|
788
|
-
title="require 'zucker/
|
789
|
-
<pre class="source" id="
|
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>
|
790
882
|
|
791
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>
|
792
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>
|
@@ -837,108 +929,100 @@ describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!
|
|
837
929
|
|
838
930
|
<span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
|
839
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>
|
840
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/
|
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>
|
841
933
|
<table class="cube_table"
|
842
|
-
id="
|
843
|
-
title="require 'zucker/
|
844
|
-
|
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>
|
845
939
|
|
846
|
-
describe <span class="s"><span class="dl">'</span><span class="k">
|
847
|
-
it <span class="s"><span class="dl">'</span><span class="k">
|
848
|
-
<span class="
|
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
|
849
945
|
|
850
|
-
|
851
|
-
|
852
|
-
<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>
|
853
|
-
<pre class="source" id="hash_source"><span class="r">class</span> <span class="cl">Hash</span>
|
854
|
-
<span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
|
855
|
-
<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 ]
|
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
|
856
948
|
<span class="r">end</span>
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
<span class="
|
861
|
-
merge! other
|
862
|
-
<span class="r">when</span> other.respond_to?(<span class="sy">:to_splat</span>)
|
863
|
-
merge! <span class="co">Hash</span>[*other]
|
864
|
-
<span class="r">else</span>
|
865
|
-
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>
|
866
|
-
<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>
|
867
953
|
<span class="r">end</span>
|
954
|
+
<span class="c"># alias copy mcopy</span>
|
868
955
|
<span class="r">end</span>
|
869
956
|
|
870
|
-
<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/
|
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>
|
871
958
|
<table class="cube_table"
|
872
|
-
id="
|
873
|
-
title="require 'zucker/
|
874
|
-
<pre class="source" id="
|
875
|
-
|
876
|
-
describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</span><span class="dl">'</span></span> <span class="r">do</span>
|
877
|
-
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>
|
878
|
-
<span class="c"># alias is_an? is_a?</span>
|
879
|
-
|
880
|
-
<span class="c"># module Enumerable</span>
|
881
|
-
<span class="c"># alias with zip</span>
|
882
|
-
<span class="c"># alias % zip</span>
|
883
|
-
<span class="c"># end</span>
|
884
|
-
|
885
|
-
<span class="c"># class Array</span>
|
886
|
-
<span class="c"># alias ** product</span>
|
887
|
-
<span class="c"># end</span>
|
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>
|
888
962
|
|
889
|
-
|
890
|
-
<span class="
|
891
|
-
|
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>
|
892
972
|
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
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>
|
899
979
|
|
900
|
-
|
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
|
+
}
|
901
988
|
|
902
|
-
|
903
|
-
<span class="co">Infinity</span>.finite?.should == <span class="pc">false</span>
|
904
|
-
<span class="co">NaN</span>.nan?.should == <span class="pc">true</span>
|
989
|
+
<span class="pc">self</span> <span class="c"># or whatever</span>
|
905
990
|
<span class="r">end</span>
|
906
|
-
<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>
|
907
|
-
<pre class="source" id="aliases_source"><span class="r">alias</span> <span class="fu">is_an?</span> <span class="fu">is_a?</span>
|
908
991
|
|
909
|
-
<span class="r">
|
910
|
-
<span class="r">alias</span> <span class="fu">with</span> <span class="fu">zip</span>
|
911
|
-
<span class="r">alias</span> <span class="fu">%</span> <span class="fu">zip</span>
|
912
|
-
<span class="r">end</span>
|
913
|
-
|
914
|
-
<span class="r">class</span> <span class="cl">Array</span>
|
915
|
-
<span class="r">alias</span> <span class="fu">**</span> <span class="fu">product</span>
|
992
|
+
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
|
916
993
|
<span class="r">end</span>
|
917
994
|
|
918
|
-
<span class="
|
919
|
-
|
920
|
-
|
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>
|
921
1002
|
|
922
|
-
<span class="
|
923
|
-
<span class="
|
924
|
-
|
925
|
-
<span class="
|
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>
|
926
1014
|
<span class="r">end</span>
|
927
1015
|
<span class="r">end</span>
|
928
1016
|
|
929
|
-
<span class="c">#
|
930
|
-
<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>
|
931
|
-
<span class="co">NaN</span> = <span class="fl">0.0</span> / <span class="fl">0.0</span>
|
932
|
-
|
933
|
-
<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/1/sandbox'">sandbox</h3>
|
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>
|
934
1018
|
<table class="cube_table"
|
935
1019
|
id="sandbox_cube"
|
936
|
-
title="require 'zucker/
|
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>
|
937
1021
|
<span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
|
938
1022
|
<span class="r">end</span>
|
939
1023
|
<span class="c"># everything's normal again</span>
|
940
|
-
</pre></td></tr><tr><th>Specification</th> <td> <span id="
|
941
|
-
<pre class="source" id="
|
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>
|
942
1026
|
|
943
1027
|
describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
|
944
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>
|
@@ -957,58 +1041,105 @@ describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span>
|
|
957
1041
|
<span class="r">end</span>
|
958
1042
|
<span class="r">end</span>
|
959
1043
|
|
960
|
-
<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/
|
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>
|
961
1045
|
<table class="cube_table"
|
962
|
-
id="
|
963
|
-
title="require 'zucker/
|
964
|
-
|
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>
|
965
1054
|
|
966
|
-
|
967
|
-
|
968
|
-
|
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>
|
969
1058
|
|
970
|
-
|
971
|
-
|
972
|
-
|
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>
|
973
1079
|
<span class="r">end</span>
|
974
1080
|
|
975
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not
|
976
|
-
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> } ]
|
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>
|
977
1082
|
|
978
|
-
|
979
|
-
|
980
|
-
|
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>
|
981
1093
|
<span class="r">end</span>
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
<span class="r">
|
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>
|
986
1106
|
<span class="r">end</span>
|
987
1107
|
|
988
|
-
<span class="
|
989
|
-
|
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>
|
990
1117
|
<span class="r">end</span>
|
991
|
-
<span class="r">end</span>
|
992
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>
|
993
1123
|
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
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>],
|
999
|
-
lambda{ <span class="pc">self</span> == <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> } => [<span class="co">Regexp</span>],
|
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>
|
1000
1128
|
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
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>
|
1006
1137
|
|
1007
|
-
<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/
|
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>
|
1008
1139
|
<table class="cube_table"
|
1009
1140
|
id="string_cube"
|
1010
|
-
title="require 'zucker/
|
1011
|
-
<pre class="source" id="
|
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>
|
1012
1143
|
|
1013
1144
|
describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1014
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>
|
@@ -1059,90 +1190,87 @@ describe <span class="s"><span class="dl">'</span><span class="k">String#ords</s
|
|
1059
1190
|
<span class="r">end</span>
|
1060
1191
|
<span class="r">end</span>
|
1061
1192
|
|
1062
|
-
<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/
|
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>
|
1063
1194
|
<table class="cube_table"
|
1064
|
-
id="
|
1065
|
-
title="require 'zucker/
|
1066
|
-
<pre class="source" id="
|
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>
|
1067
1198
|
|
1068
|
-
describe <span class="s"><span class="dl">'</span><span class="k"
|
1069
|
-
it <span class="s"><span class="dl">'</span><span class="k">should
|
1070
|
-
<span class="
|
1071
|
-
|
1072
|
-
|
1073
|
-
put_vars = lambda { |array|
|
1074
|
-
<span class="r">if</span> array.empty?
|
1075
|
-
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
1076
|
-
<span class="r">else</span>
|
1077
|
-
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>
|
1078
|
-
<span class="r">end</span>
|
1079
|
-
}
|
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>
|
1080
1204
|
|
1081
|
-
<span class="
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
self
|
1087
|
-
- </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">
|
1088
|
-
block_given?
|
1089
|
-
- </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>
|
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>
|
1090
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>
|
1091
1214
|
<span class="r">end</span>
|
1092
1215
|
<span class="r">end</span>
|
1093
1216
|
|
1094
|
-
<span class="
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
<span class="r">
|
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>
|
1100
1226
|
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
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>
|
1105
1231
|
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
1110
|
-
<span class="r">end</span>.should_not raise_exception
|
1232
|
+
<span class="r">class</span> <span class="cl">Symbol</span>
|
1233
|
+
<span class="r">def</span> <span class="fu">+@</span>
|
1234
|
+
to_s
|
1111
1235
|
<span class="r">end</span>
|
1112
1236
|
|
1113
|
-
|
1114
|
-
|
1237
|
+
<span class="r">def</span> <span class="fu">-@</span>
|
1238
|
+
<span class="pc">self</span>
|
1115
1239
|
<span class="r">end</span>
|
1240
|
+
<span class="r">end</span>
|
1116
1241
|
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
<span class="
|
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>
|
1121
1250
|
|
1122
|
-
|
1123
|
-
|
1124
|
-
<span class="
|
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> )
|
1125
1255
|
<span class="r">end</span>
|
1256
|
+
<span class="r">end</span>
|
1126
1257
|
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
<span class="r">
|
1132
|
-
<span class="
|
1133
|
-
<pre class="source" id="egonil_source"><span class="r">def</span> <span class="fu">egonil</span>(nil_value = <span class="pc">nil</span>)
|
1134
|
-
<span class="r">yield</span>
|
1135
|
-
<span class="r">rescue</span> <span class="co">NoMethodError</span> => e
|
1136
|
-
<span class="r">if</span> e.message =~ <span class="rx"><span class="dl">/</span><span class="k">NilClass$</span><span class="dl">/</span></span>
|
1137
|
-
nil_value
|
1138
|
-
<span class="r">else</span>
|
1139
|
-
raise <span class="co">NoMethodError</span>
|
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
|
1140
1264
|
<span class="r">end</span>
|
1141
1265
|
<span class="r">end</span>
|
1142
1266
|
|
1143
|
-
<span class="r">
|
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>
|
1144
1272
|
|
1145
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9
|
1273
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table>
|
1146
1274
|
</div>
|
1147
1275
|
<br/>
|
1148
1276
|
</div>
|
@@ -1150,7 +1278,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><
|
|
1150
1278
|
<div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
|
1151
1279
|
This is the Ruby Zucker 1 documentation.
|
1152
1280
|
The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
|
1153
|
-
Gem source at <a href="http://github.com/janlelis/zucker">github</a>.
|
1281
|
+
Gem source can be found at <a href="http://github.com/janlelis/zucker">github</a>.
|
1154
1282
|
</div>
|
1155
1283
|
|
1156
1284
|
</body>
|