zucker 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +4 -0
  3. data/Rakefile +23 -0
  4. data/lib/zucker.rb +10 -0
  5. data/lib/zucker/1/D.rb +19 -0
  6. data/lib/zucker/1/aliases.rb +28 -0
  7. data/lib/zucker/1/all.rb +6 -0
  8. data/lib/zucker/1/array.rb +17 -0
  9. data/lib/zucker/1/array2proc.rb +10 -0
  10. data/lib/zucker/1/binding.rb +25 -0
  11. data/lib/zucker/1/blank.rb +26 -0
  12. data/lib/zucker/1/class2proc.rb +10 -0
  13. data/lib/zucker/1/debug.rb +4 -0
  14. data/lib/zucker/1/default.rb +25 -0
  15. data/lib/zucker/1/descriptions/D.yaml +25 -0
  16. data/lib/zucker/1/descriptions/aliases.yaml +18 -0
  17. data/lib/zucker/1/descriptions/array.yaml +16 -0
  18. data/lib/zucker/1/descriptions/array2proc.yaml +15 -0
  19. data/lib/zucker/1/descriptions/binding.yaml +12 -0
  20. data/lib/zucker/1/descriptions/blank.yaml +14 -0
  21. data/lib/zucker/1/descriptions/class2proc.yaml +15 -0
  22. data/lib/zucker/1/descriptions/egonil.yaml +20 -0
  23. data/lib/zucker/1/descriptions/enumerable.yaml +13 -0
  24. data/lib/zucker/1/descriptions/hash.yaml +14 -0
  25. data/lib/zucker/1/descriptions/hash2proc.yaml +13 -0
  26. data/lib/zucker/1/descriptions/iterate.yaml +19 -0
  27. data/lib/zucker/1/descriptions/ivars.yaml +22 -0
  28. data/lib/zucker/1/descriptions/kernel.yaml +13 -0
  29. data/lib/zucker/1/descriptions/mcopy.yaml +14 -0
  30. data/lib/zucker/1/descriptions/mm.yaml +14 -0
  31. data/lib/zucker/1/descriptions/regexp2proc.yaml +15 -0
  32. data/lib/zucker/1/descriptions/sandbox.yaml +16 -0
  33. data/lib/zucker/1/descriptions/square_brackets_for.yaml +25 -0
  34. data/lib/zucker/1/descriptions/string.yaml +15 -0
  35. data/lib/zucker/1/descriptions/unary.yaml +15 -0
  36. data/lib/zucker/1/descriptions/union.yaml +11 -0
  37. data/lib/zucker/1/documentation/meta.yaml +4 -0
  38. data/lib/zucker/1/documentation/zucker_doc.html +1158 -0
  39. data/lib/zucker/1/documentation/zucker_doc.rb +369 -0
  40. data/lib/zucker/1/egonil.rb +14 -0
  41. data/lib/zucker/1/enumerable.rb +12 -0
  42. data/lib/zucker/1/hash.rb +19 -0
  43. data/lib/zucker/1/hash2proc.rb +12 -0
  44. data/lib/zucker/1/iterate.rb +23 -0
  45. data/lib/zucker/1/ivars.rb +26 -0
  46. data/lib/zucker/1/kernel.rb +20 -0
  47. data/lib/zucker/1/mcopy.rb +9 -0
  48. data/lib/zucker/1/mm.rb +31 -0
  49. data/lib/zucker/1/regexp2proc.rb +10 -0
  50. data/lib/zucker/1/sandbox.rb +15 -0
  51. data/lib/zucker/1/specification/D_spec.rb +27 -0
  52. data/lib/zucker/1/specification/aliases_spec.rb +34 -0
  53. data/lib/zucker/1/specification/array2proc_spec.rb +8 -0
  54. data/lib/zucker/1/specification/array_spec.rb +23 -0
  55. data/lib/zucker/1/specification/binding_spec.rb +6 -0
  56. data/lib/zucker/1/specification/blank_spec.rb +20 -0
  57. data/lib/zucker/1/specification/class2proc_spec.rb +9 -0
  58. data/lib/zucker/1/specification/egonil_spec.rb +30 -0
  59. data/lib/zucker/1/specification/enumerable_spec.rb +8 -0
  60. data/lib/zucker/1/specification/hash2proc_spec.rb +12 -0
  61. data/lib/zucker/1/specification/hash_spec.rb +10 -0
  62. data/lib/zucker/1/specification/iterate_spec.rb +54 -0
  63. data/lib/zucker/1/specification/ivars_spec.rb +20 -0
  64. data/lib/zucker/1/specification/kernel_spec.rb +34 -0
  65. data/lib/zucker/1/specification/mcopy_spec.rb +13 -0
  66. data/lib/zucker/1/specification/mm_spec.rb +6 -0
  67. data/lib/zucker/1/specification/regexp2proc_spec.rb +9 -0
  68. data/lib/zucker/1/specification/sandbox_spec.rb +7 -0
  69. data/lib/zucker/1/specification/square_brackets_for_spec.rb +59 -0
  70. data/lib/zucker/1/specification/string_spec.rb +29 -0
  71. data/lib/zucker/1/specification/unary_spec.rb +26 -0
  72. data/lib/zucker/1/specification/union_spec.rb +13 -0
  73. data/lib/zucker/1/square_brackets_for.rb +20 -0
  74. data/lib/zucker/1/string.rb +25 -0
  75. data/lib/zucker/1/unary.rb +22 -0
  76. data/lib/zucker/1/union.rb +14 -0
  77. data/lib/zucker/all.rb +2 -0
  78. data/lib/zucker/debug.rb +2 -0
  79. data/lib/zucker/default.rb +2 -0
  80. data/lib/zucker/edge/ideas/args.rb +22 -0
  81. data/lib/zucker/edge/ideas/args.yaml +19 -0
  82. data/lib/zucker/edge/ideas/args_spec.rb +19 -0
  83. data/lib/zucker/edge/ideas/clone_deep.rb +21 -0
  84. data/lib/zucker/edge/ideas/clone_deep_spec.rb +12 -0
  85. data/lib/zucker/edge/ideas/dclone.yaml +12 -0
  86. data/lib/zucker/edge/ideas/hash.rb +18 -0
  87. data/lib/zucker/edge/ideas/require_directory.rb +6 -0
  88. metadata +153 -0
@@ -0,0 +1,22 @@
1
+ ivars:
2
+ summary: This method lets you easily assign lots of instance variables.
3
+ why: |
4
+ Often, you have to write boilerplate code for assigning instance varialbles, for example this one:
5
+ ⇧def initialize(variable1, variable2)
6
+   @variable1, @variable2 = variable1, variable2
7
+ end⇧
8
+ methods:
9
+ instance_variables_from, ivars: |
10
+ def a_method(a = 1, b = 2)
11
+ instance_variables_from binding # assigns @a and @b
12
+
13
+ params = {:c => 3, :d => 4}
14
+ ivars params # # assigns @c and @d
15
+ end
16
+ package: default
17
+ versions:
18
+ - 1.9
19
+ - 1.8
20
+ authors:
21
+ - Jan Lelis
22
+
@@ -0,0 +1,13 @@
1
+ kernel:
2
+ summary: Some useful general shortcut methods
3
+ why: Readability
4
+ methods:
5
+ activate_warnings!: "activate_warnings! # sets $VERBOSE to 1"
6
+ deactivate_warnings!: "deactivate_warnings! # sets $VERBOSE to 0"
7
+ library?: "library? # checks, if the current file is ran directly"
8
+ ignore_sigint!: "ignore_sigint! # blocks CTRL+C"
9
+ package: default
10
+ versions:
11
+ - 1.9
12
+ - 1.8
13
+
@@ -0,0 +1,14 @@
1
+ mcopy:
2
+ summary: Adds ⇧Object#mcopy⇧ to create a deep copy using ⇧Marshal⇧.
3
+ why: Every Ruby book describes, you can do ⇧Marshal.load Marshal.dump object⇧ to create a deep copy... But who needs this verbose syntax in everyday coding?
4
+ methods:
5
+ Object#mcopy: |
6
+ a = %w[hello world]
7
+ b = a.mcopy
8
+ package: default
9
+ versions:
10
+ - 1.9
11
+ - 1.8
12
+ authors:
13
+ - Jan Lelis
14
+
@@ -0,0 +1,14 @@
1
+ mm:
2
+ summary: ⇧mm⇧ displays an ordered public method list.
3
+ why: See some object's methods without those rarely used inherited methods
4
+ methods:
5
+ Object#method_list, Object#mm: "'test'.mm 2 # outputs the list (2 levels deep)"
6
+ package: debug
7
+ info:
8
+ - See http://rbjl.net/31-the-multi-mega-method-list for more information
9
+ versions:
10
+ - 1.9
11
+ - 1.8
12
+ authors:
13
+ - Jan Lelis (this implementation)
14
+
@@ -0,0 +1,15 @@
1
+ regexp2proc:
2
+ summary: Use &/regex/ to match it against strings
3
+ methods:
4
+ Regexp#to_proc: |
5
+ %w|just another string array|.map &/[jy]/ # => ["j", nil, nil, "y"]
6
+ %w|just another string array|.select &/[jy]/ # => ["just", "array"]
7
+ package: default
8
+ info:
9
+ - "More about to_proc: http://rbjl.net/29-become-a-proc-star"
10
+ versions:
11
+ - 1.9
12
+ - 1.8
13
+ authors:
14
+ - Jan Lelis
15
+
@@ -0,0 +1,16 @@
1
+ sandbox:
2
+ summary: Creates a sandbox area.
3
+ why: Ruby comes with sandboxes, but they are hidden (--> integrated) in the ⇧$SAFE⇧ concept
4
+ methods:
5
+ sandbox: |
6
+ sandbox do
7
+ # dangerous commands throw SecurityErrors ($SAFE=4)
8
+ end
9
+ # everything's normal again
10
+ package: default
11
+ versions:
12
+ - 1.9
13
+ - 1.8
14
+ authors:
15
+ - Jan Lelis
16
+
@@ -0,0 +1,25 @@
1
+ square_brackets_for:
2
+ summary: This helper methods defines ⇧[]⇧ and ⇧[]=⇧ for accesing an instance variable.
3
+ methods:
4
+ square_brackets_for: |
5
+ class Klass
6
+ def initialize
7
+ @var = {
8
+ :a_key => 1,
9
+ :another_one => 2,
10
+ }
11
+ end
12
+
13
+ square_brackets_for :var # creates [] and []=
14
+ # square_brackets_for :var, false # would create only []
15
+ end
16
+
17
+ a = Klass.new
18
+ a[:a_key] # => 1
19
+ package: default
20
+ versions:
21
+ - 1.9
22
+ - 1.8
23
+ authors:
24
+ - Jan Lelis
25
+
@@ -0,0 +1,15 @@
1
+ string:
2
+ summary: ⇧String⇧ extensions.
3
+ why: Strings cannot be comfortable enough ;)
4
+ methods:
5
+ String#^: "'Yes vs No'^3 # => 'Yes'"
6
+ String#lchomp: "' Yes'.lchomp # => 'Yes'"
7
+ String#lchomp!: "# mutable lchomp version"
8
+ String#ords: "'Hallo'.ords # => [72, 97, 108, 108, 111]"
9
+ package: default
10
+ versions:
11
+ - 1.9
12
+ - 1.8
13
+ authors:
14
+ - Jan Lelis
15
+
@@ -0,0 +1,15 @@
1
+ unary:
2
+ summary: Easy conversion between strings and symbols.
3
+ why: Often, you do not care if you get a ⇧String⇧ or a ⇧Symbol⇧ as input - but you need one of both when analysing it.
4
+ methods:
5
+ String#+@: "+'was_string' # => 'was_string'"
6
+ String#-@: "-'was_string' # => :was_string"
7
+ Symbol#+@: "+:was_symbol # => 'was_symbol'"
8
+ Symbol#-@: "-:was_symbol # => :was_symbol"
9
+ package: default
10
+ info:
11
+ - Inspired by
12
+ versions:
13
+ - 1.9
14
+ - 1.8 (+:literal not possible)
15
+
@@ -0,0 +1,11 @@
1
+ union:
2
+ summary: Easy creation of ⇧Regexp.union⇧s
3
+ methods:
4
+ Regexp#|, String#|: |
5
+ /Ruby\d/ | /test/i | "cheat"
6
+ # creates a Regexp similar to:
7
+ # /(Ruby\d|[tT][eE][sS][tT]|cheat)/
8
+ package: default
9
+ versions:
10
+ - 1.9
11
+
@@ -0,0 +1,4 @@
1
+ version: 1
2
+ release_date: 2010-08-05
3
+ authors:
4
+ - Jan Lelis
@@ -0,0 +1,1158 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7
+
8
+ <title>Ruby Zucker 1</title>
9
+ <script type="text/javascript">
10
+ var show = function(snippet){
11
+ document.getElementById( snippet ).style.display = 'block'
12
+ if( document.getElementById( 'show_'+snippet ) ){
13
+ document.getElementById( 'show_'+snippet ).style.display = 'none'
14
+ }
15
+ }
16
+ </script>
17
+ <style type="text/css">
18
+ body{
19
+ background-color:#aaa;
20
+ color:#111;
21
+ font-family:sans-serif;
22
+ }
23
+
24
+ #world{
25
+ background:#fff;
26
+ }
27
+
28
+ h1, h2, h3, h4, h5, h6{
29
+ margin:0px;
30
+ padding:0px;
31
+ }
32
+
33
+ h1{
34
+ color:#222;
35
+ text-align:center;
36
+ padding:0.5em;
37
+ }
38
+ h2{
39
+ margin-left:0.5em;
40
+ margin-top:0.8em;
41
+ margin-bottom:0.5em;
42
+ }
43
+
44
+ th{
45
+ width:200px;
46
+ color:#444;
47
+ }
48
+
49
+ p{
50
+ margin:0px;
51
+ margin-bottom:0.5em;
52
+ }
53
+
54
+ p.text{
55
+ margin-left:1.5em;
56
+ margin-right:1em;
57
+ }
58
+
59
+ code{
60
+ }
61
+
62
+ .scode{
63
+ display:block;
64
+ margin:0.8em;
65
+ # margin-right:1.8em;
66
+ padding:0.5em;
67
+ border:1px solid black;
68
+ background:#eee;
69
+ }
70
+
71
+ #.cube_table{
72
+ # display:none;
73
+ #}
74
+ .cubes{
75
+ margin:0px;
76
+ margin-left:1.5em;
77
+ margin-right:1em;
78
+ }
79
+
80
+ a{
81
+ color:#111;
82
+ }
83
+
84
+ table{
85
+ margin:0.8em;
86
+ margin-top:0.2em;
87
+ padding:0.2em;
88
+ border:1px solid #111;
89
+ background:#eee;
90
+ overflow:auto;
91
+ display:block;
92
+ }
93
+
94
+ th{
95
+ text-align:left;
96
+ vertical-align:top;
97
+ padding-right:3em;
98
+ }
99
+
100
+ td{
101
+ width:100%;
102
+ }
103
+
104
+ li{
105
+ list-style:none;
106
+ }
107
+
108
+
109
+ #foot{
110
+ text-align:left;
111
+ padding:0.3em;
112
+ font-size:70%
113
+ }
114
+ #foot, #foot a{
115
+ color:#444;
116
+ }
117
+ #smile{
118
+ font-size:150%;
119
+ float:right;
120
+ }
121
+ #smile a{
122
+ text-decoration:none;
123
+ }
124
+
125
+ .small{
126
+ font-size:70%;
127
+ }
128
+
129
+ code, pre{
130
+ font-face:mono;
131
+ margin:0px;
132
+ padding:0px;
133
+ }
134
+
135
+ .source{
136
+ display:none;
137
+ border:1px solid #005;
138
+ background:#111;
139
+ padding:5px;
140
+ width:98%;
141
+
142
+ background-color: #232323;
143
+ # border: 1px solid black;
144
+ font-family: 'Courier New', 'Terminal', monospace;
145
+ color: #E6E0DB;
146
+ padding: 3px 5px;
147
+ # margin-right:1em;
148
+ overflow: auto;
149
+ font-size: 12px;
150
+ }
151
+
152
+ /*railscasts*/
153
+ .source .an { color:#E7BE69 } /* html attribute */
154
+ .source .c { color:#BC9358; font-style: italic; } /* comment */
155
+ .source .ch { color:#509E4F } /* escaped character */
156
+ .source .cl { color:#FFF } /* class */
157
+ .source .co { color:#FFF } /* constant */
158
+ .source .fl { color:#A4C260 } /* float */
159
+ .source .fu { color:#FFC56D } /* function */
160
+ .source .gv { color:#D0CFFE } /* global variable */
161
+ .source .i { color:#A4C260 } /* integer */
162
+ .source .il { background:#151515 } /* inline code */
163
+ .source .iv { color:#D0CFFE } /* instance variable */
164
+ .source .pp { color:#E7BE69 } /* doctype */
165
+ .source .r { color:#CB7832 } /* keyword */
166
+ .source .rx { color:#A4C260 } /* regex */
167
+ .source .s { color:#A4C260 } /* string */
168
+ .source .sy { color:#6C9CBD } /* symbol */
169
+ .source .ta { color:#E7BE69 } /* html tag */
170
+ .source .pc { color:#6C9CBD } /* boolean */
171
+
172
+ </style>
173
+ </head>
174
+ <body>
175
+ <div id="world">
176
+
177
+ <h1>Ruby Zucker 1</h1>
178
+ <h2>What is it?</h2>
179
+ <p class="text">Zucker is the German word for sugar (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>). This gem adds syntactical sugar in the form of independent, lightweight scripts that make Ruby even more beautiful. Read <a href="http://rbjl.net/32-">this blog post</a> for a little introduction!</p>
180
+
181
+ <h2>Install</h2>
182
+ <p class="text">
183
+ <code class="scode">gem install zucker # might need sudo</code>
184
+ </p>
185
+
186
+ <h2>Usage / Organisation</h2>
187
+ <p class="text">The gem consists of many small snippets, called <em>cubes</em>, which are bundled in <em>packages</em>. Currently, there are two packages available: <strong>default</strong> and <strong>debug</strong>. You can use a package be requiring it in this way:
188
+ <code class="scode">require 'zucker/default'</code>
189
+ and
190
+ <code class="scode">require 'zucker/debug'</code>
191
+
192
+ Since there aren't any dependencies within the gem, you could also pick only the cubes you want:
193
+
194
+ <code class="scode">require 'zucker/1/egonil'</code>
195
+ </p>
196
+ <p class="text"><em>Please note:</em> To cherry-pick cubes, you have to allude to the gem version you want to use. Future releases of the gem will include all previous versions, so the behaviour of directly required cubes will never change (except for critical bugs).</p>
197
+
198
+ <h2 title="require 'zucker/all'">Cubes</h2>
199
+ <div class="cubes">
200
+ <h3 title="require 'zucker/1/class2proc'">class2proc</h3>
201
+ <table class="cube_table"
202
+ id="class2proc_cube"
203
+ title="require 'zucker/1/class2proc'"> <tr><th>Summary</th> <td>Creates a new instance of the class.</td></tr><tr><th>Methods/Usage</th> <td><h5>Class#to_proc</h5><pre class="usage source" style="display:block">[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&amp;<span class="co">Set</span>) <span class="c"># =&gt; [ Set[1,2], Set[5,6,7,3] ]</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by Ruby Facets<br/>More about to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_class2proc_specification">(<a href="javascript:show('class2proc_specification')">show</a>)</span>
204
+ <pre class="source" id="class2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">class2proc</span><span class="dl">'</span></span>
205
+ require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
206
+
207
+ describe <span class="s"><span class="dl">'</span><span class="k">Class#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
208
+ it <span class="s"><span class="dl">'</span><span class="k">should create new instances of the class</span><span class="dl">'</span></span> <span class="r">do</span>
209
+ [ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&amp;<span class="co">Set</span>).should == [ <span class="co">Set</span>[<span class="i">1</span>,<span class="i">2</span>], <span class="co">Set</span>[<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ]
210
+ <span class="r">end</span>
211
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_class2proc_source">(<a href="javascript:show('class2proc_source')">show</a>)</span>
212
+ <pre class="source" id="class2proc_source"><span class="r">class</span> <span class="cl">Class</span>
213
+ <span class="r">def</span> <span class="fu">to_proc</span>
214
+ <span class="co">Proc</span>.new <span class="r">do</span> |*args|
215
+ <span class="pc">self</span>.new *args
216
+ <span class="r">end</span>
217
+ <span class="r">end</span>
218
+ <span class="r">end</span>
219
+
220
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/iterate'">iterate</h3>
221
+ <table class="cube_table"
222
+ id="iterate_cube"
223
+ title="require 'zucker/1/iterate'"> <tr><th>Summary</th> <td>Iterate over one or more collections.</td></tr><tr><th>Why?</th> <td>It's like <code>.each</code> with two differences: It feels more like a control structure and you can easily iterate over multiple objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>iterate</h5><pre class="usage source" style="display:block">iterate [<span class="i">1</span>,<span class="i">2</span>], [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>] <span class="r">do</span> |e,f|
224
+ puts <span class="er">”</span><span class="c">#{e},#{f}”</span>
225
+ <span class="r">end</span>
226
+ <span class="c"># outputs</span>
227
+ <span class="c"># 1,3</span>
228
+ <span class="c"># 2,4</span>
229
+ <span class="c"># ,5</span>
230
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_specification">(<a href="javascript:show('iterate_specification')">show</a>)</span>
231
+ <pre class="source" id="iterate_specification">require <span class="s"><span class="dl">'</span><span class="k">iterate</span><span class="dl">'</span></span>
232
+
233
+ describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
234
+ let <span class="sy">:a</span> <span class="r">do</span> [<span class="i">1</span>, <span class="i">2</span>, <span class="i">3</span>] <span class="r">end</span>
235
+ let <span class="sy">:b</span> <span class="r">do</span> <span class="s"><span class="dl">%w|</span><span class="k">a b c d</span><span class="dl">|</span></span> <span class="r">end</span>
236
+ let <span class="sy">:res</span> <span class="r">do</span> <span class="co">Hash</span>.new {[]} <span class="r">end</span>
237
+
238
+ it <span class="s"><span class="dl">'</span><span class="k">should behave like Enumerable#each for a single argument</span><span class="dl">'</span></span> <span class="r">do</span>
239
+ iterate a <span class="r">do</span> |ele|
240
+ res[<span class="sy">:iter</span>] &lt;&lt; ele
241
+ <span class="r">end</span>
242
+
243
+ a.each <span class="r">do</span> |ele|
244
+ res[<span class="sy">:each</span>] &lt;&lt; ele
245
+ <span class="r">end</span>
246
+
247
+ res[<span class="sy">:iter</span>].should == res[<span class="sy">:each</span>]
248
+ <span class="r">end</span>
249
+
250
+ it <span class="s"><span class="dl">'</span><span class="k">should pass the right params to the block</span><span class="dl">'</span></span> <span class="r">do</span>
251
+ res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
252
+ res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
253
+ res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
254
+
255
+
256
+ iterate a, b <span class="r">do</span> |e,f|
257
+ res[<span class="sy">:iter_a_b</span>] &lt;&lt; [e, f]
258
+ <span class="c"># p res[:iter_a_b], e, f</span>
259
+ <span class="r">end</span>
260
+
261
+ res[<span class="sy">:iter_a_b</span>].should == [
262
+ [<span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>],
263
+ [<span class="i">2</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>],
264
+ [<span class="i">3</span>, <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>],
265
+ [<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>],
266
+ ]
267
+
268
+ iterate b, a <span class="r">do</span> |e,f|
269
+ res[<span class="sy">:iter_b_a</span>] &lt;&lt; [e, f]
270
+ <span class="r">end</span>
271
+
272
+ res[<span class="sy">:iter_b_a</span>].should == [
273
+ [<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, <span class="i">1</span>],
274
+ [<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>, <span class="i">2</span>],
275
+ [<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>, <span class="i">3</span>],
276
+ [<span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>, <span class="pc">nil</span>],
277
+ ]
278
+
279
+ <span class="r">end</span>
280
+
281
+ it <span class="s"><span class="dl">'</span><span class="k">should return enumerators if no block is applied</span><span class="dl">'</span></span> <span class="r">do</span>
282
+ <span class="r">end</span>
283
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_iterate_source">(<a href="javascript:show('iterate_source')">show</a>)</span>
284
+ <pre class="source" id="iterate_source"><span class="r">def</span> <span class="fu">iterate</span>(*params)
285
+ <span class="c"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
286
+
287
+ first = params.shift
288
+ <span class="r">if</span> params.empty? <span class="c"># single param - like each</span>
289
+ <span class="r">if</span> block_given?
290
+ first.map{|e| <span class="r">yield</span> e }
291
+ <span class="r">else</span>
292
+ first.map
293
+ <span class="r">end</span>
294
+ <span class="r">else</span>
295
+ padded_first = <span class="co">Array</span>.new( [first, *params].max_by(&amp;<span class="sy">:size</span>).size ){|i| first[i] } <span class="c"># append nils</span>
296
+ obj = padded_first.zip *params
297
+ <span class="r">if</span> block_given?
298
+ obj.map{|es| <span class="r">yield</span> *es }
299
+ <span class="r">else</span>
300
+ obj.map.to_enum
301
+ <span class="r">end</span>
302
+ <span class="r">end</span>
303
+ <span class="r">end</span>
304
+
305
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/unary'">unary</h3>
306
+ <table class="cube_table"
307
+ id="unary_cube"
308
+ title="require 'zucker/1/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Often, you do not care if you get a <code>String</code> or a <code>Symbol</code> as input - but you need one of both when analysing it.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#+@</h5><pre class="usage source" style="display:block">+<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># =&gt; '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"># =&gt; :was_string</span></pre><h5>Symbol#+@</h5><pre class="usage source" style="display:block">+<span class="sy">:was_symbol</span> <span class="c"># =&gt; 'was_symbol'</span></pre><h5>Symbol#-@</h5><pre class="usage source" style="display:block">-<span class="sy">:was_symbol</span> <span class="c"># =&gt; :was_symbol</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by</td></tr><tr><th>Specification</th> <td> <span id="show_unary_specification">(<a href="javascript:show('unary_specification')">show</a>)</span>
309
+ <pre class="source" id="unary_specification">require <span class="s"><span class="dl">'</span><span class="k">unary</span><span class="dl">'</span></span>
310
+
311
+ describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
312
+ it <span class="s"><span class="dl">'</span><span class="k">should convert to_s</span><span class="dl">'</span></span> <span class="r">do</span>
313
+ +(<span class="sy">:matz</span>) == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> <span class="c"># () 1.8 bug</span>
314
+ <span class="r">end</span>
315
+ <span class="r">end</span>
316
+
317
+ describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
318
+ it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
319
+ -<span class="sy">:matz</span> == <span class="sy">:matz</span>
320
+ <span class="r">end</span>
321
+ <span class="r">end</span>
322
+
323
+ describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
324
+ it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
325
+ +<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span>
326
+ <span class="r">end</span>
327
+ <span class="r">end</span>
328
+
329
+ describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
330
+ it <span class="s"><span class="dl">'</span><span class="k">should convert to_sym</span><span class="dl">'</span></span> <span class="r">do</span>
331
+ -<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="sy">:matz</span>
332
+ <span class="r">end</span>
333
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_unary_source">(<a href="javascript:show('unary_source')">show</a>)</span>
334
+ <pre class="source" id="unary_source"><span class="r">class</span> <span class="cl">String</span>
335
+ <span class="r">def</span> <span class="fu">+@</span>
336
+ <span class="pc">self</span>
337
+ <span class="r">end</span>
338
+
339
+ <span class="r">def</span> <span class="fu">-@</span>
340
+ to_sym
341
+ <span class="r">end</span>
342
+ <span class="r">end</span>
343
+
344
+ <span class="r">class</span> <span class="cl">Symbol</span>
345
+ <span class="r">def</span> <span class="fu">+@</span>
346
+ to_s
347
+ <span class="r">end</span>
348
+
349
+ <span class="r">def</span> <span class="fu">-@</span>
350
+ <span class="pc">self</span>
351
+ <span class="r">end</span>
352
+ <span class="r">end</span>
353
+
354
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (+:literal not possible)</td></tr></table> <h3 title="require 'zucker/1/ivars'">ivars</h3>
355
+ <table class="cube_table"
356
+ id="ivars_cube"
357
+ title="require 'zucker/1/ivars'"> <tr><th>Summary</th> <td>This method lets you easily assign lots of instance variables.</td></tr><tr><th>Why?</th> <td>Often, you have to write boilerplate code for assigning instance varialbles, for example this one:<br/><code>def initialize(variable1, variable2)<br/>  @variable1, @variable2 = variable1, variable2<br/>end</code><br/></td></tr><tr><th>Methods/Usage</th> <td><h5>instance_variables_from, ivars</h5><pre class="usage source" style="display:block"><span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
358
+ instance_variables_from binding <span class="c"># assigns @a and @b</span>
359
+
360
+ params = {<span class="sy">:c</span> =&gt; <span class="i">3</span>, <span class="sy">:d</span> =&gt; <span class="i">4</span>}
361
+ ivars params <span class="c"># # assigns @c and @d</span>
362
+ <span class="r">end</span>
363
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_specification">(<a href="javascript:show('ivars_specification')">show</a>)</span>
364
+ <pre class="source" id="ivars_specification">require <span class="s"><span class="dl">'</span><span class="k">ivars</span><span class="dl">'</span></span>
365
+
366
+ describe <span class="s"><span class="dl">'</span><span class="k">instance_variables_from</span><span class="dl">'</span></span> <span class="r">do</span>
367
+ it <span class="s"><span class="dl">'</span><span class="k">should tansform the given parameter to instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
368
+ <span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
369
+ instance_variables_from binding <span class="c"># assigns @a and @b</span>
370
+
371
+ params = {<span class="sy">:c</span> =&gt; <span class="i">3</span>, <span class="sy">:d</span> =&gt; <span class="i">4</span>}
372
+ ivars params <span class="c"># # assigns @c and @d</span>
373
+ <span class="r">end</span>
374
+
375
+ a_method
376
+ <span class="iv">@a</span>.should == <span class="i">1</span>
377
+ <span class="iv">@b</span>.should == <span class="i">2</span>
378
+ <span class="iv">@c</span>.should == <span class="i">3</span>
379
+ <span class="iv">@d</span>.should == <span class="i">4</span>
380
+
381
+ <span class="r">end</span>
382
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_ivars_source">(<a href="javascript:show('ivars_source')">show</a>)</span>
383
+ <pre class="source" id="ivars_source"><span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
384
+ iter =
385
+ <span class="r">if</span> obj.is_a? <span class="co">Binding</span>
386
+ obj.eval(<span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span>).map{|e| [obj.eval(e), e] }
387
+ <span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
388
+ obj.map{|k,v| [v,k] }
389
+ <span class="r">else</span>
390
+ <span class="c"># elsif obj.is_a? Enumerable</span>
391
+ obj.each.with_index
392
+ <span class="r">end</span>
393
+
394
+ ret = []
395
+ iter.each{ |value, arg|
396
+ arg = arg.to_s
397
+ <span class="r">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
398
+ arg = <span class="s"><span class="dl">'</span><span class="k">_</span><span class="dl">'</span></span> + arg <span class="r">if</span> (<span class="i">48</span>..<span class="i">57</span>).member? arg.unpack(<span class="s"><span class="dl">'</span><span class="k">C</span><span class="dl">'</span></span>)[<span class="i">0</span>] <span class="c"># 1.8+1.9</span>
399
+ ret &lt;&lt; ivar = <span class="sy"><span class="sy">:</span><span class="dl">&quot;</span><span class="k">@</span><span class="il"><span class="idl">#{</span>arg<span class="idl">}</span></span><span class="dl">&quot;</span></span>
400
+ <span class="pc">self</span>.instance_variable_set ivar, value
401
+ <span class="r">end</span>
402
+ }
403
+ ret
404
+ <span class="r">end</span>
405
+ <span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
406
+
407
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/D'">D</h3>
408
+ <table class="cube_table"
409
+ id="D_cube"
410
+ title="require 'zucker/1/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>
411
+ <span class="c"># ...outputs 4 lines with the inspected object</span>
412
+ <span class="c"># =&gt; (result)</span>
413
+
414
+ <span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).<span class="co">D</span>
415
+ <span class="c"># outputs -0.916521547915634</span>
416
+ <span class="c"># =&gt; 20.0834784520844</span>
417
+
418
+ name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
419
+ <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">&quot;</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">&quot;</span></span>}
420
+ <span class="c"># outputs &quot;The length is: 5&quot;</span>
421
+ <span class="c"># =&gt; 'Hello Earth'</span>
422
+ </pre></td></tr><tr><th>Further information</th> <td>This is inspired by the funny <code>.tap</code> method: http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions</td></tr><tr><th>Specification</th> <td> <span id="show_D_specification">(<a href="javascript:show('D_specification')">show</a>)</span>
423
+ <pre class="source" id="D_specification">require <span class="s"><span class="dl">'</span><span class="k">D</span><span class="dl">'</span></span>
424
+
425
+ describe <span class="s"><span class="dl">'</span><span class="k">Object#D</span><span class="dl">'</span></span> <span class="r">do</span>
426
+
427
+ let <span class="sy">:a</span> <span class="r">do</span>
428
+ [ <span class="i">1</span>, <span class="s"><span class="dl">&quot;</span><span class="k">1</span><span class="dl">&quot;</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> =&gt; <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span> ]
429
+ <span class="r">end</span>
430
+
431
+ it <span class="s"><span class="dl">'</span><span class="k">should not change the object&quot;s value</span><span class="dl">'</span></span> <span class="r">do</span>
432
+ a.each{ |e|
433
+ (e.<span class="co">D</span>).should == e
434
+ }
435
+ <span class="r">end</span>
436
+
437
+ it <span class="s"><span class="dl">&quot;</span><span class="k">should puts .inspect if no block is given (and not change the object's value)</span><span class="dl">&quot;</span></span> <span class="r">do</span>
438
+ <span class="c"># a.each{ |e|</span>
439
+ <span class="c"># (e.D).should == e</span>
440
+ <span class="c"># }</span>
441
+ <span class="r">end</span>
442
+
443
+ it <span class="s"><span class="dl">&quot;</span><span class="k">should puts the block if it is given (and not change the object's value)</span><span class="dl">&quot;</span></span> <span class="r">do</span>
444
+ a.each{ |e|
445
+ (e.<span class="co">D</span>{|value| <span class="s"><span class="dl">&quot;</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">&quot;</span></span>}).should == e
446
+ }
447
+ <span class="r">end</span>
448
+ <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>
449
+ <pre class="source" id="D_source"><span class="r">module</span> <span class="cl">Kernel</span>
450
+ <span class="r">def</span> <span class="fu">D</span>(*args, &amp;block)
451
+ <span class="r">if</span> args.empty?
452
+ tap{
453
+ <span class="r">if</span> block_given?
454
+ puts <span class="r">yield</span> <span class="pc">self</span>
455
+ <span class="r">else</span>
456
+ puts <span class="pc">self</span>.inspect
457
+ <span class="r">end</span>
458
+ }
459
+ <span class="r">else</span>
460
+ raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">&quot;</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">&quot;</span></span>
461
+ <span class="c"># eval ...</span>
462
+ <span class="r">end</span>
463
+ <span class="r">end</span>
464
+ <span class="r">end</span>
465
+
466
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/array'">array</h3>
467
+ <table class="cube_table"
468
+ id="array_cube"
469
+ title="require 'zucker/1/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code></td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&</code> and <code>|</code>, but why does it not have <code>^</code>?</p><p><code>sum</code> is an alias for <code>inject(:+)</code>, but it is just needed pretty often!</p></td></tr><tr><th>Methods/Usage</th> <td><h5>Array#^</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>] ^ [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># =&gt; [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"># =&gt; 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"># =&gt; 'Hallo'</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_array_specification">(<a href="javascript:show('array_specification')">show</a>)</span>
470
+ <pre class="source" id="array_specification">require <span class="s"><span class="dl">'</span><span class="k">array</span><span class="dl">'</span></span>
471
+
472
+ describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
473
+ 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>
474
+ a = [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>]
475
+ b = [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>]
476
+ (a^b).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">5</span>,<span class="i">6</span>]
477
+ <span class="r">end</span>
478
+ <span class="r">end</span>
479
+
480
+ describe <span class="s"><span class="dl">'</span><span class="k">Array#sum</span><span class="dl">'</span></span> <span class="r">do</span>
481
+ it <span class="s"><span class="dl">'</span><span class="k">should sum the array</span><span class="dl">'</span></span> <span class="r">do</span>
482
+ [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum.should == <span class="i">15</span>
483
+ <span class="s"><span class="dl">%w|</span><span class="k">More Ruby</span><span class="dl">|</span></span>.sum.should == <span class="s"><span class="dl">'</span><span class="k">MoreRuby</span><span class="dl">'</span></span>
484
+ <span class="r">end</span>
485
+ <span class="r">end</span>
486
+
487
+ describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</span><span class="dl">'</span></span> <span class="r">do</span>
488
+ it <span class="s"><span class="dl">'</span><span class="k">should convert the array to a string, using each element as ord value for the char</span><span class="dl">'</span></span> <span class="r">do</span>
489
+ [<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs.should == <span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>
490
+ <span class="r">end</span>
491
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_array_source">(<a href="javascript:show('array_source')">show</a>)</span>
492
+ <pre class="source" id="array_source"><span class="r">class</span> <span class="cl">Array</span>
493
+ <span class="r">def</span> <span class="fu">^</span>(other) <span class="c"># TODO: more efficient</span>
494
+ (<span class="pc">self</span> - other) +
495
+ (other - <span class="pc">self</span>)
496
+ <span class="r">end</span>
497
+
498
+ <span class="r">def</span> <span class="fu">sum</span> <span class="c"># sry, you just need this one too often</span>
499
+ inject <span class="sy">:+</span>
500
+ <span class="r">end</span>
501
+
502
+ <span class="r">def</span> <span class="fu">chrs</span>
503
+ <span class="pc">self</span>.pack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
504
+ <span class="r">end</span>
505
+ <span class="r">end</span>
506
+
507
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/array2proc'">array2proc</h3>
508
+ <table class="cube_table"
509
+ id="array2proc_cube"
510
+ title="require 'zucker/1/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( &amp;[<span class="sy">:to_s</span>, <span class="i">2</span>] ) <span class="c"># =&gt; [&quot;1&quot;, &quot;10&quot;, &quot;11&quot;, &quot;100&quot;]</span></pre></td></tr><tr><th>Further information</th> <td>Inspired by http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby<br/>More about to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_array2proc_specification">(<a href="javascript:show('array2proc_specification')">show</a>)</span>
511
+ <pre class="source" id="array2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">array2proc</span><span class="dl">'</span></span>
512
+
513
+ describe <span class="s"><span class="dl">'</span><span class="k">Array#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
514
+ 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>
515
+ [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &amp;[<span class="sy">:to_s</span>, <span class="i">2</span>] ).should == [<span class="s"><span class="dl">&quot;</span><span class="k">1</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">10</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">11</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">100</span><span class="dl">&quot;</span></span>]
516
+ <span class="r">end</span>
517
+ <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>
518
+ <pre class="source" id="array2proc_source"><span class="r">class</span> <span class="cl">Array</span>
519
+ <span class="r">def</span> <span class="fu">to_proc</span>
520
+ <span class="co">Proc</span>.new{ |obj|
521
+ obj.send *<span class="pc">self</span>
522
+ }
523
+ <span class="r">end</span>
524
+ <span class="r">end</span>
525
+
526
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/square_brackets_for'">square_brackets_for</h3>
527
+ <table class="cube_table"
528
+ id="square_brackets_for_cube"
529
+ title="require 'zucker/1/square_brackets_for'"> <tr><th>Summary</th> <td>This helper methods defines <code>[]</code> and <code>[]=</code> for accesing an instance variable.</td></tr><tr><th>Methods/Usage</th> <td><h5>square_brackets_for</h5><pre class="usage source" style="display:block"><span class="r">class</span> <span class="cl">Klass</span>
530
+ <span class="r">def</span> <span class="fu">initialize</span>
531
+ <span class="iv">@var</span> = {
532
+ <span class="sy">:a_key</span> =&gt; <span class="i">1</span>,
533
+ <span class="sy">:another_one</span> =&gt; <span class="i">2</span>,
534
+ }
535
+ <span class="r">end</span>
536
+
537
+ square_brackets_for <span class="sy">:var</span> <span class="c"># creates [] and []=</span>
538
+ <span class="c"># square_brackets_for :var, false # would create only []</span>
539
+ <span class="r">end</span>
540
+
541
+ a = <span class="co">Klass</span>.new
542
+ a[<span class="sy">:a_key</span>] <span class="c"># =&gt; 1</span>
543
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_specification">(<a href="javascript:show('square_brackets_for_specification')">show</a>)</span>
544
+ <pre class="source" id="square_brackets_for_specification">require <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span>
545
+
546
+ describe <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span> <span class="r">do</span>
547
+
548
+ before <span class="r">do</span>
549
+ <span class="r">class</span> <span class="cl">Klass</span>
550
+ <span class="r">def</span> <span class="fu">initialize</span>
551
+ <span class="iv">@var</span> = {
552
+ <span class="sy">:a_key</span> =&gt; <span class="i">1</span>,
553
+ <span class="sy">:another_one</span> =&gt; <span class="i">2</span>,
554
+ }
555
+ <span class="r">end</span>
556
+
557
+ <span class="iv">@eigenvar</span> = {
558
+ <span class="sy">:a_key</span> =&gt; <span class="i">99</span>
559
+ }
560
+ <span class="r">end</span>
561
+ <span class="r">end</span>
562
+
563
+ it <span class="s"><span class="dl">'</span><span class="k">should define a [] getter (not a setter) for an instance var, if the second parameter is false</span><span class="dl">'</span></span> <span class="r">do</span>
564
+
565
+ <span class="r">class</span> <span class="cl">Klass</span>
566
+ square_brackets_for <span class="sy">:var</span>, <span class="pc">nil</span>
567
+ <span class="r">end</span>
568
+
569
+ a = <span class="co">Klass</span>.new
570
+ a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
571
+
572
+ proc <span class="r">do</span>
573
+ a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">not possible</span><span class="dl">'</span></span>
574
+ <span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
575
+ <span class="r">end</span>
576
+
577
+ it <span class="s"><span class="dl">'</span><span class="k">should define [] and []= for accessing an instance variable</span><span class="dl">'</span></span> <span class="r">do</span>
578
+
579
+ <span class="r">class</span> <span class="cl">Klass</span>
580
+ square_brackets_for <span class="sy">:var</span>
581
+ <span class="r">end</span>
582
+
583
+ a = <span class="co">Klass</span>.new
584
+ a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
585
+
586
+ a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
587
+ a[<span class="sy">:this_is</span>].should == <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
588
+ <span class="r">end</span>
589
+
590
+ it <span class="s"><span class="dl">'</span><span class="k">should also work for class-instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
591
+
592
+ <span class="r">class</span> <span class="cl">Klass</span>
593
+ <span class="r">class</span> &lt;&lt; <span class="cl">Klass</span>
594
+ square_brackets_for <span class="sy">:eigenvar</span>
595
+ <span class="r">end</span>
596
+ <span class="r">end</span>
597
+
598
+ <span class="co">Klass</span>[<span class="sy">:a_key</span>].should == <span class="i">99</span>
599
+ <span class="r">end</span>
600
+
601
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_square_brackets_for_source">(<a href="javascript:show('square_brackets_for_source')">show</a>)</span>
602
+ <pre class="source" id="square_brackets_for_source"><span class="r">def</span> <span class="fu">square_brackets_for</span>(ivar, assignment = <span class="pc">true</span>)
603
+ <span class="c"># undef [] if respond_to? :[]</span>
604
+ <span class="c"># undef []= if respond_to? :[]=</span>
605
+
606
+ <span class="c">#instance_eval do</span>
607
+ define_method <span class="sy">:[]</span> <span class="r">do</span> |key|
608
+ (instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">&quot;</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">&quot;</span></span>)[key]
609
+ <span class="r">end</span>
610
+
611
+ <span class="r">if</span> assignment
612
+ define_method <span class="sy">:[]=</span> <span class="r">do</span> |key, value|
613
+ (instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">&quot;</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">&quot;</span></span>)[key] = value
614
+ <span class="r">end</span>
615
+ <span class="r">end</span>
616
+ <span class="c">#end</span>
617
+
618
+ <span class="r">end</span>
619
+
620
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/mm'">mm</h3>
621
+ <table class="cube_table"
622
+ id="mm_cube"
623
+ title="require 'zucker/1/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 http://rbjl.net/31-the-multi-mega-method-list for more information</td></tr><tr><th>Specification</th> <td> <span id="show_mm_specification">(<a href="javascript:show('mm_specification')">show</a>)</span>
624
+ <pre class="source" id="mm_specification">require <span class="s"><span class="dl">'</span><span class="k">mm</span><span class="dl">'</span></span>
625
+
626
+ 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>
627
+ it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span>
628
+ <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>
629
+ <pre class="source" id="mm_source"><span class="r">module</span> <span class="cl">Kernel</span>
630
+ <span class="r">def</span> <span class="fu">method_list</span>(levels = <span class="i">1</span>)
631
+ <span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
632
+ klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
633
+ <span class="r">else</span>
634
+ klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
635
+
636
+ eigen = <span class="pc">self</span>.singleton_methods
637
+ <span class="r">if</span> !eigen.empty?
638
+ puts <span class="sy">:Eigenclass</span> <span class="c"># sorry for not being up to date, I just love the word</span>
639
+ p <span class="pc">self</span>.singleton_methods
640
+ <span class="r">end</span>
641
+ <span class="r">end</span>
642
+
643
+ levels.times{ |level|
644
+ <span class="r">if</span> cur = klass.ancestors[level]
645
+ p cur <span class="c"># put class name</span>
646
+ p cur.send method_function, <span class="pc">false</span> <span class="c"># put methods of the class</span>
647
+ <span class="r">else</span>
648
+ <span class="r">break</span>
649
+ <span class="r">end</span>
650
+ }
651
+
652
+ <span class="pc">self</span> <span class="c"># or whatever</span>
653
+ <span class="r">end</span>
654
+
655
+ <span class="r">alias</span> <span class="fu">mm</span> <span class="fu">method_list</span>
656
+ <span class="r">end</span>
657
+
658
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/regexp2proc'">regexp2proc</h3>
659
+ <table class="cube_table"
660
+ id="regexp2proc_cube"
661
+ title="require 'zucker/1/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 &amp;<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># =&gt; [&quot;j&quot;, nil, nil, &quot;y&quot;]</span>
662
+ <span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select &amp;<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># =&gt; [&quot;just&quot;, &quot;array&quot;]</span>
663
+ </pre></td></tr><tr><th>Further information</th> <td>More about to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_regexp2proc_specification">(<a href="javascript:show('regexp2proc_specification')">show</a>)</span>
664
+ <pre class="source" id="regexp2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">regexp2proc</span><span class="dl">'</span></span>
665
+
666
+ 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>
667
+ 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>
668
+ <span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map( &amp;<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">&quot;</span><span class="k">j</span><span class="dl">&quot;</span></span>, <span class="pc">nil</span>, <span class="pc">nil</span>, <span class="s"><span class="dl">&quot;</span><span class="k">y</span><span class="dl">&quot;</span></span>]
669
+ <span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select( &amp;<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">&quot;</span><span class="k">just</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">array</span><span class="dl">&quot;</span></span>]
670
+ <span class="r">end</span>
671
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_regexp2proc_source">(<a href="javascript:show('regexp2proc_source')">show</a>)</span>
672
+ <pre class="source" id="regexp2proc_source"><span class="r">class</span> <span class="cl">Regexp</span>
673
+ <span class="r">def</span> <span class="fu">to_proc</span>
674
+ proc <span class="r">do</span> |e|
675
+ e.to_s[<span class="pc">self</span>]
676
+ <span class="r">end</span>
677
+ <span class="r">end</span>
678
+ <span class="r">end</span>
679
+
680
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/mcopy'">mcopy</h3>
681
+ <table class="cube_table"
682
+ id="mcopy_cube"
683
+ title="require 'zucker/1/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>
684
+ b = a.mcopy
685
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_specification">(<a href="javascript:show('mcopy_specification')">show</a>)</span>
686
+ <pre class="source" id="mcopy_specification">require <span class="s"><span class="dl">'</span><span class="k">mcopy</span><span class="dl">'</span></span>
687
+
688
+ describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
689
+ it <span class="s"><span class="dl">'</span><span class="k">create a (deep) copy via marshalling</span><span class="dl">'</span></span> <span class="r">do</span>
690
+ a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
691
+ b = a.mcopy
692
+ b.should == a
693
+
694
+ b[<span class="i">0</span>][<span class="i">1</span>,<span class="i">1</span>] = <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
695
+ b.should_not == a
696
+ <span class="r">end</span>
697
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mcopy_source">(<a href="javascript:show('mcopy_source')">show</a>)</span>
698
+ <pre class="source" id="mcopy_source"><span class="r">class</span> <span class="cl">Object</span>
699
+ <span class="r">def</span> <span class="fu">mcopy</span>
700
+ <span class="co">Marshal</span>.load <span class="co">Marshal</span>.dump <span class="pc">self</span>
701
+ <span class="r">end</span>
702
+ <span class="c"># alias copy mcopy</span>
703
+ <span class="r">end</span>
704
+
705
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/hash2proc'">hash2proc</h3>
706
+ <table class="cube_table"
707
+ id="hash2proc_cube"
708
+ title="require 'zucker/1/hash2proc'"> <tr><th>Summary</th> <td>Use a hash to apply procs to specific objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash#to_proc</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&amp;{
709
+ <span class="i">2</span> =&gt; lambda {|e| e + <span class="i">1000</span>},
710
+ <span class="i">4</span> =&gt; <span class="sy">:to_s</span>,
711
+ }) <span class="c"># =&gt; [1, 1002, 3, '4']</span></pre></td></tr><tr><th>Further information</th> <td>More about to_proc: http://rbjl.net/29-become-a-proc-star</td></tr><tr><th>Specification</th> <td> <span id="show_hash2proc_specification">(<a href="javascript:show('hash2proc_specification')">show</a>)</span>
712
+ <pre class="source" id="hash2proc_specification">require <span class="s"><span class="dl">'</span><span class="k">hash2proc</span><span class="dl">'</span></span>
713
+
714
+ describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
715
+ 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>
716
+ [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&amp;{
717
+ <span class="i">4</span> =&gt; <span class="sy">:to_s</span>,
718
+ <span class="c"># 3 =&gt; [:to_s, 2] # &quot;11&quot; =&gt; if array2proc is used</span>
719
+ <span class="i">2</span> =&gt; lambda {|e| e + <span class="i">1000</span>}
720
+ }).should == [<span class="i">1</span>, <span class="i">1002</span>, <span class="i">3</span>, <span class="s"><span class="dl">&quot;</span><span class="k">4</span><span class="dl">&quot;</span></span>]
721
+ <span class="r">end</span>
722
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash2proc_source">(<a href="javascript:show('hash2proc_source')">show</a>)</span>
723
+ <pre class="source" id="hash2proc_source"><span class="r">class</span> <span class="cl">Hash</span>
724
+ <span class="r">def</span> <span class="fu">to_proc</span>
725
+ <span class="co">Proc</span>.new{ |obj|
726
+ <span class="r">if</span> <span class="pc">self</span>.member? obj
727
+ <span class="pc">self</span>[obj].to_proc.call obj
728
+ <span class="r">else</span>
729
+ obj
730
+ <span class="r">end</span>
731
+ }
732
+ <span class="r">end</span>
733
+ <span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/enumerable'">enumerable</h3>
734
+ <table class="cube_table"
735
+ id="enumerable_cube"
736
+ title="require 'zucker/1/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"># =&gt; {1=&gt;'1',2=&gt;'2',3=&gt;'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_specification">(<a href="javascript:show('enumerable_specification')">show</a>)</span>
737
+ <pre class="source" id="enumerable_specification">require <span class="s"><span class="dl">'</span><span class="k">enumerable</span><span class="dl">'</span></span>
738
+
739
+ describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mash</span><span class="dl">'</span></span> <span class="r">do</span>
740
+ it <span class="s"><span class="dl">'</span><span class="k">should &quot;map&quot; a hash</span><span class="dl">'</span></span> <span class="r">do</span>
741
+ [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] }.should == {<span class="i">1</span>=&gt;<span class="s"><span class="dl">'</span><span class="k">1</span><span class="dl">'</span></span>,<span class="i">2</span>=&gt;<span class="s"><span class="dl">'</span><span class="k">2</span><span class="dl">'</span></span>,<span class="i">3</span>=&gt;<span class="s"><span class="dl">'</span><span class="k">3</span><span class="dl">'</span></span>,}
742
+ <span class="r">end</span>
743
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_enumerable_source">(<a href="javascript:show('enumerable_source')">show</a>)</span>
744
+ <pre class="source" id="enumerable_source"><span class="r">module</span> <span class="cl">Enumerable</span>
745
+ <span class="r">def</span> <span class="fu">mash</span>
746
+ ret = {}
747
+ each{ |kv|
748
+ ret.store *( <span class="r">yield</span>(kv)[<span class="i">0</span>,<span class="i">2</span>] )
749
+ }
750
+ ret
751
+ <span class="r">end</span>
752
+ <span class="r">end</span>
753
+
754
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/union'">union</h3>
755
+ <table class="cube_table"
756
+ id="union_cube"
757
+ title="require 'zucker/1/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">&quot;</span><span class="k">cheat</span><span class="dl">&quot;</span></span>
758
+ <span class="c"># creates a Regexp similar to:</span>
759
+ <span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
760
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_union_specification">(<a href="javascript:show('union_specification')">show</a>)</span>
761
+ <pre class="source" id="union_specification">require <span class="s"><span class="dl">'</span><span class="k">union</span><span class="dl">'</span></span>
762
+
763
+ describe <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span> <span class="r">do</span>
764
+ 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>
765
+ (<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 ==
766
+ <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> )
767
+ <span class="r">end</span>
768
+ <span class="r">end</span>
769
+
770
+ describe <span class="s"><span class="dl">'</span><span class="k">String#|</span><span class="dl">'</span></span> <span class="r">do</span>
771
+ it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span>
772
+ <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>
773
+ <pre class="source" id="union_source"><span class="r">class</span> <span class="cl">Regexp</span>
774
+ <span class="r">def</span> <span class="fu">|</span>(arg)
775
+ <span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
776
+ <span class="r">end</span>
777
+ <span class="r">end</span>
778
+
779
+ <span class="r">class</span> <span class="cl">String</span>
780
+ <span class="r">def</span> <span class="fu">|</span>(arg)
781
+ <span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
782
+ <span class="r">end</span>
783
+ <span class="r">end</span>
784
+
785
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr></table> <h3 title="require 'zucker/1/kernel'">kernel</h3>
786
+ <table class="cube_table"
787
+ id="kernel_cube"
788
+ title="require 'zucker/1/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_specification">(<a href="javascript:show('kernel_specification')">show</a>)</span>
789
+ <pre class="source" id="kernel_specification">require <span class="s"><span class="dl">'</span><span class="k">kernel</span><span class="dl">'</span></span>
790
+
791
+ describe <span class="s"><span class="dl">'</span><span class="k">activate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
792
+ 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>
793
+ activate_warnings!
794
+ <span class="gv">$VERBOSE</span>.should == <span class="pc">true</span>
795
+ <span class="r">end</span>
796
+ <span class="r">end</span>
797
+
798
+ describe <span class="s"><span class="dl">'</span><span class="k">deactivate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
799
+ it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to false</span><span class="dl">'</span></span> <span class="r">do</span>
800
+ deactivate_warnings!
801
+ <span class="gv">$VERBOSE</span>.should == <span class="pc">false</span>
802
+ <span class="r">end</span>
803
+ <span class="r">end</span>
804
+
805
+ describe <span class="s"><span class="dl">'</span><span class="k">warnings_activated?</span><span class="dl">'</span></span> <span class="r">do</span>
806
+ 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>
807
+ warnings_activated?.should == <span class="gv">$VERBOSE</span>
808
+ <span class="r">end</span>
809
+ <span class="r">end</span>
810
+
811
+ describe <span class="s"><span class="dl">'</span><span class="k">library?</span><span class="dl">'</span></span> <span class="r">do</span>
812
+ 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>
813
+ library?.should == ( <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span> )
814
+ <span class="r">end</span>
815
+ <span class="r">end</span>
816
+
817
+ describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!</span><span class="dl">'</span></span> <span class="r">do</span>
818
+ it <span class="s"><span class="dl">'</span><span class="k">should catch ctrl+c signals</span><span class="dl">'</span></span> <span class="r">do</span>
819
+ <span class="c"># ...</span>
820
+ <span class="r">end</span>
821
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_kernel_source">(<a href="javascript:show('kernel_source')">show</a>)</span>
822
+ <pre class="source" id="kernel_source"><span class="r">def</span> <span class="fu">activate_warnings!</span>
823
+ <span class="gv">$VERBOSE</span> = <span class="pc">true</span>
824
+ <span class="r">end</span>
825
+
826
+ <span class="r">def</span> <span class="fu">deactivate_warnings!</span>
827
+ <span class="gv">$VERBOSE</span> = <span class="pc">false</span>
828
+ <span class="r">end</span>
829
+
830
+ <span class="r">def</span> <span class="fu">warnings_activated?</span>
831
+ <span class="gv">$VERBOSE</span>
832
+ <span class="r">end</span>
833
+
834
+ <span class="r">def</span> <span class="fu">library?</span>
835
+ <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span>
836
+ <span class="r">end</span>
837
+
838
+ <span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
839
+ <span class="co">Signal</span>.trap *<span class="s"><span class="dl">%w|</span><span class="k">SIGINT IGNORE</span><span class="dl">|</span></span>
840
+ <span class="r">end</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/hash'">hash</h3>
841
+ <table class="cube_table"
842
+ id="hash_cube"
843
+ title="require 'zucker/1/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"># =&gt; {1 =&gt; 4, 2 =&gt; 5, 3 =&gt; 6}</span></pre><h5>Hash#<<</h5><pre class="usage source" style="display:block">{<span class="i">1</span> =&gt; <span class="i">2</span>} &lt;&lt; [<span class="i">3</span>, <span class="i">4</span>] <span class="c"># =&gt; {1 =&gt; 2, 3 =&gt; 4}</span></pre></td></tr><tr><th>Further information</th> <td>Some of the operators are inspired by Ruby Facets</td></tr><tr><th>Specification</th> <td> <span id="show_hash_specification">(<a href="javascript:show('hash_specification')">show</a>)</span>
844
+ <pre class="source" id="hash_specification">require <span class="s"><span class="dl">'</span><span class="k">hash</span><span class="dl">'</span></span>
845
+
846
+ describe <span class="s"><span class="dl">'</span><span class="k">Hash.zip</span><span class="dl">'</span></span> <span class="r">do</span>
847
+ it <span class="s"><span class="dl">'</span><span class="k">should zip together both given enumerables and take them as key=&gt;values for a new hash</span><span class="dl">'</span></span>
848
+ <span class="r">end</span>
849
+
850
+ describe <span class="s"><span class="dl">'</span><span class="k">Hash#&lt;&lt;</span><span class="dl">'</span></span> <span class="r">do</span>
851
+ it <span class="s"><span class="dl">'</span><span class="k">should append new elements to the hash</span><span class="dl">'</span></span>
852
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash_source">(<a href="javascript:show('hash_source')">show</a>)</span>
853
+ <pre class="source" id="hash_source"><span class="r">class</span> <span class="cl">Hash</span>
854
+ <span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
855
+ <span class="co">Hash</span>[ *[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].zip( [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] ).flatten ]
856
+ <span class="r">end</span>
857
+
858
+ <span class="r">def</span> <span class="fu">&lt;&lt;</span>(other)
859
+ <span class="r">case</span>
860
+ <span class="r">when</span> other.is_a?(<span class="co">Hash</span>)
861
+ merge! other
862
+ <span class="r">when</span> other.respond_to?(<span class="sy">:to_splat</span>)
863
+ merge! <span class="co">Hash</span>[*other]
864
+ <span class="r">else</span>
865
+ raise <span class="co">TypeError</span>, <span class="s"><span class="dl">'</span><span class="k">can only append other Hashs and Enumerables</span><span class="dl">'</span></span>
866
+ <span class="r">end</span>
867
+ <span class="r">end</span>
868
+ <span class="r">end</span>
869
+
870
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/aliases'">aliases</h3>
871
+ <table class="cube_table"
872
+ id="aliases_cube"
873
+ title="require 'zucker/1/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_specification">(<a href="javascript:show('aliases_specification')">show</a>)</span>
874
+ <pre class="source" id="aliases_specification">require <span class="s"><span class="dl">'</span><span class="k">aliases</span><span class="dl">'</span></span>
875
+
876
+ describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</span><span class="dl">'</span></span> <span class="r">do</span>
877
+ it <span class="s"><span class="dl">'</span><span class="k">should create these aliases [ sorry, no real spec for this one :P ] </span><span class="dl">'</span></span> <span class="r">do</span>
878
+ <span class="c"># alias is_an? is_a?</span>
879
+
880
+ <span class="c"># module Enumerable</span>
881
+ <span class="c"># alias with zip</span>
882
+ <span class="c"># alias % zip</span>
883
+ <span class="c"># end</span>
884
+
885
+ <span class="c"># class Array</span>
886
+ <span class="c"># alias ** product</span>
887
+ <span class="c"># end</span>
888
+
889
+ <span class="c"># class Hash</span>
890
+ <span class="c"># alias + merge</span>
891
+ <span class="c"># end</span>
892
+
893
+ <span class="c"># class Binding</span>
894
+ <span class="c"># #alias [] eval</span>
895
+ <span class="c"># def [](expr)</span>
896
+ <span class="c"># self.eval &quot;#{expr}&quot;</span>
897
+ <span class="c"># end</span>
898
+ <span class="c"># end</span>
899
+
900
+ <span class="r">end</span>
901
+
902
+ 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>
903
+ <span class="co">Infinity</span>.finite?.should == <span class="pc">false</span>
904
+ <span class="co">NaN</span>.nan?.should == <span class="pc">true</span>
905
+ <span class="r">end</span>
906
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_aliases_source">(<a href="javascript:show('aliases_source')">show</a>)</span>
907
+ <pre class="source" id="aliases_source"><span class="r">alias</span> <span class="fu">is_an?</span> <span class="fu">is_a?</span>
908
+
909
+ <span class="r">module</span> <span class="cl">Enumerable</span>
910
+ <span class="r">alias</span> <span class="fu">with</span> <span class="fu">zip</span>
911
+ <span class="r">alias</span> <span class="fu">%</span> <span class="fu">zip</span>
912
+ <span class="r">end</span>
913
+
914
+ <span class="r">class</span> <span class="cl">Array</span>
915
+ <span class="r">alias</span> <span class="fu">**</span> <span class="fu">product</span>
916
+ <span class="r">end</span>
917
+
918
+ <span class="r">class</span> <span class="cl">Hash</span>
919
+ <span class="r">alias</span> <span class="fu">+</span> <span class="fu">merge</span>
920
+ <span class="r">end</span>
921
+
922
+ <span class="r">class</span> <span class="cl">Binding</span>
923
+ <span class="c">#alias [] eval</span>
924
+ <span class="r">def</span> <span class="fu">[]</span>(expr)
925
+ <span class="pc">self</span>.eval <span class="s"><span class="dl">&quot;</span><span class="il"><span class="idl">#{</span>expr<span class="idl">}</span></span><span class="dl">&quot;</span></span>
926
+ <span class="r">end</span>
927
+ <span class="r">end</span>
928
+
929
+ <span class="c"># constants - who would use these in real-world code for other things?</span>
930
+ <span class="co">Infinity</span> = <span class="fl">1.0</span> / <span class="fl">0.0</span> <span class="c"># or 2*Float::MAX</span>
931
+ <span class="co">NaN</span> = <span class="fl">0.0</span> / <span class="fl">0.0</span>
932
+
933
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/sandbox'">sandbox</h3>
934
+ <table class="cube_table"
935
+ id="sandbox_cube"
936
+ title="require 'zucker/1/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (--> integrated) in the <code>$SAFE</code> concept</td></tr><tr><th>Methods/Usage</th> <td><h5>sandbox</h5><pre class="usage source" style="display:block">sandbox <span class="r">do</span>
937
+ <span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
938
+ <span class="r">end</span>
939
+ <span class="c"># everything's normal again</span>
940
+ </pre></td></tr><tr><th>Specification</th> <td> <span id="show_sandbox_specification">(<a href="javascript:show('sandbox_specification')">show</a>)</span>
941
+ <pre class="source" id="sandbox_specification">require <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span>
942
+
943
+ describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
944
+ 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>
945
+ it <span class="s"><span class="dl">'</span><span class="k">should run the lambda passed as first parameter for errors, if it is given</span><span class="dl">'</span></span>
946
+ <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>
947
+ <pre class="source" id="sandbox_source"><span class="r">def</span> <span class="fu">sandbox</span>(rescueblock=<span class="pc">nil</span>)
948
+ <span class="co">Thread</span>.start <span class="r">do</span>
949
+ <span class="gv">$SAFE</span> = <span class="i">4</span>
950
+ <span class="r">yield</span>
951
+ <span class="r">end</span>.value
952
+ <span class="r">rescue</span> <span class="co">SecurityError</span> =&gt; e
953
+ <span class="r">if</span> rescueblock
954
+ rescueblock.call e
955
+ <span class="r">else</span>
956
+ raise e
957
+ <span class="r">end</span>
958
+ <span class="r">end</span>
959
+
960
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/blank'">blank</h3>
961
+ <table class="cube_table"
962
+ id="blank_cube"
963
+ title="require 'zucker/1/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"># =&gt; false</span></pre></td></tr><tr><th>Further information</th> <td>See ActiveSupport</td></tr><tr><th>Specification</th> <td> <span id="show_blank_specification">(<a href="javascript:show('blank_specification')">show</a>)</span>
964
+ <pre class="source" id="blank_specification">require <span class="s"><span class="dl">'</span><span class="k">blank</span><span class="dl">'</span></span>
965
+
966
+ describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?</span><span class="dl">'</span></span> <span class="r">do</span>
967
+ 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>
968
+ blank_values = [ <span class="pc">nil</span>, <span class="pc">false</span>, <span class="s"><span class="dl">'</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k"> </span><span class="ch">\n</span><span class="ch">\t</span><span class="k"> </span><span class="ch">\r</span><span class="k"> </span><span class="dl">&quot;</span></span>, [], {}, <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> ]
969
+
970
+ blank_values.each{ |blank|
971
+ blank.blank?.should == <span class="pc">true</span>
972
+ }
973
+ <span class="r">end</span>
974
+
975
+ it <span class="s"><span class="dl">'</span><span class="k">should not be blank for non blank values</span><span class="dl">'</span></span> <span class="r">do</span>
976
+ present_values = [ <span class="co">Object</span>.new, <span class="pc">true</span>, <span class="i">0</span>, <span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, [<span class="pc">nil</span>], { <span class="pc">nil</span> =&gt; <span class="pc">nil</span> } ]
977
+
978
+ present_values.each{ |present|
979
+ present.blank?.should == <span class="pc">false</span>
980
+ }
981
+ <span class="r">end</span>
982
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_blank_source">(<a href="javascript:show('blank_source')">show</a>)</span>
983
+ <pre class="source" id="blank_source"><span class="r">class</span> <span class="cl">Object</span>
984
+ <span class="r">def</span> <span class="fu">blank?</span>
985
+ <span class="r">if</span> respond_to? <span class="sy">:empty?</span> <span class="r">then</span> empty? <span class="r">else</span> !<span class="pc">self</span> <span class="r">end</span>
986
+ <span class="r">end</span>
987
+
988
+ <span class="r">def</span> <span class="fu">present?</span>
989
+ !blank?
990
+ <span class="r">end</span>
991
+ <span class="r">end</span>
992
+
993
+
994
+ { <span class="c"># what to do # for which classes</span>
995
+ lambda{ <span class="pc">true</span> } =&gt; [<span class="co">FalseClass</span>, <span class="co">NilClass</span>],
996
+ lambda{ <span class="pc">false</span> } =&gt; [<span class="co">TrueClass</span>, <span class="co">Numeric</span>],
997
+ lambda{ empty? } =&gt; [<span class="co">Array</span>, <span class="co">Hash</span>],
998
+ lambda{ <span class="pc">self</span> !~ <span class="rx"><span class="dl">/</span><span class="ch">\S</span><span class="dl">/</span></span> } =&gt; [<span class="co">String</span>],
999
+ lambda{ <span class="pc">self</span> == <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> } =&gt; [<span class="co">Regexp</span>],
1000
+
1001
+ }.each{ |action, klass_array|
1002
+ klass_array.each{ |klass|
1003
+ klass.send <span class="sy">:define_method</span>, <span class="sy">:blank?</span>, &amp;action
1004
+ }
1005
+ }
1006
+
1007
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/string'">string</h3>
1008
+ <table class="cube_table"
1009
+ id="string_cube"
1010
+ title="require 'zucker/1/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"># =&gt; '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"># =&gt; '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"># =&gt; [72, 97, 108, 108, 111]</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_string_specification">(<a href="javascript:show('string_specification')">show</a>)</span>
1011
+ <pre class="source" id="string_specification">require <span class="s"><span class="dl">'</span><span class="k">string</span><span class="dl">'</span></span>
1012
+
1013
+ describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
1014
+ 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>
1015
+ string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
1016
+
1017
+ (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>
1018
+ (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>
1019
+ (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>
1020
+ (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>
1021
+ (string|<span class="i">38</span>).should == <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
1022
+ (string|<span class="i">99</span>).should == <span class="pc">nil</span>
1023
+ <span class="r">end</span>
1024
+ <span class="r">end</span>
1025
+
1026
+ describe <span class="s"><span class="dl">'</span><span class="k">String#lchomp</span><span class="dl">'</span></span> <span class="r">do</span>
1027
+ it <span class="s"><span class="dl">'</span><span class="k">should chomp on the left side</span><span class="dl">'</span></span> <span class="r">do</span>
1028
+ string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
1029
+ string.lchomp(<span class="s"><span class="dl">'</span><span class="k">T</span><span class="dl">'</span></span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
1030
+ <span class="r">end</span>
1031
+ <span class="r">end</span>
1032
+
1033
+ describe <span class="s"><span class="dl">'</span><span class="k">String#ords</span><span class="dl">'</span></span> <span class="r">do</span>
1034
+ it <span class="s"><span class="dl">'</span><span class="k">should unpack characters</span><span class="dl">'</span></span> <span class="r">do</span>
1035
+ string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
1036
+ string.ords.should == [<span class="i">84</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">101</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">99</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">32</span>, <span class="i">73</span>, <span class="i">110</span>, <span class="i">102</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">109</span>, <span class="i">97</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">107</span>, <span class="i">32</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">116</span>, <span class="i">32</span>, <span class="i">118</span>, <span class="i">111</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">32</span>, <span class="i">103</span>, <span class="i">101</span>, <span class="i">105</span>, <span class="i">108</span>, <span class="i">33</span>]
1037
+ <span class="r">end</span>
1038
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_string_source">(<a href="javascript:show('string_source')">show</a>)</span>
1039
+ <pre class="source" id="string_source"><span class="r">class</span> <span class="cl">String</span>
1040
+ <span class="r">def</span> <span class="fu">^</span>(pos)
1041
+ pos = pos.to_i
1042
+ <span class="r">if</span> pos &gt;= <span class="i">0</span>
1043
+ <span class="pc">self</span>[pos..<span class="i">-1</span>]
1044
+ <span class="r">else</span>
1045
+ <span class="pc">self</span>[<span class="i">0</span>...pos]
1046
+ <span class="r">end</span>
1047
+ <span class="r">end</span>
1048
+
1049
+ <span class="r">def</span> <span class="fu">lchomp</span>(arg=<span class="gv">$/</span>)
1050
+ <span class="pc">self</span>.reverse.chomp(arg).reverse
1051
+ <span class="r">end</span>
1052
+
1053
+ <span class="r">def</span> <span class="fu">lchomp!</span>(arg=<span class="gv">$/</span>)
1054
+ <span class="pc">self</span>.reverse.chomp!(arg).reverse
1055
+ <span class="r">end</span>
1056
+
1057
+ <span class="r">def</span> <span class="fu">ords</span>
1058
+ <span class="pc">self</span>.unpack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
1059
+ <span class="r">end</span>
1060
+ <span class="r">end</span>
1061
+
1062
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/binding'">binding</h3>
1063
+ <table class="cube_table"
1064
+ id="binding_cube"
1065
+ title="require 'zucker/1/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_specification">(<a href="javascript:show('binding_specification')">show</a>)</span>
1066
+ <pre class="source" id="binding_specification">require <span class="s"><span class="dl">'</span><span class="k">binding</span><span class="dl">'</span></span>
1067
+
1068
+ describe <span class="s"><span class="dl">'</span><span class="k">Binding#inspect</span><span class="dl">'</span></span> <span class="r">do</span>
1069
+ it <span class="s"><span class="dl">'</span><span class="k">should output the current environment</span><span class="dl">'</span></span>
1070
+ <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>
1071
+ <pre class="source" id="binding_source"><span class="r">class</span> <span class="cl">Binding</span>
1072
+ <span class="r">def</span> <span class="fu">inspect</span>
1073
+ put_vars = lambda { |array|
1074
+ <span class="r">if</span> array.empty?
1075
+ <span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
1076
+ <span class="r">else</span>
1077
+ array.map{|e| <span class="s"><span class="dl">&quot;</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">&quot;</span></span>}*<span class="s"><span class="dl">&quot;</span><span class="ch">\n</span><span class="dl">&quot;</span></span>
1078
+ <span class="r">end</span>
1079
+ }
1080
+
1081
+ <span class="s"><span class="dl">&quot;</span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.to_s<span class="idl">}</span></span><span class="k">
1082
+ local vars
1083
+ </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">
1084
+ (instance vars)
1085
+ </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">
1086
+ self
1087
+ - </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">self</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="k">
1088
+ block_given?
1089
+ - </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">block_given?</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">&quot;</span></span>
1090
+
1091
+ <span class="r">end</span>
1092
+ <span class="r">end</span>
1093
+
1094
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table> <h3 title="require 'zucker/1/egonil'">egonil</h3>
1095
+ <table class="cube_table"
1096
+ id="egonil_cube"
1097
+ title="require 'zucker/1/egonil'"> <tr><th>Summary</th> <td>Creates a block, where <code>nil</code> does not raise <code>NoMethodError</code>s</td></tr><tr><th>Methods/Usage</th> <td><h5>egonil, nn</h5><pre class="usage source" style="display:block">egonil <span class="r">do</span>
1098
+ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist
1099
+ <span class="r">end</span> <span class="c"># =&gt; nil</span>
1100
+
1101
+ nn(<span class="i">5</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
1102
+ <span class="c"># =&gt; 5</span>
1103
+ </pre></td></tr><tr><th>Further information</th> <td>See http://rbjl.net/26-the-28-bytes-of-ruby-joy for more information and discussion</td></tr><tr><th>Specification</th> <td> <span id="show_egonil_specification">(<a href="javascript:show('egonil_specification')">show</a>)</span>
1104
+ <pre class="source" id="egonil_specification">require <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span>
1105
+
1106
+ describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span> <span class="r">do</span>
1107
+ 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>
1108
+ proc <span class="r">do</span>
1109
+ egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
1110
+ <span class="r">end</span>.should_not raise_exception
1111
+ <span class="r">end</span>
1112
+
1113
+ it <span class="s"><span class="dl">'</span><span class="k">should return the nil_value if given</span><span class="dl">'</span></span> <span class="r">do</span>
1114
+ egonil(<span class="i">9</span>){ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }.should == <span class="i">9</span>
1115
+ <span class="r">end</span>
1116
+
1117
+ it <span class="s"><span class="dl">'</span><span class="k">should restore default behaviour after the block</span><span class="dl">'</span></span> <span class="r">do</span>
1118
+ proc <span class="r">do</span>
1119
+ egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
1120
+ <span class="r">end</span>.should_not raise_exception
1121
+
1122
+ proc <span class="r">do</span>
1123
+ <span class="pc">nil</span>.a_method
1124
+ <span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
1125
+ <span class="r">end</span>
1126
+
1127
+ it <span class="s"><span class="dl">'</span><span class="k">raise NoMethodError for non-nil objects</span><span class="dl">'</span></span> <span class="r">do</span>
1128
+ proc <span class="r">do</span>
1129
+ egonil{ <span class="i">5</span>.a_method }
1130
+ <span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
1131
+ <span class="r">end</span>
1132
+ <span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_egonil_source">(<a href="javascript:show('egonil_source')">show</a>)</span>
1133
+ <pre class="source" id="egonil_source"><span class="r">def</span> <span class="fu">egonil</span>(nil_value = <span class="pc">nil</span>)
1134
+ <span class="r">yield</span>
1135
+ <span class="r">rescue</span> <span class="co">NoMethodError</span> =&gt; e
1136
+ <span class="r">if</span> e.message =~ <span class="rx"><span class="dl">/</span><span class="k">NilClass$</span><span class="dl">/</span></span>
1137
+ nil_value
1138
+ <span class="r">else</span>
1139
+ raise <span class="co">NoMethodError</span>
1140
+ <span class="r">end</span>
1141
+ <span class="r">end</span>
1142
+
1143
+ <span class="r">alias</span> <span class="fu">nn</span> <span class="fu">egonil</span>
1144
+
1145
+ <span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr></table>
1146
+ </div>
1147
+ <br/>
1148
+ </div>
1149
+ <div id="foot">
1150
+ <div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
1151
+ This is the Ruby Zucker 1 documentation.
1152
+ The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
1153
+ Gem source at <a href="http://github.com/janlelis/zucker">github</a>.
1154
+ </div>
1155
+
1156
+ </body>
1157
+ </html>
1158
+