zucker 1 → 2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/Rakefile +14 -4
- data/lib/zucker.rb +0 -4
- data/lib/zucker/2/D.rb +19 -0
- data/lib/zucker/2/aliases.rb +28 -0
- data/lib/zucker/2/all.rb +6 -0
- data/lib/zucker/2/array.rb +17 -0
- data/lib/zucker/2/array2proc.rb +16 -0
- data/lib/zucker/2/binding.rb +25 -0
- data/lib/zucker/2/blank.rb +26 -0
- data/lib/zucker/2/class2proc.rb +10 -0
- data/lib/zucker/2/debug.rb +4 -0
- data/lib/zucker/2/default.rb +25 -0
- data/lib/zucker/2/desc/D.yaml +23 -0
- data/lib/zucker/2/desc/aliases.yaml +16 -0
- data/lib/zucker/2/desc/array.yaml +14 -0
- data/lib/zucker/2/desc/array2proc.yaml +17 -0
- data/lib/zucker/2/desc/binding.yaml +10 -0
- data/lib/zucker/2/desc/blank.yaml +10 -0
- data/lib/zucker/2/desc/class2proc.yaml +12 -0
- data/lib/zucker/2/desc/egonil.yaml +17 -0
- data/lib/zucker/2/desc/enumerable.yaml +11 -0
- data/lib/zucker/2/desc/hash.yaml +14 -0
- data/lib/zucker/2/desc/hash2proc.yaml +11 -0
- data/lib/zucker/2/desc/info.yaml +12 -0
- data/lib/zucker/2/desc/iterate.yaml +17 -0
- data/lib/zucker/2/desc/ivars.yaml +20 -0
- data/lib/zucker/2/desc/kernel.yaml +13 -0
- data/lib/zucker/2/desc/mcopy.yaml +12 -0
- data/lib/zucker/2/desc/mm.yaml +12 -0
- data/lib/zucker/2/desc/regexp2proc.yaml +13 -0
- data/lib/zucker/2/desc/sandbox.yaml +19 -0
- data/lib/zucker/2/desc/square_brackets_for.yaml +23 -0
- data/lib/zucker/2/desc/string.yaml +13 -0
- data/lib/zucker/2/desc/unary.yaml +15 -0
- data/lib/zucker/2/desc/union.yaml +11 -0
- data/lib/zucker/2/doc/zucker_doc.html +1494 -0
- data/lib/zucker/2/doc/zucker_doc.rb +498 -0
- data/lib/zucker/2/egonil.rb +14 -0
- data/lib/zucker/2/enumerable.rb +12 -0
- data/lib/zucker/2/hash.rb +19 -0
- data/lib/zucker/2/hash2proc.rb +14 -0
- data/lib/zucker/2/info.rb +164 -0
- data/lib/zucker/2/iterate.rb +23 -0
- data/lib/zucker/2/ivars.rb +26 -0
- data/lib/zucker/2/kernel.rb +18 -0
- data/lib/zucker/2/mcopy.rb +9 -0
- data/lib/zucker/2/mm.rb +31 -0
- data/lib/zucker/2/regexp2proc.rb +10 -0
- data/lib/zucker/2/sandbox.rb +19 -0
- data/lib/zucker/2/spec/D_spec.rb +35 -0
- data/lib/zucker/2/spec/aliases_spec.rb +13 -0
- data/lib/zucker/2/spec/array2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/array_spec.rb +23 -0
- data/lib/zucker/2/spec/binding_spec.rb +8 -0
- data/lib/zucker/2/spec/blank_spec.rb +20 -0
- data/lib/zucker/2/spec/class2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/egonil_spec.rb +30 -0
- data/lib/zucker/2/spec/enumerable_spec.rb +8 -0
- data/lib/zucker/2/spec/hash2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/hash_spec.rb +17 -0
- data/lib/zucker/2/spec/info_spec.rb +8 -0
- data/lib/zucker/2/spec/iterate_spec.rb +54 -0
- data/lib/zucker/2/spec/ivars_spec.rb +20 -0
- data/lib/zucker/2/spec/kernel_spec.rb +28 -0
- data/lib/zucker/2/spec/mcopy_spec.rb +13 -0
- data/lib/zucker/2/spec/mm_spec.rb +8 -0
- data/lib/zucker/2/spec/regexp2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/sandbox_spec.rb +26 -0
- data/lib/zucker/2/spec/spec_helper.rb +18 -0
- data/lib/zucker/2/spec/square_brackets_for_spec.rb +59 -0
- data/lib/zucker/2/spec/string_spec.rb +29 -0
- data/lib/zucker/2/spec/unary_spec.rb +26 -0
- data/lib/zucker/2/spec/union_spec.rb +15 -0
- data/lib/zucker/2/square_brackets_for.rb +20 -0
- data/lib/zucker/2/string.rb +25 -0
- data/lib/zucker/2/unary.rb +22 -0
- data/lib/zucker/2/union.rb +14 -0
- data/lib/zucker/all.rb +2 -2
- data/lib/zucker/array2proc.rb +7 -1
- data/lib/zucker/debug.rb +3 -3
- data/lib/zucker/default.rb +1 -1
- data/lib/zucker/desc/D.yaml +1 -1
- data/lib/zucker/desc/array.yaml +1 -1
- data/lib/zucker/desc/array2proc.yaml +5 -1
- data/lib/zucker/desc/binding.yaml +2 -2
- data/lib/zucker/desc/blank.yaml +1 -3
- data/lib/zucker/desc/egonil.yaml +1 -1
- data/lib/zucker/desc/enumerable.yaml +1 -1
- data/lib/zucker/desc/hash.yaml +4 -2
- data/lib/zucker/desc/info.yaml +12 -0
- data/lib/zucker/desc/kernel.yaml +2 -2
- data/lib/zucker/desc/mm.yaml +3 -3
- data/lib/zucker/desc/regexp2proc.yaml +1 -1
- data/lib/zucker/desc/sandbox.yaml +6 -1
- data/lib/zucker/desc/string.yaml +1 -1
- data/lib/zucker/desc/unary.yaml +2 -2
- data/lib/zucker/desc/union.yaml +1 -1
- data/lib/zucker/doc/zucker_doc.html +441 -233
- data/lib/zucker/doc/zucker_doc.rb +32 -27
- data/lib/zucker/hash.rb +3 -3
- data/lib/zucker/info.rb +164 -0
- data/lib/zucker/ivars.rb +1 -1
- data/lib/zucker/kernel.rb +2 -4
- data/lib/zucker/sandbox.rb +7 -3
- data/lib/zucker/spec/D_spec.rb +15 -7
- data/lib/zucker/spec/aliases_spec.rb +3 -24
- data/lib/zucker/spec/array2proc_spec.rb +5 -1
- data/lib/zucker/spec/array_spec.rb +1 -1
- data/lib/zucker/spec/binding_spec.rb +4 -2
- data/lib/zucker/spec/blank_spec.rb +1 -1
- data/lib/zucker/spec/class2proc_spec.rb +1 -1
- data/lib/zucker/spec/egonil_spec.rb +1 -1
- data/lib/zucker/spec/enumerable_spec.rb +1 -1
- data/lib/zucker/spec/hash2proc_spec.rb +1 -1
- data/lib/zucker/spec/hash_spec.rb +10 -3
- data/lib/zucker/spec/info_spec.rb +8 -0
- data/lib/zucker/spec/iterate_spec.rb +1 -1
- data/lib/zucker/spec/ivars_spec.rb +1 -1
- data/lib/zucker/spec/kernel_spec.rb +1 -7
- data/lib/zucker/spec/mcopy_spec.rb +1 -1
- data/lib/zucker/spec/mm_spec.rb +4 -2
- data/lib/zucker/spec/regexp2proc_spec.rb +1 -1
- data/lib/zucker/spec/sandbox_spec.rb +22 -3
- data/lib/zucker/spec/spec_helper.rb +18 -0
- data/lib/zucker/spec/square_brackets_for_spec.rb +1 -1
- data/lib/zucker/spec/string_spec.rb +7 -7
- data/lib/zucker/spec/unary_spec.rb +1 -1
- data/lib/zucker/spec/union_spec.rb +8 -6
- metadata +207 -131
- data/lib/zucker/doc/meta.yaml +0 -4
@@ -0,0 +1,20 @@
|
|
1
|
+
def square_brackets_for(ivar, assignment = true)
|
2
|
+
# undef [] if respond_to? :[]
|
3
|
+
# undef []= if respond_to? :[]=
|
4
|
+
|
5
|
+
#instance_eval do
|
6
|
+
define_method :[] do |key|
|
7
|
+
(instance_variable_get :"@#{ivar}")[key]
|
8
|
+
end
|
9
|
+
|
10
|
+
if assignment
|
11
|
+
define_method :[]= do |key, value|
|
12
|
+
(instance_variable_get :"@#{ivar}")[key] = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
#end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
# J-_-L
|
20
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class String
|
2
|
+
def ^(pos)
|
3
|
+
pos = pos.to_i
|
4
|
+
if pos >= 0
|
5
|
+
self[pos..-1]
|
6
|
+
else
|
7
|
+
self[0...pos]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def lchomp(arg=$/)
|
12
|
+
self.reverse.chomp(arg).reverse
|
13
|
+
end
|
14
|
+
|
15
|
+
def lchomp!(arg=$/)
|
16
|
+
self.reverse.chomp!(arg).reverse
|
17
|
+
end
|
18
|
+
|
19
|
+
def ords
|
20
|
+
self.unpack 'C*'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# J-_-L
|
25
|
+
|
data/lib/zucker/all.rb
CHANGED
data/lib/zucker/array2proc.rb
CHANGED
data/lib/zucker/debug.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'zucker/
|
2
|
-
require 'zucker/
|
3
|
-
require 'zucker/
|
1
|
+
require 'zucker/D'
|
2
|
+
require 'zucker/mm'
|
3
|
+
require 'zucker/binding'
|
4
4
|
|
data/lib/zucker/default.rb
CHANGED
data/lib/zucker/desc/D.yaml
CHANGED
@@ -16,7 +16,7 @@ D:
|
|
16
16
|
# => 'Hello Earth'
|
17
17
|
package: debug
|
18
18
|
info:
|
19
|
-
- "This is inspired by the funny →⇧.tap⇧ method→http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions
|
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
|
data/lib/zucker/desc/array.yaml
CHANGED
@@ -2,7 +2,7 @@ array:
|
|
2
2
|
summary: Methods one could miss for ⇧Array⇧.
|
3
3
|
why:
|
4
4
|
- ⇧Array⇧ has ⇧&⇧ and ⇧|⇧, but why does it not have ⇧^⇧?
|
5
|
-
- ⇧sum⇧ is an alias for ⇧inject(:+)⇧, but it is
|
5
|
+
- ⇧sum⇧ is just an alias for ⇧inject(:+)⇧, but it is needed pretty often..
|
6
6
|
methods:
|
7
7
|
Array#^: "[1,2,3,4] ^ [3,4,5,6] # => [1,2,5,6]"
|
8
8
|
Array#sum: "[1,2,3,4,5].sum # => 15"
|
@@ -2,10 +2,14 @@ array2proc:
|
|
2
2
|
summary: Calls the method named by the first paramenter and passes the other elements as paramaters.
|
3
3
|
why: When using ⇧Symbol#to_proc⇧, you often wish to pass parameters.
|
4
4
|
methods:
|
5
|
-
Array#to_proc:
|
5
|
+
Array#to_proc: |
|
6
|
+
[1,2,3,4].map &[:*, 5] # => [5, 10, 15, 20]
|
7
|
+
# you can also chain them, if the first parameter is an Array
|
8
|
+
[1,2,3,4].map &[[:to_s, 2],[:+, 'b']] # => ["1b", "10b", "11b", "100b"]
|
6
9
|
info:
|
7
10
|
- Inspired by →this article→http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby→.
|
8
11
|
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
12
|
+
- Chaining inspired by →eregon→http://github.com/eregon/Classes/blob/master/to_proc.rb→.
|
9
13
|
package: default
|
10
14
|
versions:
|
11
15
|
- 1.9
|
@@ -1,6 +1,6 @@
|
|
1
1
|
binding:
|
2
|
-
summary: Adds a verbose Binding#inspect
|
3
|
-
why: It gives you information about the current environment
|
2
|
+
summary: Adds a verbose ⇧Binding#inspect⇧.
|
3
|
+
why: It gives you information about the current environment.
|
4
4
|
methods:
|
5
5
|
Binding#inspect: binding.inspect
|
6
6
|
package: debug
|
data/lib/zucker/desc/blank.yaml
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
blank:
|
2
|
-
summary: Does pretty the same as in ActiveSupport (Every object can be asked if it is blank)
|
2
|
+
summary: Does pretty the same as in ActiveSupport (Every object can be asked if it is blank).
|
3
3
|
why: It's too familiar ;)
|
4
4
|
methods:
|
5
5
|
Object#blank?: "'an object'.blank? # => false"
|
6
6
|
package: default
|
7
|
-
info:
|
8
|
-
- See ActiveSupport
|
9
7
|
versions:
|
10
8
|
- 1.9
|
11
9
|
- 1.8
|
data/lib/zucker/desc/egonil.yaml
CHANGED
@@ -10,7 +10,7 @@ egonil:
|
|
10
10
|
# => 5
|
11
11
|
package: default
|
12
12
|
info:
|
13
|
-
- See →this post→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
|
data/lib/zucker/desc/hash.yaml
CHANGED
@@ -2,10 +2,12 @@ hash:
|
|
2
2
|
summary: Some sugar for dealing with hashs.
|
3
3
|
methods:
|
4
4
|
Hash.zip: "Hash.zip [1,2,3], [4,5,6] # => {1 => 4, 2 => 5, 3 => 6}"
|
5
|
-
Hash#<<:
|
5
|
+
Hash#<<: |
|
6
|
+
{1 => 2} << [3, 4] # => { 1 => 2, 3 => 4 }
|
7
|
+
{1 => 2} << { 5=>6 } # => { 1 => 2, 5 => 6 }
|
6
8
|
package: default
|
7
9
|
info:
|
8
|
-
- Some of the operators are inspired by Ruby Facets
|
10
|
+
- Some of the operators are inspired by Ruby Facets.
|
9
11
|
versions:
|
10
12
|
- 1.9
|
11
13
|
- 1.8
|
@@ -0,0 +1,12 @@
|
|
1
|
+
info:
|
2
|
+
summary: Access environment information with the ⇧Info⇧ module.
|
3
|
+
why: "You don't need to remember in which global variable, constant or special method the information you are searching for is hidden."
|
4
|
+
methods:
|
5
|
+
Info: |
|
6
|
+
# see the source file for the list of accessors
|
7
|
+
# you could also add them to the global namespace with: include Info
|
8
|
+
package: default
|
9
|
+
versions:
|
10
|
+
- 1.9
|
11
|
+
- 1.8 (not all information)
|
12
|
+
|
data/lib/zucker/desc/kernel.yaml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
kernel:
|
2
|
-
summary: Some useful general shortcut methods
|
3
|
-
why: Readability
|
2
|
+
summary: Some useful general shortcut methods.
|
3
|
+
why: Readability.
|
4
4
|
methods:
|
5
5
|
activate_warnings!: "activate_warnings! # sets $VERBOSE to 1"
|
6
6
|
deactivate_warnings!: "deactivate_warnings! # sets $VERBOSE to 0"
|
data/lib/zucker/desc/mm.yaml
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
mm:
|
2
2
|
summary: ⇧mm⇧ displays an ordered public method list.
|
3
|
-
why: See
|
3
|
+
why: See one object's methods without those rarely used inherited methods.
|
4
4
|
methods:
|
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 →this article→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
|
-
- 1.8
|
11
|
+
- 1.8 (returns strings instead of symbols)
|
12
12
|
|
@@ -1,12 +1,17 @@
|
|
1
1
|
sandbox:
|
2
2
|
summary: Creates a sandbox area.
|
3
|
-
why: Ruby comes with sandboxes, but they are hidden (
|
3
|
+
why: Ruby comes with sandboxes, but they are hidden (-> integrated) in the ⇧$SAFE⇧ concept.
|
4
4
|
methods:
|
5
5
|
sandbox: |
|
6
6
|
sandbox do
|
7
7
|
# dangerous commands throw SecurityErrors ($SAFE=4)
|
8
8
|
end
|
9
9
|
# everything's normal again
|
10
|
+
|
11
|
+
sandbox( lambda{} ) do
|
12
|
+
# no Exception is thrown, if non-nil parameter is passed
|
13
|
+
end
|
14
|
+
# if it is a proc, it will be run instead, if an SecurityError gets raised
|
10
15
|
package: default
|
11
16
|
versions:
|
12
17
|
- 1.9
|
data/lib/zucker/desc/string.yaml
CHANGED
data/lib/zucker/desc/unary.yaml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
unary:
|
2
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
|
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
4
|
methods:
|
5
5
|
String#+@: "+'was_string' # => 'was_string'"
|
6
6
|
String#-@: "-'was_string' # => :was_string"
|
@@ -11,5 +11,5 @@ unary:
|
|
11
11
|
- "Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)"
|
12
12
|
versions:
|
13
13
|
- 1.9
|
14
|
-
- 1.8 (
|
14
|
+
- 1.8 (⇧+:literal⇧ not possible)
|
15
15
|
|
data/lib/zucker/desc/union.yaml
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
<head>
|
6
6
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
7
7
|
|
8
|
-
<title>Ruby Zucker
|
8
|
+
<title>Ruby Zucker 2</title>
|
9
9
|
<script type="text/javascript">
|
10
10
|
var show = function(snippet){
|
11
11
|
document.getElementById( snippet ).style.display = 'block'
|
@@ -134,19 +134,12 @@ code, pre{
|
|
134
134
|
|
135
135
|
.source{
|
136
136
|
display:none;
|
137
|
-
border:1px solid #005;
|
138
|
-
# background:#111;
|
139
137
|
width:98%;
|
140
|
-
|
141
|
-
# background-color: #232323;
|
142
|
-
# border: 1px solid black;
|
143
|
-
# font-family: 'Courier New', 'Terminal', monospace;
|
144
|
-
# color: #E6E0DB;
|
145
138
|
padding: 3px 5px;
|
146
|
-
# margin-right:1em;
|
147
139
|
overflow: auto;
|
148
140
|
font-size: 12px;
|
149
141
|
background-color: #f8f8f8;
|
142
|
+
border:1px solid #005;
|
150
143
|
border: 1px solid silver;
|
151
144
|
font-family: 'Courier New', 'Terminal', monospace;
|
152
145
|
color: #000;
|
@@ -298,9 +291,9 @@ table.source td { padding: 2px 4px; vertical-align: top; }
|
|
298
291
|
<body>
|
299
292
|
<div id="world">
|
300
293
|
|
301
|
-
<h1>Ruby Zucker
|
294
|
+
<h1>Ruby Zucker 2</h1>
|
302
295
|
<h2>What is it?</h2>
|
303
|
-
<p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>).
|
296
|
+
<p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>). It adds syntactical sugar in the form of independent, small scripts that make Ruby even more sweet. Read <a href="http://rbjl.net/32-introducing-ruby-zucker-a-new-syntactical-sugar-gem">this blog post</a> for a little introduction.</p>
|
304
297
|
|
305
298
|
<h2>Install</h2>
|
306
299
|
<p class="text">
|
@@ -321,97 +314,18 @@ table.source td { padding: 2px 4px; vertical-align: top; }
|
|
321
314
|
<p class="text">You can also lock your require to a specific version of Zucker by simply putting the version before the cube name in this way:
|
322
315
|
<code>require 'zucker/1/egonil'</code>. Future releases of the gem will include all previous (main) versions, so the behaviour of these directly required cubes will not change (except for critical bugs).</p>
|
323
316
|
|
324
|
-
|
325
|
-
<
|
326
|
-
|
327
|
-
<
|
328
|
-
id="D_cube"
|
329
|
-
title="require 'zucker/D'"> <tr><th>Summary</th> <td>Easy debug printing with the <code>p</code> alternative <code>.D</code>. It outputs to stdout and returns <code>self</code>. Accepts a block.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#D</h5><pre class="usage source" style="display:block">some.<span class="co">D</span>.methods.<span class="co">D</span>.noone.<span class="co">D</span>.knows.<span class="co">D</span>
|
330
|
-
<span class="c"># ...outputs 4 lines with the inspected objects</span>
|
331
|
-
<span class="c"># => (result)</span>
|
332
|
-
|
333
|
-
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
|
334
|
-
<span class="c"># outputs -0.916521547915634</span>
|
335
|
-
<span class="c"># => 20.0834784520844</span>
|
336
|
-
|
337
|
-
name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
|
338
|
-
<span class="s"><span class="dl">'</span><span class="k">Hello </span><span class="dl">'</span></span> + name.<span class="co">D</span>{|e| <span class="s"><span class="dl">"</span><span class="k">The length is: </span><span class="il"><span class="idl">#{</span>e.size<span class="idl">}</span></span><span class="dl">"</span></span>}
|
339
|
-
<span class="c"># outputs "The length is: 5"</span>
|
340
|
-
<span class="c"># => 'Hello Earth'</span>
|
341
|
-
</pre></td></tr><tr><th>Further information</th> <td>This is inspired by the funny <a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions"><code>.tap</code> method</a></td></tr><tr><th>Specification</th> <td> <span id="show_D_spec">(<a href="javascript:show('D_spec')">show</a>)</span>
|
342
|
-
<pre class="source" id="D_spec">require <span class="s"><span class="dl">'</span><span class="k">D</span><span class="dl">'</span></span>
|
343
|
-
|
344
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span><span class="dl">'</span></span> <span class="r">do</span>
|
345
|
-
|
346
|
-
let <span class="sy">:a</span> <span class="r">do</span>
|
347
|
-
[ <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span> ]
|
348
|
-
<span class="r">end</span>
|
349
|
-
|
350
|
-
it <span class="s"><span class="dl">'</span><span class="k">should not change the object"s value</span><span class="dl">'</span></span> <span class="r">do</span>
|
351
|
-
a.each{ |e|
|
352
|
-
(e.<span class="co">D</span>).should == e
|
353
|
-
}
|
354
|
-
<span class="r">end</span>
|
355
|
-
|
356
|
-
it <span class="s"><span class="dl">"</span><span class="k">should puts .inspect if no block is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
357
|
-
<span class="c"># a.each{ |e|</span>
|
358
|
-
<span class="c"># (e.D).should == e</span>
|
359
|
-
<span class="c"># }</span>
|
360
|
-
<span class="r">end</span>
|
361
|
-
|
362
|
-
it <span class="s"><span class="dl">"</span><span class="k">should puts the block if it is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
363
|
-
a.each{ |e|
|
364
|
-
(e.<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}).should == e
|
365
|
-
}
|
366
|
-
<span class="r">end</span>
|
367
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_D_source">(<a href="javascript:show('D_source')">show</a>)</span>
|
368
|
-
<pre class="source" id="D_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
369
|
-
<span class="r">def</span> <span class="fu">D</span>(*args, &block)
|
370
|
-
<span class="r">if</span> args.empty?
|
371
|
-
tap{
|
372
|
-
<span class="r">if</span> block_given?
|
373
|
-
puts <span class="r">yield</span> <span class="pc">self</span>
|
374
|
-
<span class="r">else</span>
|
375
|
-
puts <span class="pc">self</span>.inspect
|
376
|
-
<span class="r">end</span>
|
377
|
-
}
|
378
|
-
<span class="r">else</span>
|
379
|
-
raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">"</span><span class="k">.D - The parser thought that the code after .D are method arguments... Please don't put a space after D or use .D() or .D{} in this case!</span><span class="dl">"</span></span>
|
380
|
-
<span class="c"># eval ...</span>
|
381
|
-
<span class="r">end</span>
|
382
|
-
<span class="r">end</span>
|
383
|
-
<span class="r">end</span>
|
384
|
-
|
385
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/aliases'">aliases</h3>
|
317
|
+
|
318
|
+
<h2 title="require 'zucker/default'">Cubes[default]</h2>
|
319
|
+
<div class="cubes">
|
320
|
+
<h3 title="require 'zucker/aliases'">aliases</h3>
|
386
321
|
<table class="cube_table"
|
387
322
|
id="aliases_cube"
|
388
323
|
title="require 'zucker/aliases'"> <tr><th>Summary</th> <td>Some convenient aliases (and constants) for existing methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#is_an?</h5><pre class="usage source" style="display:block"><span class="co">Object</span>.is_a?</pre><h5>Enumerable#with</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Enumerable#%</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Array#**</h5><pre class="usage source" style="display:block"><span class="co">Array</span><span class="c">#product</span></pre><h5>Hash#+</h5><pre class="usage source" style="display:block"><span class="co">Hash</span><span class="c">#merge</span></pre><h5>Binding#[]</h5><pre class="usage source" style="display:block"><span class="co">Binding</span><span class="c">#eval</span></pre><h5>Infinity</h5><pre class="usage source" style="display:block"><span class="fl">1.0</span> / <span class="fl">0.0</span></pre><h5>NaN</h5><pre class="usage source" style="display:block"><span class="fl">0.0</span> / <span class="fl">0.0</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_aliases_spec">(<a href="javascript:show('aliases_spec')">show</a>)</span>
|
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>
|
324
|
+
<pre class="source" id="aliases_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/aliases</span><span class="dl">'</span></span>
|
390
325
|
|
391
326
|
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
|
393
|
-
|
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
|
-
|
327
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create these aliases</span><span class="dl">'</span></span> <span class="r">do</span>
|
328
|
+
<span class="c"># see aliases.rb for aliase list</span>
|
415
329
|
<span class="r">end</span>
|
416
330
|
|
417
331
|
it <span class="s"><span class="dl">'</span><span class="k">should define these constants</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -448,8 +362,8 @@ describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</spa
|
|
448
362
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/array'">array</h3>
|
449
363
|
<table class="cube_table"
|
450
364
|
id="array_cube"
|
451
|
-
title="require 'zucker/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code>.</td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&</code> and <code>|</code>, but why does it not have <code>^</code>?</p><p><code>sum</code> is an alias for <code>inject(:+)</code>, but it is
|
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>
|
365
|
+
title="require 'zucker/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code>.</td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&</code> and <code>|</code>, but why does it not have <code>^</code>?</p><p><code>sum</code> is just an alias for <code>inject(:+)</code>, but it is needed pretty often..</p></td></tr><tr><th>Methods/Usage</th> <td><h5>Array#^</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>] ^ [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => [1,2,5,6]</span></pre><h5>Array#sum</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum <span class="c"># => 15</span></pre><h5>Array#chrs</h5><pre class="usage source" style="display:block">[<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs <span class="c"># => 'Hallo'</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_array_spec">(<a href="javascript:show('array_spec')">show</a>)</span>
|
366
|
+
<pre class="source" id="array_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array</span><span class="dl">'</span></span>
|
453
367
|
|
454
368
|
describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
455
369
|
it <span class="s"><span class="dl">'</span><span class="k">should do an exclusive or</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -489,59 +403,40 @@ describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</sp
|
|
489
403
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/array2proc'">array2proc</h3>
|
490
404
|
<table class="cube_table"
|
491
405
|
id="array2proc_cube"
|
492
|
-
title="require 'zucker/array2proc'"> <tr><th>Summary</th> <td>Calls the method named by the first paramenter and passes the other elements as paramaters.</td></tr><tr><th>Why?</th> <td>When using <code>Symbol#to_proc</code>, you often wish to pass parameters.</td></tr><tr><th>Methods/Usage</th> <td><h5>Array#to_proc</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map
|
493
|
-
|
406
|
+
title="require 'zucker/array2proc'"> <tr><th>Summary</th> <td>Calls the method named by the first paramenter and passes the other elements as paramaters.</td></tr><tr><th>Why?</th> <td>When using <code>Symbol#to_proc</code>, you often wish to pass parameters.</td></tr><tr><th>Methods/Usage</th> <td><h5>Array#to_proc</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[<span class="sy">:*</span>, <span class="i">5</span>] <span class="c"># => [5, 10, 15, 20]</span>
|
407
|
+
<span class="c"># you can also chain them, if the first parameter is an Array</span>
|
408
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[[<span class="sy">:to_s</span>, <span class="i">2</span>],[<span class="sy">:+</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>]] <span class="c"># => ["1b", "10b", "11b", "100b"]</span>
|
409
|
+
</pre></td></tr><tr><th>Further information</th> <td>Inspired by <a href="http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby">this article</a>.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.<br/>Chaining inspired by <a href="http://github.com/eregon/Classes/blob/master/to_proc.rb">eregon</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_array2proc_spec">(<a href="javascript:show('array2proc_spec')">show</a>)</span>
|
410
|
+
<pre class="source" id="array2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array2proc</span><span class="dl">'</span></span>
|
494
411
|
|
495
412
|
describe <span class="s"><span class="dl">'</span><span class="k">Array#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
496
413
|
it <span class="s"><span class="dl">'</span><span class="k">should call the method of the first symbol, using the remaining elements as paramaters</span><span class="dl">'</span></span> <span class="r">do</span>
|
497
414
|
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[<span class="sy">:to_s</span>, <span class="i">2</span>] ).should == [<span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">10</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">11</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">100</span><span class="dl">"</span></span>]
|
498
415
|
<span class="r">end</span>
|
416
|
+
|
417
|
+
it <span class="s"><span class="dl">"</span><span class="k">should convert each element to a proc and chain it, if the first parameter is an array</span><span class="dl">"</span></span> <span class="r">do</span>
|
418
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[[<span class="sy">:*</span>,<span class="i">2</span>],[<span class="sy">:+</span>,<span class="i">4</span>]] ).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map{|i| i*<span class="i">2</span> + <span class="i">4</span> }
|
419
|
+
<span class="r">end</span>
|
499
420
|
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_array2proc_source">(<a href="javascript:show('array2proc_source')">show</a>)</span>
|
500
421
|
<pre class="source" id="array2proc_source"><span class="r">class</span> <span class="cl">Array</span>
|
501
422
|
<span class="r">def</span> <span class="fu">to_proc</span>
|
502
423
|
<span class="co">Proc</span>.new{ |obj|
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/binding'">binding</h3>
|
509
|
-
<table class="cube_table"
|
510
|
-
id="binding_cube"
|
511
|
-
title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds a verbose Binding#inspect</td></tr><tr><th>Why?</th> <td>It gives you information about the current environment</td></tr><tr><th>Methods/Usage</th> <td><h5>Binding#inspect</h5><pre class="usage source" style="display:block">binding.inspect</pre></td></tr><tr><th>Specification</th> <td> <span id="show_binding_spec">(<a href="javascript:show('binding_spec')">show</a>)</span>
|
512
|
-
<pre class="source" id="binding_spec">require <span class="s"><span class="dl">'</span><span class="k">binding</span><span class="dl">'</span></span>
|
513
|
-
|
514
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Binding#inspect</span><span class="dl">'</span></span> <span class="r">do</span>
|
515
|
-
it <span class="s"><span class="dl">'</span><span class="k">should output the current environment</span><span class="dl">'</span></span>
|
516
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_binding_source">(<a href="javascript:show('binding_source')">show</a>)</span>
|
517
|
-
<pre class="source" id="binding_source"><span class="r">class</span> <span class="cl">Binding</span>
|
518
|
-
<span class="r">def</span> <span class="fu">inspect</span>
|
519
|
-
put_vars = lambda { |array|
|
520
|
-
<span class="r">if</span> array.empty?
|
521
|
-
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
424
|
+
<span class="r">if</span> <span class="pc">self</span>.first.is_a? <span class="co">Array</span>
|
425
|
+
<span class="pc">self</span>.inject(obj){ |result, nested_array|
|
426
|
+
nested_array.to_proc.call result
|
427
|
+
}
|
522
428
|
<span class="r">else</span>
|
523
|
-
|
429
|
+
obj.send *<span class="pc">self</span>
|
524
430
|
<span class="r">end</span>
|
525
431
|
}
|
526
|
-
|
527
|
-
<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.to_s<span class="idl">}</span></span><span class="k">
|
528
|
-
local vars
|
529
|
-
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
530
|
-
(instance vars)
|
531
|
-
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">instance_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
532
|
-
self
|
533
|
-
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">self</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="k">
|
534
|
-
block_given?
|
535
|
-
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">block_given?</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>
|
536
|
-
|
537
432
|
<span class="r">end</span>
|
538
433
|
<span class="r">end</span>
|
539
434
|
|
540
435
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/blank'">blank</h3>
|
541
436
|
<table class="cube_table"
|
542
437
|
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)
|
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>
|
438
|
+
title="require 'zucker/blank'"> <tr><th>Summary</th> <td>Does pretty the same as in ActiveSupport (Every object can be asked if it is blank).</td></tr><tr><th>Why?</th> <td>It's too familiar ;)</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#blank?</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">an object</span><span class="dl">'</span></span>.blank? <span class="c"># => false</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_blank_spec">(<a href="javascript:show('blank_spec')">show</a>)</span>
|
439
|
+
<pre class="source" id="blank_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/blank</span><span class="dl">'</span></span>
|
545
440
|
|
546
441
|
describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?</span><span class="dl">'</span></span> <span class="r">do</span>
|
547
442
|
it <span class="s"><span class="dl">'</span><span class="k">should be blank for blank values</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -588,7 +483,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?<
|
|
588
483
|
<table class="cube_table"
|
589
484
|
id="class2proc_cube"
|
590
485
|
title="require 'zucker/class2proc'"> <tr><th>Summary</th> <td>Creates a new instance of the class.</td></tr><tr><th>Methods/Usage</th> <td><h5>Class#to_proc</h5><pre class="usage source" style="display:block">[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&<span class="co">Set</span>) <span class="c"># => [ Set[1,2], Set[5,6,7,3] ]</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by Ruby Facets.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_class2proc_spec">(<a href="javascript:show('class2proc_spec')">show</a>)</span>
|
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>
|
486
|
+
<pre class="source" id="class2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/class2proc</span><span class="dl">'</span></span>
|
592
487
|
require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
|
593
488
|
|
594
489
|
describe <span class="s"><span class="dl">'</span><span class="k">Class#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -613,8 +508,8 @@ describe <span class="s"><span class="dl">'</span><span class="k">Class#to_proc<
|
|
613
508
|
|
614
509
|
nn(<span class="i">5</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
615
510
|
<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
|
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>
|
511
|
+
</pre></td></tr><tr><th>Further information</th> <td>See <a href="http://rbjl.net/26-the-28-bytes-of-ruby-joy">this post</a> for more information and discussion.</td></tr><tr><th>Specification</th> <td> <span id="show_egonil_spec">(<a href="javascript:show('egonil_spec')">show</a>)</span>
|
512
|
+
<pre class="source" id="egonil_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/egonil</span><span class="dl">'</span></span>
|
618
513
|
|
619
514
|
describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span> <span class="r">do</span>
|
620
515
|
it <span class="s"><span class="dl">'</span><span class="k">should not raise nil exceptions in the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -658,8 +553,8 @@ describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><
|
|
658
553
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/enumerable'">enumerable</h3>
|
659
554
|
<table class="cube_table"
|
660
555
|
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>
|
556
|
+
title="require 'zucker/enumerable'"> <tr><th>Summary</th> <td><code>Enumerable</code> extensions.</td></tr><tr><th>Methods/Usage</th> <td><h5>Enumerable#mash</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] } <span class="c"># => {1=>'1',2=>'2',3=>'3'}</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by Ruby Facets' <code>mash</code>.</td></tr><tr><th>Specification</th> <td> <span id="show_enumerable_spec">(<a href="javascript:show('enumerable_spec')">show</a>)</span>
|
557
|
+
<pre class="source" id="enumerable_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/enumerable</span><span class="dl">'</span></span>
|
663
558
|
|
664
559
|
describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mash</span><span class="dl">'</span></span> <span class="r">do</span>
|
665
560
|
it <span class="s"><span class="dl">'</span><span class="k">should "map" a hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -679,29 +574,38 @@ describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mas
|
|
679
574
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/hash'">hash</h3>
|
680
575
|
<table class="cube_table"
|
681
576
|
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>]
|
683
|
-
|
577
|
+
title="require 'zucker/hash'"> <tr><th>Summary</th> <td>Some sugar for dealing with hashs.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash.zip</h5><pre class="usage source" style="display:block"><span class="co">Hash</span>.zip [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => {1 => 4, 2 => 5, 3 => 6}</span></pre><h5>Hash#<<</h5><pre class="usage source" style="display:block">{<span class="i">1</span> => <span class="i">2</span>} << [<span class="i">3</span>, <span class="i">4</span>] <span class="c"># => { 1 => 2, 3 => 4 }</span>
|
578
|
+
{<span class="i">1</span> => <span class="i">2</span>} << { <span class="i">5</span>=><span class="i">6</span> } <span class="c"># => { 1 => 2, 5 => 6 }</span>
|
579
|
+
</pre></td></tr><tr><th>Further information</th> <td>Some of the operators are inspired by Ruby Facets.</td></tr><tr><th>Specification</th> <td> <span id="show_hash_spec">(<a href="javascript:show('hash_spec')">show</a>)</span>
|
580
|
+
<pre class="source" id="hash_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash</span><span class="dl">'</span></span>
|
684
581
|
|
685
582
|
describe <span class="s"><span class="dl">'</span><span class="k">Hash.zip</span><span class="dl">'</span></span> <span class="r">do</span>
|
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>
|
583
|
+
it <span class="s"><span class="dl">'</span><span class="k">should zip together both given enumerables and take them as key=>values for a new hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
584
|
+
<span class="co">Hash</span>.zip( [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] ).should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
585
|
+
<span class="r">end</span>
|
687
586
|
<span class="r">end</span>
|
688
587
|
|
689
588
|
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>
|
589
|
+
it <span class="s"><span class="dl">'</span><span class="k">should append new elements to the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
590
|
+
a = { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
591
|
+
a << { <span class="i">4</span>=><span class="i">7</span> }
|
592
|
+
a << [<span class="i">5</span>, <span class="i">8</span>]
|
593
|
+
a.should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span>, <span class="i">4</span>=><span class="i">7</span>, <span class="i">5</span>=><span class="i">8</span> }
|
594
|
+
<span class="r">end</span>
|
691
595
|
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash_source">(<a href="javascript:show('hash_source')">show</a>)</span>
|
692
596
|
<pre class="source" id="hash_source"><span class="r">class</span> <span class="cl">Hash</span>
|
693
597
|
<span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
|
694
|
-
<span class="co">Hash</span>[ *
|
598
|
+
<span class="co">Hash</span>[ *keys.zip( values ).flatten ]
|
695
599
|
<span class="r">end</span>
|
696
600
|
|
697
601
|
<span class="r">def</span> <span class="fu"><<</span>(other)
|
698
602
|
<span class="r">case</span>
|
699
603
|
<span class="r">when</span> other.is_a?(<span class="co">Hash</span>)
|
700
604
|
merge! other
|
701
|
-
<span class="r">when</span> other.respond_to?(<span class="sy">:to_splat</span>)
|
605
|
+
<span class="r">when</span> other.is_a?(<span class="co">Enumerable</span>) || other.respond_to?(<span class="sy">:to_splat</span>)
|
702
606
|
merge! <span class="co">Hash</span>[*other]
|
703
607
|
<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>
|
608
|
+
raise <span class="co">TypeError</span>, <span class="s"><span class="dl">'</span><span class="k">can only append other Hashs and Enumerables (or Classes that implement to_splat)</span><span class="dl">'</span></span>
|
705
609
|
<span class="r">end</span>
|
706
610
|
<span class="r">end</span>
|
707
611
|
<span class="r">end</span>
|
@@ -713,7 +617,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">Hash#<<<
|
|
713
617
|
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>},
|
714
618
|
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
715
619
|
}) <span class="c"># => [1, 1002, 3, '4']</span></pre></td></tr><tr><th>Further information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_hash2proc_spec">(<a href="javascript:show('hash2proc_spec')">show</a>)</span>
|
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>
|
620
|
+
<pre class="source" id="hash2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash2proc</span><span class="dl">'</span></span>
|
717
621
|
|
718
622
|
describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
719
623
|
it <span class="s"><span class="dl">'</span><span class="k">should run the proc given in the value for a key in the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -736,7 +640,182 @@ describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</
|
|
736
640
|
<span class="r">end</span>
|
737
641
|
<span class="r">end</span>
|
738
642
|
|
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/
|
643
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/info'">info</h3>
|
644
|
+
<table class="cube_table"
|
645
|
+
id="info_cube"
|
646
|
+
title="require 'zucker/info'"> <tr><th>Summary</th> <td>Access environment information with the <code>Info</code> module.</td></tr><tr><th>Why?</th> <td>You don't need to remember in which global variable, constant or special method the information you are searching for is hidden.</td></tr><tr><th>Methods/Usage</th> <td><h5>Info</h5><pre class="usage source" style="display:block"><span class="c"># see the source file for the list of accessors</span>
|
647
|
+
<span class="c"># you could also add them to the global namespace with: include Info</span>
|
648
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_info_spec">(<a href="javascript:show('info_spec')">show</a>)</span>
|
649
|
+
<pre class="source" id="info_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/info</span><span class="dl">'</span></span>
|
650
|
+
|
651
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Info</span><span class="dl">'</span></span> <span class="r">do</span>
|
652
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define accessors for global variables, constants and some special methods/keywords</span><span class="dl">'</span></span> <span class="r">do</span>
|
653
|
+
<span class="c"># see sourcefile for the list</span>
|
654
|
+
<span class="r">end</span>
|
655
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_info_source">(<a href="javascript:show('info_source')">show</a>)</span>
|
656
|
+
<pre class="source" id="info_source"><span class="r">module</span> <span class="cl">Info</span>
|
657
|
+
<span class="r">class</span> << <span class="cl">Info</span>
|
658
|
+
<span class="c"># hash like access</span>
|
659
|
+
<span class="r">def</span> <span class="fu">[]</span>(what)
|
660
|
+
send what
|
661
|
+
<span class="r">end</span>
|
662
|
+
|
663
|
+
<span class="c"># TODO: maybe add a descriptions method</span>
|
664
|
+
<span class="r">end</span>
|
665
|
+
|
666
|
+
module_function
|
667
|
+
|
668
|
+
<span class="c"># current script file</span>
|
669
|
+
<span class="r">def</span> <span class="fu">current_file</span>
|
670
|
+
<span class="pc">__FILE__</span>
|
671
|
+
<span class="r">end</span>
|
672
|
+
|
673
|
+
<span class="r">def</span> <span class="fu">current_file_directory</span>
|
674
|
+
<span class="co">File</span>.dirname(<span class="pc">__FILE__</span>)
|
675
|
+
<span class="r">end</span>
|
676
|
+
|
677
|
+
<span class="c"># input</span>
|
678
|
+
<span class="r">def</span> <span class="fu">last_input_file</span>
|
679
|
+
<span class="gv">$FILENAME</span>
|
680
|
+
<span class="r">end</span>
|
681
|
+
|
682
|
+
<span class="r">def</span> <span class="fu">last_input_line_number</span>
|
683
|
+
<span class="gv">$.</span>
|
684
|
+
<span class="r">end</span>
|
685
|
+
|
686
|
+
<span class="r">def</span> <span class="fu">last_input</span>
|
687
|
+
<span class="gv">$_</span>
|
688
|
+
<span class="r">end</span>
|
689
|
+
|
690
|
+
<span class="c"># program</span>
|
691
|
+
<span class="r">def</span> <span class="fu">program_name</span>
|
692
|
+
<span class="gv">$0</span>
|
693
|
+
<span class="r">end</span>
|
694
|
+
|
695
|
+
<span class="r">def</span> <span class="fu">program_arguments</span>
|
696
|
+
<span class="gv">$:</span>
|
697
|
+
<span class="r">end</span>
|
698
|
+
|
699
|
+
<span class="r">def</span> <span class="fu">loaded_programs</span>
|
700
|
+
<span class="gv">$"</span>
|
701
|
+
<span class="r">end</span>
|
702
|
+
|
703
|
+
<span class="r">def</span> <span class="fu">program_data</span>
|
704
|
+
::<span class="co">DATA</span>
|
705
|
+
<span class="r">end</span>
|
706
|
+
|
707
|
+
<span class="r">def</span> <span class="fu">child_program_status</span>
|
708
|
+
<span class="gv">$CHILD_STATUS</span>
|
709
|
+
<span class="r">end</span>
|
710
|
+
|
711
|
+
<span class="c"># system info</span>
|
712
|
+
<span class="r">def</span> <span class="fu">environment</span>
|
713
|
+
::<span class="co">ENV</span>
|
714
|
+
<span class="r">end</span>
|
715
|
+
<span class="r">alias</span> <span class="fu">env</span> <span class="fu">environment</span>
|
716
|
+
|
717
|
+
<span class="r">def</span> <span class="fu">working_directory</span>
|
718
|
+
<span class="co">Dir</span>.pwd
|
719
|
+
<span class="r">end</span>
|
720
|
+
|
721
|
+
<span class="r">def</span> <span class="fu">platform</span>
|
722
|
+
::<span class="co">RUBY_PLATFORM</span>
|
723
|
+
<span class="r">end</span>
|
724
|
+
|
725
|
+
<span class="r">def</span> <span class="fu">process_id</span>
|
726
|
+
<span class="gv">$$</span>
|
727
|
+
<span class="r">end</span>
|
728
|
+
|
729
|
+
<span class="r">def</span> <span class="fu">load_path</span>
|
730
|
+
<span class="gv">$:</span>
|
731
|
+
<span class="r">end</span>
|
732
|
+
|
733
|
+
<span class="c"># current</span>
|
734
|
+
<span class="r">def</span> <span class="fu">current_line</span>
|
735
|
+
<span class="pc">__LINE__</span>
|
736
|
+
<span class="r">end</span>
|
737
|
+
|
738
|
+
<span class="r">def</span> <span class="fu">current_method</span> <span class="c"># 1.9</span>
|
739
|
+
__method__
|
740
|
+
<span class="r">end</span>
|
741
|
+
|
742
|
+
<span class="r">def</span> <span class="fu">current_callstack</span>
|
743
|
+
caller
|
744
|
+
<span class="r">end</span>
|
745
|
+
|
746
|
+
<span class="c"># dealing with strings</span>
|
747
|
+
<span class="r">def</span> <span class="fu">gets_separator</span>
|
748
|
+
<span class="gv">$/</span>
|
749
|
+
<span class="r">end</span>
|
750
|
+
|
751
|
+
<span class="r">def</span> <span class="fu">join_separator</span>
|
752
|
+
<span class="gv">$,</span>
|
753
|
+
<span class="r">end</span>
|
754
|
+
|
755
|
+
<span class="r">def</span> <span class="fu">print_separator</span>
|
756
|
+
<span class="gv">$,</span>
|
757
|
+
<span class="r">end</span>
|
758
|
+
|
759
|
+
<span class="r">def</span> <span class="fu">split_separator</span>
|
760
|
+
<span class="gv">$;</span>
|
761
|
+
<span class="r">end</span>
|
762
|
+
|
763
|
+
<span class="c"># misc</span>
|
764
|
+
<span class="r">def</span> <span class="fu">security_level</span>
|
765
|
+
<span class="gv">$SAFE</span>
|
766
|
+
<span class="r">end</span>
|
767
|
+
|
768
|
+
<span class="r">def</span> <span class="fu">warnings_activated?</span>
|
769
|
+
<span class="gv">$VERBOSE</span>
|
770
|
+
<span class="r">end</span>
|
771
|
+
|
772
|
+
<span class="r">def</span> <span class="fu">debug_activated?</span>
|
773
|
+
<span class="gv">$DEBUG</span>
|
774
|
+
<span class="r">end</span>
|
775
|
+
|
776
|
+
<span class="r">def</span> <span class="fu">last_exception</span>
|
777
|
+
<span class="gv">$!</span>
|
778
|
+
<span class="r">end</span>
|
779
|
+
|
780
|
+
<span class="c"># defined objects</span>
|
781
|
+
<span class="r">def</span> <span class="fu">global_variables</span>
|
782
|
+
global_variables
|
783
|
+
<span class="r">end</span>
|
784
|
+
|
785
|
+
<span class="r">def</span> <span class="fu">global_constants</span>
|
786
|
+
<span class="co">Object</span>.constants
|
787
|
+
<span class="r">end</span>
|
788
|
+
|
789
|
+
<span class="c"># encoding (1.9)</span>
|
790
|
+
<span class="r">def</span> <span class="fu">source_encoding</span>
|
791
|
+
__ENCODING__
|
792
|
+
<span class="r">end</span>
|
793
|
+
|
794
|
+
<span class="r">def</span> <span class="fu">external_encoding</span>
|
795
|
+
<span class="co">Encoding</span>.default_external
|
796
|
+
<span class="r">end</span>
|
797
|
+
|
798
|
+
<span class="r">def</span> <span class="fu">internal_encoding</span>
|
799
|
+
<span class="co">Encoding</span>.default_internal
|
800
|
+
<span class="r">end</span>
|
801
|
+
|
802
|
+
<span class="c"># ruby version info</span>
|
803
|
+
<span class="r">def</span> <span class="fu">ruby_version</span>
|
804
|
+
::<span class="co">RUBY_VERSION</span>
|
805
|
+
<span class="r">end</span>
|
806
|
+
|
807
|
+
<span class="r">def</span> <span class="fu">ruby_patchlevel</span>
|
808
|
+
::<span class="co">RUBY_PATCHLEVEL</span>
|
809
|
+
<span class="r">end</span>
|
810
|
+
|
811
|
+
<span class="r">def</span> <span class="fu">ruby_description</span>
|
812
|
+
::<span class="co">RUBY_DESCRIPTION</span>
|
813
|
+
<span class="r">end</span>
|
814
|
+
|
815
|
+
<span class="r">def</span> <span class="fu">ruby_release_date</span>
|
816
|
+
::<span class="co">RUBY_RELEASE_DATE</span>
|
817
|
+
<span class="r">end</span>
|
818
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (not all information)</td></tr></table> <h3 title="require 'zucker/iterate'">iterate</h3>
|
740
819
|
<table class="cube_table"
|
741
820
|
id="iterate_cube"
|
742
821
|
title="require 'zucker/iterate'"> <tr><th>Summary</th> <td>Iterate over one or more collections.</td></tr><tr><th>Why?</th> <td>It's like <code>.each</code> with two differences: It feels more like a control structure and you can easily iterate over multiple objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>iterate</h5><pre class="usage source" style="display:block">iterate [<span class="i">1</span>,<span class="i">2</span>], [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>] <span class="r">do</span> |e,f|
|
@@ -747,7 +826,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</
|
|
747
826
|
<span class="c"># 2,4</span>
|
748
827
|
<span class="c"># ,5</span>
|
749
828
|
</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>
|
829
|
+
<pre class="source" id="iterate_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/iterate</span><span class="dl">'</span></span>
|
751
830
|
|
752
831
|
describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
|
753
832
|
let <span class="sy">:a</span> <span class="r">do</span> [<span class="i">1</span>, <span class="i">2</span>, <span class="i">3</span>] <span class="r">end</span>
|
@@ -831,7 +910,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate
|
|
831
910
|
instance_variables_from params <span class="c"># # assigns @c and @d</span>
|
832
911
|
<span class="r">end</span>
|
833
912
|
</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>
|
913
|
+
<pre class="source" id="ivars_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/ivars</span><span class="dl">'</span></span>
|
835
914
|
|
836
915
|
describe <span class="s"><span class="dl">'</span><span class="k">instance_variables_from</span><span class="dl">'</span></span> <span class="r">do</span>
|
837
916
|
it <span class="s"><span class="dl">'</span><span class="k">should tansform the given parameter to instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -853,7 +932,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">instance_varia
|
|
853
932
|
<pre class="source" id="ivars_source"><span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
|
854
933
|
iter =
|
855
934
|
<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] }
|
935
|
+
obj.eval(<span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span>).map{|e| [obj.eval(<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="dl">"</span></span>), e] }
|
857
936
|
<span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
|
858
937
|
obj.map{|k,v| [v,k] }
|
859
938
|
<span class="r">else</span>
|
@@ -877,8 +956,8 @@ describe <span class="s"><span class="dl">'</span><span class="k">instance_varia
|
|
877
956
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/kernel'">kernel</h3>
|
878
957
|
<table class="cube_table"
|
879
958
|
id="kernel_cube"
|
880
|
-
title="require 'zucker/kernel'"> <tr><th>Summary</th> <td>Some useful general shortcut methods
|
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>
|
959
|
+
title="require 'zucker/kernel'"> <tr><th>Summary</th> <td>Some useful general shortcut methods.</td></tr><tr><th>Why?</th> <td>Readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>activate_warnings!</h5><pre class="usage source" style="display:block">activate_warnings! <span class="c"># sets $VERBOSE to 1</span></pre><h5>deactivate_warnings!</h5><pre class="usage source" style="display:block">deactivate_warnings! <span class="c"># sets $VERBOSE to 0</span></pre><h5>library?</h5><pre class="usage source" style="display:block">library? <span class="c"># checks, if the current file is ran directly</span></pre><h5>ignore_sigint!</h5><pre class="usage source" style="display:block">ignore_sigint! <span class="c"># blocks CTRL+C</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_kernel_spec">(<a href="javascript:show('kernel_spec')">show</a>)</span>
|
960
|
+
<pre class="source" id="kernel_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/kernel</span><span class="dl">'</span></span>
|
882
961
|
|
883
962
|
describe <span class="s"><span class="dl">'</span><span class="k">activate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
884
963
|
it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to true</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -894,12 +973,6 @@ describe <span class="s"><span class="dl">'</span><span class="k">deactivate_war
|
|
894
973
|
<span class="r">end</span>
|
895
974
|
<span class="r">end</span>
|
896
975
|
|
897
|
-
describe <span class="s"><span class="dl">'</span><span class="k">warnings_activated?</span><span class="dl">'</span></span> <span class="r">do</span>
|
898
|
-
it <span class="s"><span class="dl">'</span><span class="k">should return $VERBOSE</span><span class="dl">'</span></span> <span class="r">do</span>
|
899
|
-
warnings_activated?.should == <span class="gv">$VERBOSE</span>
|
900
|
-
<span class="r">end</span>
|
901
|
-
<span class="r">end</span>
|
902
|
-
|
903
976
|
describe <span class="s"><span class="dl">'</span><span class="k">library?</span><span class="dl">'</span></span> <span class="r">do</span>
|
904
977
|
it <span class="s"><span class="dl">'</span><span class="k">should return false if the file is invoked directly</span><span class="dl">'</span></span> <span class="r">do</span>
|
905
978
|
library?.should == ( <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span> )
|
@@ -919,23 +992,21 @@ describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!
|
|
919
992
|
<span class="gv">$VERBOSE</span> = <span class="pc">false</span>
|
920
993
|
<span class="r">end</span>
|
921
994
|
|
922
|
-
<span class="r">def</span> <span class="fu">warnings_activated?</span>
|
923
|
-
<span class="gv">$VERBOSE</span>
|
924
|
-
<span class="r">end</span>
|
925
|
-
|
926
995
|
<span class="r">def</span> <span class="fu">library?</span>
|
927
996
|
<span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span>
|
928
997
|
<span class="r">end</span>
|
929
998
|
|
930
999
|
<span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
|
931
1000
|
<span class="co">Signal</span>.trap *<span class="s"><span class="dl">%w|</span><span class="k">SIGINT IGNORE</span><span class="dl">|</span></span>
|
932
|
-
<span class="r">end</span
|
1001
|
+
<span class="r">end</span>
|
1002
|
+
|
1003
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mcopy'">mcopy</h3>
|
933
1004
|
<table class="cube_table"
|
934
1005
|
id="mcopy_cube"
|
935
1006
|
title="require 'zucker/mcopy'"> <tr><th>Summary</th> <td>Adds <code>Object#mcopy</code> to create a deep copy using <code>Marshal</code>.</td></tr><tr><th>Why?</th> <td>Every Ruby book describes, you can do <code>Marshal.load Marshal.dump object</code> to create a deep copy... But who needs this verbose syntax in everyday coding?</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#mcopy</h5><pre class="usage source" style="display:block">a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
936
1007
|
b = a.mcopy
|
937
1008
|
</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>
|
1009
|
+
<pre class="source" id="mcopy_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mcopy</span><span class="dl">'</span></span>
|
939
1010
|
|
940
1011
|
describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
|
941
1012
|
it <span class="s"><span class="dl">'</span><span class="k">create a (deep) copy via marshalling</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -954,51 +1025,13 @@ describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</
|
|
954
1025
|
<span class="c"># alias copy mcopy</span>
|
955
1026
|
<span class="r">end</span>
|
956
1027
|
|
957
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mm'">mm</h3>
|
958
|
-
<table class="cube_table"
|
959
|
-
id="mm_cube"
|
960
|
-
title="require 'zucker/mm'"> <tr><th>Summary</th> <td><code>mm</code> displays an ordered public method list.</td></tr><tr><th>Why?</th> <td>See some object's methods without those rarely used inherited methods</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#method_list, Object#mm</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">test</span><span class="dl">'</span></span>.mm <span class="i">2</span> <span class="c"># outputs the list (2 levels deep)</span></pre></td></tr><tr><th>Further information</th> <td>See <a href="http://rbjl.net/31-the-multi-mega-method-list">this article</a> for more information</td></tr><tr><th>Specification</th> <td> <span id="show_mm_spec">(<a href="javascript:show('mm_spec')">show</a>)</span>
|
961
|
-
<pre class="source" id="mm_spec">require <span class="s"><span class="dl">'</span><span class="k">mm</span><span class="dl">'</span></span>
|
962
|
-
|
963
|
-
describe <span class="s"><span class="dl">'</span><span class="k">Object#method_list</span><span class="dl">'</span></span> <span class="r">do</span>
|
964
|
-
it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span>
|
965
|
-
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mm_source">(<a href="javascript:show('mm_source')">show</a>)</span>
|
966
|
-
<pre class="source" id="mm_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
967
|
-
<span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
|
968
|
-
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
969
|
-
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
970
|
-
<span class="r">else</span>
|
971
|
-
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
972
|
-
|
973
|
-
eigen = <span class="pc">self</span>.singleton_methods
|
974
|
-
<span class="r">if</span> !eigen.empty?
|
975
|
-
puts <span class="sy">:Eigenclass</span> <span class="c"># sorry for not being up to date, I just love the word</span>
|
976
|
-
p <span class="pc">self</span>.singleton_methods
|
977
|
-
<span class="r">end</span>
|
978
|
-
<span class="r">end</span>
|
979
|
-
|
980
|
-
levels.times{ |level|
|
981
|
-
<span class="r">if</span> cur = klass.ancestors[level]
|
982
|
-
p cur <span class="c"># put class name</span>
|
983
|
-
p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
|
984
|
-
<span class="r">else</span>
|
985
|
-
<span class="r">break</span>
|
986
|
-
<span class="r">end</span>
|
987
|
-
}
|
988
|
-
|
989
|
-
<span class="pc">self</span> <span class="c"># or whatever</span>
|
990
|
-
<span class="r">end</span>
|
991
|
-
|
992
|
-
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
|
993
|
-
<span class="r">end</span>
|
994
|
-
|
995
1028
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/regexp2proc'">regexp2proc</h3>
|
996
1029
|
<table class="cube_table"
|
997
1030
|
id="regexp2proc_cube"
|
998
|
-
title="require 'zucker/regexp2proc'"> <tr><th>Summary</th> <td>Use &/regex/ to match it against strings
|
1031
|
+
title="require 'zucker/regexp2proc'"> <tr><th>Summary</th> <td>Use &/regex/ to match it against strings.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#to_proc</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["j", nil, nil, "y"]</span>
|
999
1032
|
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["just", "array"]</span>
|
1000
1033
|
</pre></td></tr><tr><th>Further information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_regexp2proc_spec">(<a href="javascript:show('regexp2proc_spec')">show</a>)</span>
|
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>
|
1034
|
+
<pre class="source" id="regexp2proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/regexp2proc</span><span class="dl">'</span></span>
|
1002
1035
|
|
1003
1036
|
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1004
1037
|
it <span class="s"><span class="dl">'</span><span class="k">should match the regex</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -1017,25 +1050,53 @@ describe <span class="s"><span class="dl">'</span><span class="k">Regexp#to_proc
|
|
1017
1050
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/sandbox'">sandbox</h3>
|
1018
1051
|
<table class="cube_table"
|
1019
1052
|
id="sandbox_cube"
|
1020
|
-
title="require 'zucker/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (
|
1053
|
+
title="require 'zucker/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (-> integrated) in the <code>$SAFE</code> concept.</td></tr><tr><th>Methods/Usage</th> <td><h5>sandbox</h5><pre class="usage source" style="display:block">sandbox <span class="r">do</span>
|
1021
1054
|
<span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
|
1022
1055
|
<span class="r">end</span>
|
1023
1056
|
<span class="c"># everything's normal again</span>
|
1057
|
+
|
1058
|
+
sandbox( lambda{} ) <span class="r">do</span>
|
1059
|
+
<span class="c"># no Exception is thrown, if non-nil parameter is passed</span>
|
1060
|
+
<span class="r">end</span>
|
1061
|
+
<span class="c"># if it is a proc, it will be run instead, if an SecurityError gets raised</span>
|
1024
1062
|
</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>
|
1063
|
+
<pre class="source" id="sandbox_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/sandbox</span><span class="dl">'</span></span>
|
1026
1064
|
|
1027
1065
|
describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
|
1028
|
-
it <span class="s"><span class="dl">'</span><span class="k">should throw a SecurityError if bad commands are issued</span><span class="dl">'</span></span>
|
1029
|
-
|
1066
|
+
it <span class="s"><span class="dl">'</span><span class="k">should throw a SecurityError if bad commands are issued</span><span class="dl">'</span></span> <span class="r">do</span>
|
1067
|
+
proc <span class="r">do</span>
|
1068
|
+
sandbox <span class="r">do</span>
|
1069
|
+
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1070
|
+
<span class="r">end</span>
|
1071
|
+
<span class="r">end</span>.should raise_exception <span class="co">SecurityError</span>
|
1072
|
+
<span class="r">end</span>
|
1073
|
+
|
1074
|
+
it <span class="s"><span class="dl">'</span><span class="k">should not throw an exception for errors, if the given parameter is not nil</span><span class="dl">'</span></span> <span class="r">do</span>
|
1075
|
+
proc <span class="r">do</span>
|
1076
|
+
sandbox( <span class="pc">true</span> ) <span class="r">do</span>
|
1077
|
+
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1078
|
+
<span class="r">end</span>
|
1079
|
+
<span class="r">end</span>.should_not raise_exception
|
1080
|
+
<span class="r">end</span>
|
1081
|
+
|
1082
|
+
it <span class="s"><span class="dl">'</span><span class="k">should run the proc passed as parameter for errors, if it is given</span><span class="dl">'</span></span> <span class="r">do</span>
|
1083
|
+
sandbox( lambda{|e| e.class } ) <span class="r">do</span>
|
1084
|
+
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
1085
|
+
<span class="r">end</span>.should == <span class="co">SecurityError</span>
|
1086
|
+
<span class="r">end</span>
|
1030
1087
|
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_sandbox_source">(<a href="javascript:show('sandbox_source')">show</a>)</span>
|
1031
|
-
<pre class="source" id="sandbox_source"><span class="r">def</span> <span class="fu">sandbox</span>(
|
1088
|
+
<pre class="source" id="sandbox_source"><span class="r">def</span> <span class="fu">sandbox</span>(rescueblock_or_default=<span class="pc">nil</span>)
|
1032
1089
|
<span class="co">Thread</span>.start <span class="r">do</span>
|
1033
1090
|
<span class="gv">$SAFE</span> = <span class="i">4</span>
|
1034
1091
|
<span class="r">yield</span>
|
1035
1092
|
<span class="r">end</span>.value
|
1036
1093
|
<span class="r">rescue</span> <span class="co">SecurityError</span> => e
|
1037
|
-
<span class="r">if</span>
|
1038
|
-
|
1094
|
+
<span class="r">if</span> !rescueblock_or_default.nil?
|
1095
|
+
<span class="r">if</span> rescueblock_or_default.is_a? <span class="co">Proc</span>
|
1096
|
+
rescueblock_or_default.call e
|
1097
|
+
<span class="r">else</span>
|
1098
|
+
rescueblock_or_default
|
1099
|
+
<span class="r">end</span>
|
1039
1100
|
<span class="r">else</span>
|
1040
1101
|
raise e
|
1041
1102
|
<span class="r">end</span>
|
@@ -1059,7 +1120,7 @@ describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span>
|
|
1059
1120
|
a = <span class="co">Klass</span>.new
|
1060
1121
|
a[<span class="sy">:a_key</span>] <span class="c"># => 1</span>
|
1061
1122
|
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_spec">(<a href="javascript:show('square_brackets_for_spec')">show</a>)</span>
|
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>
|
1123
|
+
<pre class="source" id="square_brackets_for_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/square_brackets_for</span><span class="dl">'</span></span>
|
1063
1124
|
|
1064
1125
|
describe <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span> <span class="r">do</span>
|
1065
1126
|
|
@@ -1138,19 +1199,19 @@ describe <span class="s"><span class="dl">'</span><span class="k">square_bracket
|
|
1138
1199
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/string'">string</h3>
|
1139
1200
|
<table class="cube_table"
|
1140
1201
|
id="string_cube"
|
1141
|
-
title="require 'zucker/string'"> <tr><th>Summary</th> <td><code>String</code> extensions.</td></tr><tr><th>Why?</th> <td>Strings cannot be comfortable enough ;)
|
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>
|
1202
|
+
title="require 'zucker/string'"> <tr><th>Summary</th> <td><code>String</code> extensions.</td></tr><tr><th>Why?</th> <td>Strings cannot be comfortable enough ;).</td></tr><tr><th>Methods/Usage</th> <td><h5>String#^</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Yes vs No</span><span class="dl">'</span></span>^<span class="i">3</span> <span class="c"># => 'Yes'</span></pre><h5>String#lchomp</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k"> Yes</span><span class="dl">'</span></span>.lchomp <span class="c"># => 'Yes'</span></pre><h5>String#lchomp!</h5><pre class="usage source" style="display:block"><span class="c"># mutable lchomp version</span></pre><h5>String#ords</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>.ords <span class="c"># => [72, 97, 108, 108, 111]</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_string_spec">(<a href="javascript:show('string_spec')">show</a>)</span>
|
1203
|
+
<pre class="source" id="string_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/string</span><span class="dl">'</span></span>
|
1143
1204
|
|
1144
1205
|
describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1145
1206
|
it <span class="s"><span class="dl">'</span><span class="k">should give C-like substring access to strings</span><span class="dl">'</span></span> <span class="r">do</span>
|
1146
1207
|
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1147
1208
|
|
1148
|
-
(string
|
1149
|
-
(string
|
1150
|
-
(string
|
1151
|
-
(string
|
1152
|
-
(string
|
1153
|
-
(string
|
1209
|
+
(string^<span class="i">0</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1210
|
+
(string^<span class="i">1</span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1211
|
+
(string^<span class="i">13</span>).should == <span class="s"><span class="dl">'</span><span class="k">Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1212
|
+
(string^<span class="i">-1</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil</span><span class="dl">'</span></span>
|
1213
|
+
(string^<span class="i">38</span>).should == <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1214
|
+
(string^<span class="i">99</span>).should == <span class="pc">nil</span>
|
1154
1215
|
<span class="r">end</span>
|
1155
1216
|
<span class="r">end</span>
|
1156
1217
|
|
@@ -1193,8 +1254,8 @@ describe <span class="s"><span class="dl">'</span><span class="k">String#ords</s
|
|
1193
1254
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/unary'">unary</h3>
|
1194
1255
|
<table class="cube_table"
|
1195
1256
|
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
|
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>
|
1257
|
+
title="require 'zucker/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Sometimes, you do not care if you get a <code>String</code> or <code>Symbol</code> as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators <code>String#-@</code> and <code>Symbol#+@</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#+@</h5><pre class="usage source" style="display:block">+<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => 'was_string'</span></pre><h5>String#-@</h5><pre class="usage source" style="display:block">-<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => :was_string</span></pre><h5>Symbol#+@</h5><pre class="usage source" style="display:block">+<span class="sy">:was_symbol</span> <span class="c"># => 'was_symbol'</span></pre><h5>Symbol#-@</h5><pre class="usage source" style="display:block">-<span class="sy">:was_symbol</span> <span class="c"># => :was_symbol</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)</td></tr><tr><th>Specification</th> <td> <span id="show_unary_spec">(<a href="javascript:show('unary_spec')">show</a>)</span>
|
1258
|
+
<pre class="source" id="unary_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/unary</span><span class="dl">'</span></span>
|
1198
1259
|
|
1199
1260
|
describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1200
1261
|
it <span class="s"><span class="dl">'</span><span class="k">should convert to_s</span><span class="dl">'</span></span> <span class="r">do</span>
|
@@ -1239,24 +1300,26 @@ describe <span class="co">String</span>, <span class="s"><span class="dl">'</spa
|
|
1239
1300
|
<span class="r">end</span>
|
1240
1301
|
<span class="r">end</span>
|
1241
1302
|
|
1242
|
-
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (
|
1303
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (<code>+:literal</code> not possible)</td></tr></table> <h3 title="require 'zucker/union'">union</h3>
|
1243
1304
|
<table class="cube_table"
|
1244
1305
|
id="union_cube"
|
1245
|
-
title="require 'zucker/union'"> <tr><th>Summary</th> <td>Easy creation of <code>Regexp.union</code>s
|
1306
|
+
title="require 'zucker/union'"> <tr><th>Summary</th> <td>Easy creation of <code>Regexp.union</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#|, String#|</h5><pre class="usage source" style="display:block"><span class="rx"><span class="dl">/</span><span class="k">Ruby</span><span class="ch">\d</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="k">test</span><span class="dl">/</span><span class="mod">i</span></span> | <span class="s"><span class="dl">"</span><span class="k">cheat</span><span class="dl">"</span></span>
|
1246
1307
|
<span class="c"># creates a Regexp similar to:</span>
|
1247
1308
|
<span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
|
1248
1309
|
</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>
|
1310
|
+
<pre class="source" id="union_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/union</span><span class="dl">'</span></span>
|
1250
1311
|
|
1251
|
-
|
1252
|
-
it <span class="s"><span class="dl"
|
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> )
|
1312
|
+
shared_examples_for <span class="s"><span class="dl">"</span><span class="k">Regexp.union operator</span><span class="dl">"</span></span> <span class="r">do</span>
|
1313
|
+
it <span class="s"><span class="dl">"</span><span class="k">should create an Regexp.union of both operands</span><span class="dl">"</span></span> <span class="r">do</span>
|
1255
1314
|
<span class="r">end</span>
|
1256
1315
|
<span class="r">end</span>
|
1257
1316
|
|
1317
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1318
|
+
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1319
|
+
<span class="r">end</span>
|
1320
|
+
|
1258
1321
|
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
|
1322
|
+
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1260
1323
|
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_union_source">(<a href="javascript:show('union_source')">show</a>)</span>
|
1261
1324
|
<pre class="source" id="union_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
1262
1325
|
<span class="r">def</span> <span class="fu">|</span>(arg)
|
@@ -1271,12 +1334,157 @@ describe <span class="s"><span class="dl">'</span><span class="k">String#|</span
|
|
1271
1334
|
<span class="r">end</span>
|
1272
1335
|
|
1273
1336
|
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table>
|
1274
|
-
|
1337
|
+
</div>
|
1338
|
+
<h2 title="require 'zucker/debug'">Cubes[debug]</h2>
|
1339
|
+
<div class="cubes">
|
1340
|
+
<h3 title="require 'zucker/D'">D</h3>
|
1341
|
+
<table class="cube_table"
|
1342
|
+
id="D_cube"
|
1343
|
+
title="require 'zucker/D'"> <tr><th>Summary</th> <td>Easy debug printing with the <code>p</code> alternative <code>.D</code>. It outputs to stdout and returns <code>self</code>. Accepts a block.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#D</h5><pre class="usage source" style="display:block">some.<span class="co">D</span>.methods.<span class="co">D</span>.noone.<span class="co">D</span>.knows.<span class="co">D</span>
|
1344
|
+
<span class="c"># ...outputs 4 lines with the inspected objects</span>
|
1345
|
+
<span class="c"># => (result)</span>
|
1346
|
+
|
1347
|
+
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
|
1348
|
+
<span class="c"># outputs -0.916521547915634</span>
|
1349
|
+
<span class="c"># => 20.0834784520844</span>
|
1350
|
+
|
1351
|
+
name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
|
1352
|
+
<span class="s"><span class="dl">'</span><span class="k">Hello </span><span class="dl">'</span></span> + name.<span class="co">D</span>{|e| <span class="s"><span class="dl">"</span><span class="k">The length is: </span><span class="il"><span class="idl">#{</span>e.size<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1353
|
+
<span class="c"># outputs "The length is: 5"</span>
|
1354
|
+
<span class="c"># => 'Hello Earth'</span>
|
1355
|
+
</pre></td></tr><tr><th>Further information</th> <td>This is inspired by the funny <a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions"><code>.tap</code> method</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_D_spec">(<a href="javascript:show('D_spec')">show</a>)</span>
|
1356
|
+
<pre class="source" id="D_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/D</span><span class="dl">'</span></span>
|
1357
|
+
|
1358
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span><span class="dl">'</span></span> <span class="r">do</span>
|
1359
|
+
|
1360
|
+
let <span class="sy">:a</span> <span class="r">do</span>
|
1361
|
+
[ <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span> ]
|
1362
|
+
<span class="r">end</span>
|
1363
|
+
|
1364
|
+
it <span class="s"><span class="dl">'</span><span class="k">should not change the object"s value</span><span class="dl">'</span></span> <span class="r">do</span>
|
1365
|
+
a.each{ |e|
|
1366
|
+
(e.<span class="co">D</span>).should == e
|
1367
|
+
}
|
1368
|
+
|
1369
|
+
a.each{ |e|
|
1370
|
+
(e.<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}).should == e
|
1371
|
+
}
|
1372
|
+
<span class="r">end</span>
|
1373
|
+
|
1374
|
+
it <span class="s"><span class="dl">"</span><span class="k">should puts .inspect if no block is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
1375
|
+
capture_stdout <span class="r">do</span>
|
1376
|
+
a[<span class="i">0</span>].<span class="co">D</span>
|
1377
|
+
a[<span class="i">1</span>].<span class="co">D</span>
|
1378
|
+
a[<span class="i">6</span>].<span class="co">D</span>
|
1379
|
+
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">1</span><span class="ch">\n</span><span class="k">"1"</span><span class="ch">\n</span><span class="k">true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
1380
|
+
<span class="r">end</span>
|
1381
|
+
|
1382
|
+
it <span class="s"><span class="dl">"</span><span class="k">should puts the block if it is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
1383
|
+
capture_stdout <span class="r">do</span>
|
1384
|
+
a[<span class="i">0</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1385
|
+
a[<span class="i">1</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1386
|
+
a[<span class="i">6</span>].<span class="co">D</span>{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
1387
|
+
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
1388
|
+
<span class="r">end</span>
|
1389
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_D_source">(<a href="javascript:show('D_source')">show</a>)</span>
|
1390
|
+
<pre class="source" id="D_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
1391
|
+
<span class="r">def</span> <span class="fu">D</span>(*args, &block)
|
1392
|
+
<span class="r">if</span> args.empty?
|
1393
|
+
tap{
|
1394
|
+
<span class="r">if</span> block_given?
|
1395
|
+
puts <span class="r">yield</span> <span class="pc">self</span>
|
1396
|
+
<span class="r">else</span>
|
1397
|
+
puts <span class="pc">self</span>.inspect
|
1398
|
+
<span class="r">end</span>
|
1399
|
+
}
|
1400
|
+
<span class="r">else</span>
|
1401
|
+
raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">"</span><span class="k">.D - The parser thought that the code after .D are method arguments... Please don't put a space after D or use .D() or .D{} in this case!</span><span class="dl">"</span></span>
|
1402
|
+
<span class="c"># eval ...</span>
|
1403
|
+
<span class="r">end</span>
|
1404
|
+
<span class="r">end</span>
|
1405
|
+
<span class="r">end</span>
|
1406
|
+
|
1407
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/binding'">binding</h3>
|
1408
|
+
<table class="cube_table"
|
1409
|
+
id="binding_cube"
|
1410
|
+
title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds a verbose <code>Binding#inspect</code>.</td></tr><tr><th>Why?</th> <td>It gives you information about the current environment.</td></tr><tr><th>Methods/Usage</th> <td><h5>Binding#inspect</h5><pre class="usage source" style="display:block">binding.inspect</pre></td></tr><tr><th>Specification</th> <td> <span id="show_binding_spec">(<a href="javascript:show('binding_spec')">show</a>)</span>
|
1411
|
+
<pre class="source" id="binding_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/binding</span><span class="dl">'</span></span>
|
1412
|
+
|
1413
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Binding#inspect</span><span class="dl">'</span></span> <span class="r">do</span>
|
1414
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output the current environment</span><span class="dl">'</span></span> <span class="r">do</span>
|
1415
|
+
pending <span class="s"><span class="dl">'</span><span class="k">#TODO: write spec</span><span class="dl">'</span></span>
|
1416
|
+
<span class="r">end</span>
|
1417
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_binding_source">(<a href="javascript:show('binding_source')">show</a>)</span>
|
1418
|
+
<pre class="source" id="binding_source"><span class="r">class</span> <span class="cl">Binding</span>
|
1419
|
+
<span class="r">def</span> <span class="fu">inspect</span>
|
1420
|
+
put_vars = lambda { |array|
|
1421
|
+
<span class="r">if</span> array.empty?
|
1422
|
+
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
1423
|
+
<span class="r">else</span>
|
1424
|
+
array.map{|e| <span class="s"><span class="dl">"</span><span class="k"> - </span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="k">: </span><span class="il"><span class="idl">#{</span> <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">#{e}</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>}*<span class="s"><span class="dl">"</span><span class="ch">\n</span><span class="dl">"</span></span>
|
1425
|
+
<span class="r">end</span>
|
1426
|
+
}
|
1427
|
+
|
1428
|
+
<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.to_s<span class="idl">}</span></span><span class="k">
|
1429
|
+
local vars
|
1430
|
+
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
1431
|
+
(instance vars)
|
1432
|
+
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">instance_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
1433
|
+
self
|
1434
|
+
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">self</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="k">
|
1435
|
+
block_given?
|
1436
|
+
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">block_given?</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>
|
1437
|
+
|
1438
|
+
<span class="r">end</span>
|
1439
|
+
<span class="r">end</span>
|
1440
|
+
|
1441
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/mm'">mm</h3>
|
1442
|
+
<table class="cube_table"
|
1443
|
+
id="mm_cube"
|
1444
|
+
title="require 'zucker/mm'"> <tr><th>Summary</th> <td><code>mm</code> displays an ordered public method list.</td></tr><tr><th>Why?</th> <td>See one object's methods without those rarely used inherited methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#method_list, Object#mm</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">test</span><span class="dl">'</span></span>.mm <span class="i">2</span> <span class="c"># outputs the list (2 levels deep)</span></pre></td></tr><tr><th>Further information</th> <td>See <a href="http://rbjl.net/31-the-multi-mega-method-list">this article</a> for more information.</td></tr><tr><th>Specification</th> <td> <span id="show_mm_spec">(<a href="javascript:show('mm_spec')">show</a>)</span>
|
1445
|
+
<pre class="source" id="mm_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mm</span><span class="dl">'</span></span>
|
1446
|
+
|
1447
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#method_list</span><span class="dl">'</span></span> <span class="r">do</span>
|
1448
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span> <span class="r">do</span>
|
1449
|
+
pending <span class="s"><span class="dl">'</span><span class="k">#TODO: write spec</span><span class="dl">'</span></span>
|
1450
|
+
<span class="r">end</span>
|
1451
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mm_source">(<a href="javascript:show('mm_source')">show</a>)</span>
|
1452
|
+
<pre class="source" id="mm_source"><span class="r">module</span> <span class="cl">Kernel</span>
|
1453
|
+
<span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
|
1454
|
+
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
1455
|
+
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
1456
|
+
<span class="r">else</span>
|
1457
|
+
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
1458
|
+
|
1459
|
+
eigen = <span class="pc">self</span>.singleton_methods
|
1460
|
+
<span class="r">if</span> !eigen.empty?
|
1461
|
+
puts <span class="sy">:Eigenclass</span> <span class="c"># sorry for not being up to date, I just love the word</span>
|
1462
|
+
p <span class="pc">self</span>.singleton_methods
|
1463
|
+
<span class="r">end</span>
|
1464
|
+
<span class="r">end</span>
|
1465
|
+
|
1466
|
+
levels.times{ |level|
|
1467
|
+
<span class="r">if</span> cur = klass.ancestors[level]
|
1468
|
+
p cur <span class="c"># put class name</span>
|
1469
|
+
p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
|
1470
|
+
<span class="r">else</span>
|
1471
|
+
<span class="r">break</span>
|
1472
|
+
<span class="r">end</span>
|
1473
|
+
}
|
1474
|
+
|
1475
|
+
<span class="pc">self</span> <span class="c"># or whatever</span>
|
1476
|
+
<span class="r">end</span>
|
1477
|
+
|
1478
|
+
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
|
1479
|
+
<span class="r">end</span>
|
1480
|
+
|
1481
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (returns strings instead of symbols)</td></tr></table>
|
1482
|
+
</div>
|
1275
1483
|
<br/>
|
1276
1484
|
</div>
|
1277
1485
|
<div id="foot">
|
1278
1486
|
<div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
|
1279
|
-
This is the Ruby Zucker
|
1487
|
+
This is the Ruby Zucker 2 documentation.
|
1280
1488
|
The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
|
1281
1489
|
Gem source can be found at <a href="http://github.com/janlelis/zucker">github</a>.
|
1282
1490
|
</div>
|