zucker 13.1 → 100.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +17 -2
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +22 -0
  5. data/Rakefile +17 -58
  6. data/doc/{zucker_doc.rb → create_documentation.rb} +67 -129
  7. data/doc/zucker.html +1774 -0
  8. data/lib/zucker.rb +12 -42
  9. data/lib/zucker/alias_for.rb +9 -16
  10. data/lib/zucker/all.rb +0 -2
  11. data/lib/zucker/array_op.rb +15 -0
  12. data/lib/zucker/blank.rb +39 -21
  13. data/lib/zucker/camel_snake.rb +15 -0
  14. data/lib/zucker/constantize.rb +29 -0
  15. data/lib/zucker/dir_extras.rb +20 -0
  16. data/lib/zucker/egonil.rb +13 -24
  17. data/lib/zucker/file_extras.rb +33 -0
  18. data/lib/zucker/hash_extras.rb +11 -0
  19. data/lib/zucker/hash_op.rb +28 -0
  20. data/lib/zucker/instance_variables_from.rb +34 -0
  21. data/lib/zucker/iterate.rb +24 -21
  22. data/lib/zucker/marshal_copy.rb +11 -0
  23. data/lib/zucker/mash.rb +22 -0
  24. data/lib/zucker/ords.rb +17 -0
  25. data/lib/zucker/regexp_union.rb +17 -0
  26. data/lib/zucker/square_brackets_for.rb +12 -15
  27. data/lib/zucker/string_extras.rb +15 -0
  28. data/lib/zucker/string_op.rb +20 -0
  29. data/lib/zucker/tap.rb +13 -8
  30. data/lib/zucker/unary_conversion.rb +25 -0
  31. data/spec/alias_for_spec.rb +11 -73
  32. data/spec/array_op_spec.rb +17 -0
  33. data/spec/blank_spec.rb +2 -0
  34. data/spec/camel_snake_spec.rb +15 -0
  35. data/spec/constantize_spec.rb +33 -0
  36. data/spec/dir_extras_spec.rb +32 -0
  37. data/spec/egonil_spec.rb +1 -0
  38. data/spec/file_extras_spec.rb +57 -0
  39. data/spec/hash_extras_spec.rb +11 -0
  40. data/spec/hash_op_spec.rb +28 -0
  41. data/spec/instance_variables_from_spec.rb +24 -0
  42. data/spec/iterate_spec.rb +2 -0
  43. data/spec/{mcopy_spec.rb → marshal_copy_spec.rb} +5 -3
  44. data/spec/mash_spec.rb +17 -0
  45. data/spec/ords_spec.rb +15 -0
  46. data/spec/{union_spec.rb → regexp_union_spec.rb} +4 -2
  47. data/spec/square_brackets_for_spec.rb +4 -12
  48. data/spec/string_extras_spec.rb +20 -0
  49. data/spec/string_op_spec.rb +22 -0
  50. data/spec/tap_spec.rb +2 -0
  51. data/spec/{unary_spec.rb → unary_conversion_spec.rb} +4 -2
  52. data/zucker.gemspec +8 -18
  53. metadata +75 -147
  54. data/.gemtest +0 -0
  55. data/README +0 -7
  56. data/desc/alias_for.yaml +0 -17
  57. data/desc/aliases.yaml +0 -18
  58. data/desc/array.yaml +0 -11
  59. data/desc/array_to_proc.yaml +0 -16
  60. data/desc/binding.yaml +0 -22
  61. data/desc/blank.yaml +0 -11
  62. data/desc/case.yaml +0 -9
  63. data/desc/cc.yaml +0 -10
  64. data/desc/class_to_proc.yaml +0 -11
  65. data/desc/dd.yaml +0 -23
  66. data/desc/egonil.yaml +0 -15
  67. data/desc/engine.yaml +0 -15
  68. data/desc/enumerable.yaml +0 -10
  69. data/desc/file.yaml +0 -14
  70. data/desc/hash.yaml +0 -14
  71. data/desc/hash_to_proc.yaml +0 -10
  72. data/desc/info.yaml +0 -20
  73. data/desc/iterate.yaml +0 -16
  74. data/desc/ivars.yaml +0 -19
  75. data/desc/kernel.yaml +0 -15
  76. data/desc/mcopy.yaml +0 -11
  77. data/desc/mm.yaml +0 -15
  78. data/desc/not.yaml +0 -11
  79. data/desc/oo.yaml +0 -10
  80. data/desc/os.yaml +0 -19
  81. data/desc/qq.yaml +0 -11
  82. data/desc/regexp_to_proc.yaml +0 -12
  83. data/desc/regexp_visualize.yaml +0 -23
  84. data/desc/ruby_version.yaml +0 -28
  85. data/desc/sandbox.yaml +0 -13
  86. data/desc/square_brackets_for.yaml +0 -22
  87. data/desc/string.yaml +0 -22
  88. data/desc/tap.yaml +0 -17
  89. data/desc/unary.yaml +0 -15
  90. data/desc/union.yaml +0 -10
  91. data/doc/zucker_doc.html +0 -3042
  92. data/lib/zucker/aliases.rb +0 -52
  93. data/lib/zucker/array.rb +0 -15
  94. data/lib/zucker/array_to_proc.rb +0 -19
  95. data/lib/zucker/binding.rb +0 -35
  96. data/lib/zucker/case.rb +0 -14
  97. data/lib/zucker/cc.rb +0 -31
  98. data/lib/zucker/class_to_proc.rb +0 -13
  99. data/lib/zucker/control.rb +0 -4
  100. data/lib/zucker/dd.rb +0 -24
  101. data/lib/zucker/debug.rb +0 -4
  102. data/lib/zucker/default.rb +0 -4
  103. data/lib/zucker/engine.rb +0 -70
  104. data/lib/zucker/enumerable.rb +0 -15
  105. data/lib/zucker/env.rb +0 -4
  106. data/lib/zucker/extensions.rb +0 -4
  107. data/lib/zucker/file.rb +0 -28
  108. data/lib/zucker/hash.rb +0 -28
  109. data/lib/zucker/hash_to_proc.rb +0 -17
  110. data/lib/zucker/info.rb +0 -193
  111. data/lib/zucker/ivars.rb +0 -29
  112. data/lib/zucker/kernel.rb +0 -42
  113. data/lib/zucker/mcopy.rb +0 -11
  114. data/lib/zucker/mm.rb +0 -41
  115. data/lib/zucker/not.rb +0 -20
  116. data/lib/zucker/object.rb +0 -4
  117. data/lib/zucker/oo.rb +0 -18
  118. data/lib/zucker/os.rb +0 -55
  119. data/lib/zucker/qq.rb +0 -13
  120. data/lib/zucker/regexp_to_proc.rb +0 -13
  121. data/lib/zucker/regexp_visualize.rb +0 -31
  122. data/lib/zucker/ruby_version.rb +0 -120
  123. data/lib/zucker/sandbox.rb +0 -17
  124. data/lib/zucker/shortcuts.rb +0 -4
  125. data/lib/zucker/string.rb +0 -55
  126. data/lib/zucker/to_proc.rb +0 -4
  127. data/lib/zucker/unary.rb +0 -25
  128. data/lib/zucker/union.rb +0 -17
  129. data/spec/aliases_spec.rb +0 -13
  130. data/spec/array_spec.rb +0 -16
  131. data/spec/array_to_proc_spec.rb +0 -12
  132. data/spec/binding_spec.rb +0 -6
  133. data/spec/case_spec.rb +0 -15
  134. data/spec/cc_spec.rb +0 -6
  135. data/spec/class_to_proc_spec.rb +0 -9
  136. data/spec/dd_spec.rb +0 -37
  137. data/spec/engine_spec.rb +0 -11
  138. data/spec/enumerable_spec.rb +0 -8
  139. data/spec/file_spec.rb +0 -46
  140. data/spec/hash_spec.rb +0 -25
  141. data/spec/hash_to_proc_spec.rb +0 -12
  142. data/spec/info_spec.rb +0 -8
  143. data/spec/ivars_spec.rb +0 -20
  144. data/spec/kernel_spec.rb +0 -42
  145. data/spec/mm_spec.rb +0 -6
  146. data/spec/not_spec.rb +0 -10
  147. data/spec/oo_spec.rb +0 -6
  148. data/spec/os_spec.rb +0 -37
  149. data/spec/qq_spec.rb +0 -19
  150. data/spec/regexp_to_proc_spec.rb +0 -9
  151. data/spec/regexp_visualize_spec.rb +0 -38
  152. data/spec/ruby_version_spec.rb +0 -55
  153. data/spec/sandbox_spec.rb +0 -14
  154. data/spec/string_spec.rb +0 -65
@@ -1,23 +0,0 @@
1
- regexp_visualize:
2
- summary: Test your regex against a string, handy for the command line.
3
- methods:
4
- "Regexp#visualize, Regexp#vis": |
5
- # regex = /\b([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})\b/i
6
- regex.vis 'I do not contain an email address.'
7
- # no match
8
- regex.vis 'I contain an email address: mail@example.com'
9
- # I contain an email address: >mail@example.com<
10
- regex.vis 'mail@example.com', 1
11
- # 1: >mail< @example.com
12
- regex.vis 'mail@example.com', 2
13
- # 2: mail@ >example.com<
14
- regex.vis 'mail@example.com', 3
15
- # 3: no match
16
- regex.vis 'mail@example.com', [0,1,2]
17
- # 0: >mail@example.com<
18
- # 1: >mail< @example.com
19
- # 2: mail@ >example.com<
20
- versions:
21
- - 1.9
22
- - 1.8
23
-
@@ -1,28 +0,0 @@
1
- version:
2
- summary: Adds a ⇧RubyVersion⇧ class for easy compatibility checks.
3
- methods:
4
- RubyVersion: |
5
- # return RUBY_VERSION
6
- RubyVersion
7
- # check for the main version with a Float
8
- RubyVersion.is? 1.8
9
- # use strings for exacter checking
10
- RubyVersion.is.above '1.8.7'
11
- RubyVersion.is.at_least '1.8.7' # or exactly, below, at_most
12
- # you can use the common comparison operators
13
- RubyVersion >= '1.8.7'
14
- RubyVersion.between? '1.8.6', '1.8.7'
15
- # relase date checks
16
- RubyVersion.is.older_than Date.today
17
- RubyVersion.is.newer_than '2009-08-19'
18
- # accessors
19
- RubyVersion.major # e.g. => 1
20
- RubyVersion.minor # e.g. => 8
21
- RubyVersion.tiny # e.g. => 7
22
- RubyVersion.patchlevel # e.g. => 249
23
- RubyVersion.description # e.g. => "ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]"
24
- info:
25
- - Thanks to Hanmac for a nice improvement.
26
- versions:
27
- - 1.9
28
- - 1.8
data/desc/sandbox.yaml DELETED
@@ -1,13 +0,0 @@
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
- versions:
11
- - 1.9
12
- - 1.8
13
- - "Does not work on JRuby, since $SAFE is not supported there"
@@ -1,22 +0,0 @@
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
- versions:
20
- - 1.9
21
- - 1.8
22
-
data/desc/string.yaml DELETED
@@ -1,22 +0,0 @@
1
- string:
2
- summary: "Generic ⇧String⇧ extensions."
3
- why: "Strings cannot be comfortable enough ;)."
4
- methods:
5
- String#-: "'1234abc5678' - 'b' - /\\d/ # => ac"
6
- String#^: "'Yes vs No'^2 # => 's vs No'"
7
- String#lchomp: "' Yes'.lchomp # => ' Yes'"
8
- String#lchomp!: "# mutable lchomp version"
9
- String#ords: "'Hallo'.ords # => [72, 97, 108, 108, 111]"
10
- String#constantize: |
11
- 'Object'.constantize # => Object
12
- 'Spec::VERSION'.constantize # => Spec::VERSION if rspec is loaded
13
- # you can also pass a parameter or block to define what happens, when constant does not exist
14
- 'IdontExist'.constantize(Array) # => Array
15
- 'String5'.constantize do |string|
16
- string.chop.constantize
17
- end # => String
18
- info:
19
- - "⇧constantize⇧ is an improved version of ActiveSupport's one."
20
- versions:
21
- - 1.9
22
- - 1.8
data/desc/tap.yaml DELETED
@@ -1,17 +0,0 @@
1
- tap:
2
- summary: This cube adds the two ⇧tap⇧ variants ⇧tap_on⇧ (known as ⇧returning⇧ in ActiveSupport) and ⇧make_new⇧.
3
- methods:
4
- tap_on: |
5
- tap_on [1,2] do |obj|
6
- obj[4] = 5
7
- end #=> [1, 2, nil, nil, 5]
8
- make_new: |
9
- make_new Hash do |obj|
10
- obj[1] = 2
11
- end #=> {1 => 2}
12
- info:
13
- - "Read more about using tap on the →ruby best practices→http://blog.rubybestpractices.com/posts/gregory/011-tap-that-hash.html→ blog."
14
- versions:
15
- - 1.9
16
- - 1.8
17
-
data/desc/unary.yaml DELETED
@@ -1,15 +0,0 @@
1
- unary:
2
- summary: Easy conversion between strings and symbols.
3
- why: "Sometimes, you do not care if you get a ⇧String⇧ or ⇧Symbol⇧ as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators ⇧String#-@⇧ and ⇧Symbol#+@⇧."
4
- methods:
5
- String#+@: "+'was_string' # => 'was_string'"
6
- String#-@: "-'was_string' # => :was_string"
7
- Symbol#+@: "+:was_symbol # => 'was_symbol'"
8
- Symbol#-@: "-:was_symbol # => :was_symbol"
9
- package: default
10
- info:
11
- - "Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)"
12
- versions:
13
- - 1.9
14
- - 1.8 (⇧+:literal⇧ not possible)
15
-
data/desc/union.yaml DELETED
@@ -1,10 +0,0 @@
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
- versions:
9
- - 1.9
10
- - 1.8
data/doc/zucker_doc.html DELETED
@@ -1,3042 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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 13.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:left;
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
- ul{
95
- margin:5px 0px !important;
96
- padding:none;
97
- }
98
-
99
- th{
100
- text-align:left;
101
- vertical-align:top;
102
- padding-right:3em;
103
- }
104
-
105
- td{
106
- width:100%;
107
- }
108
-
109
- li{
110
- list-style:none;
111
- }
112
-
113
-
114
- #foot{
115
- text-align:left;
116
- padding:0.3em;
117
- font-size:70%
118
- }
119
- #foot, #foot a{
120
- color:#444;
121
- }
122
- #smile{
123
- font-size:150%;
124
- float:right;
125
- }
126
- #smile a{
127
- text-decoration:none;
128
- }
129
-
130
- .small{
131
- font-size:70%;
132
- }
133
-
134
- code, pre{
135
- font-face:mono;
136
- margin:0px;
137
- padding:0px;
138
- }
139
-
140
- .source{
141
- display:none;
142
- width:98%;
143
- padding: 3px 5px;
144
- overflow: auto;
145
- font-size: 12px;
146
- background-color: #f8f8f8;
147
- border:1px solid #005;
148
- border: 1px solid silver;
149
- font-family: 'Courier New', 'Terminal', monospace;
150
- color: #000;
151
- }
152
-
153
- # http://coderay.rubychan.de/
154
- .source pre { margin: 0px; }
155
-
156
- span.source { white-space: pre; border: 0px; padding: 2px; }
157
-
158
- table.source { border-collapse: collapse; width: 100%; padding: 2px; }
159
- table.source td { padding: 2px 4px; vertical-align: top; }
160
-
161
- .source .line_numbers, .source .no {
162
- background-color: #def;
163
- color: gray;
164
- text-align: right;
165
- }
166
- .source .line_numbers a:target, .source .no a:target { color: blue; }
167
- .source .line_numbers .highlighted, .source .no .highlighted { color: red; }
168
- .source .no { padding: 0px 4px; }
169
- .source .code { width: 100%; }
170
- .source .code pre { overflow: auto; }
171
-
172
- .source .debug { color:white ! important; background:blue ! important; }
173
-
174
- .source .an { color:#007 }
175
- .source .at { color:#f08 }
176
- .source .av { color:#700 }
177
- .source .bi { color:#509; font-weight:bold }
178
- .source .c { color:#888; }
179
- .source .c .dl { color:#444; }
180
- .source .c .ch { color:#444; }
181
-
182
- .source .ch { color:#04D }
183
- .source .ch .k { color:#04D }
184
- .source .ch .dl { color:#039 }
185
-
186
- .source .cl { color:#B06; font-weight:bold }
187
- .source .cm { color:#A08; font-weight:bold }
188
- .source .co { color:#036; font-weight:bold }
189
- .source .cr { color:#0A0 }
190
- .source .cv { color:#369 }
191
- .source .de { color:#B0B; }
192
- .source .df { color:#099; font-weight:bold }
193
- .source .di { color:#088; font-weight:bold }
194
- .source .dl { color:black }
195
- .source .do { color:#970 }
196
- .source .dt { color:#34b }
197
- .source .ds { color:#D42; font-weight:bold }
198
- .source .e { color:#666; font-weight:bold }
199
- .source .en { color:#800; font-weight:bold }
200
- .source .er { color:#F00; background-color:#FAA }
201
- .source .ex { color:#C00; font-weight:bold }
202
- .source .fl { color:#60E; font-weight:bold }
203
- .source .fu { color:#06B; font-weight:bold }
204
- .source .gv { color:#d70; font-weight:bold }
205
- .source .hx { color:#058; font-weight:bold }
206
- .source .i { color:#00D; font-weight:bold }
207
- .source .ic { color:#B44; font-weight:bold }
208
-
209
- .source .il { background-color: hsla(0,0%,0%,0.1); color: black }
210
- .source .il .idl { font-weight: bold; color: #666 }
211
- .source .idl { font-weight: bold; background-color: hsla(0,0%,0%,0.1); color: #666; }
212
-
213
- .source .im { color:#f00; }
214
- .source .in { color:#B2B; font-weight:bold }
215
- .source .iv { color:#33B }
216
- .source .la { color:#970; font-weight:bold }
217
- .source .lv { color:#963 }
218
- .source .ns { color:#707; font-weight:bold }
219
- .source .oc { color:#40E; font-weight:bold }
220
- .source .op { }
221
- .source .pc { color:#058; font-weight:bold }
222
- .source .pd { color:#369; font-weight:bold }
223
- .source .pp { color:#579; }
224
- .source .ps { color:#00C; font-weight:bold }
225
- .source .pt { color:#074; font-weight:bold }
226
- .source .r, .kw { color:#080; font-weight:bold }
227
-
228
- .source .ke { color: #808; }
229
- .source .ke .dl { color: #606; }
230
- .source .ke .ch { color: #80f; }
231
- .source .vl { color: #088; }
232
-
233
- .source .rx { background-color:hsla(300,100%,50%,0.1); }
234
- .source .rx .k { color:#808 }
235
- .source .rx .dl { color:#404 }
236
- .source .rx .mod { color:#C2C }
237
- .source .rx .fu { color:#404; font-weight: bold }
238
-
239
- .source .s { background-color:hsla(0,100%,50%,0.1); }
240
- .source .s .k { color: #D20; }
241
- .source .s .ch { color: #b0b; }
242
- .source .s .dl { color: #710; }
243
-
244
- .source .sh { background-color:hsla(120,100%,50%,0.1); }
245
- .source .sh .k { color:#2B2 }
246
- .source .sh .dl { color:#161 }
247
-
248
- .source .sy { color:#A60 }
249
- .source .sy .k { color:#A60 }
250
- .source .sy .dl { color:#630 }
251
-
252
- .source .ta { color:#070 }
253
- .source .ts { color:#D70; font-weight:bold }
254
- .source .ty { color:#339; font-weight:bold }
255
- .source .v { color:#036 }
256
- .source .xt { color:#444 }
257
-
258
- .source .ins { background: hsla(120,100%,50%,0.2) }
259
- .source .del { background: hsla(0,100%,50%,0.2) }
260
- .source .chg { color: #aaf; background: #007; }
261
- .source .head { color: #f8f; background: #505 }
262
- .source .head .filename { color: white; }
263
-
264
- .source .ins .eye { background-color: hsla(120,100%,50%,0.2) }
265
- .source .del .eye { background-color: hsla(0,100%,50%,0.2) }
266
-
267
- .source .ins .ins { color: #080; background:transparent; font-weight:bold }
268
- .source .del .del { color: #800; background:transparent; font-weight:bold }
269
- .source .chg .chg { color: #66f; }
270
- .source .head .head { color: #f4f; }
271
-
272
- .rubylogo{
273
- margin-left:20px;
274
- margin-top:10px;
275
- margin-right:15px;
276
- float:left;
277
- }
278
-
279
- </style>
280
- </head>
281
- <body>
282
- <div id="world">
283
- <img class="rubylogo" alt="ruby" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAADAVgAAwFYBgeEOVwAAAAd0SU1FB9oIHxY1C2ozXR0AABglSURBVGjevZt/sGVVdec/a+9z7q/3o1830N00DQiCQkoQiPgj0UiG8UcyMZNSC82PkdIEqYlaISkTMTPlVEw5IdHRqKEURQ2JSZwaiYijJimnxohMVH42Aml+2dD9ul//fP1+9Lv3nnP23mv+2Puce25DTOI4dtepe+5+7913vnut9V1rfdd+wo/wn/cea+3UWlBFIFfVDOdCsbi/Gj7+CMWePRQH9lEsLlItLjLav0h1fAX1ivqKUDq08njnkNKzYZXDsyb86qoH4KNzXd6xXjztGbIfBdAQAsYYjDFT66q6E7gQeJWIBDXmmKpfN1k2kl4P040X3S6m08FYIyH4TBEQASMYhGGOGWY4CSx+fC5fqZD971gv9gH8wVyHd6+XPxrAJwMVEVR1AFwBnFsdWrrKrSy/TIcbSJ6Tb9tBftpWqiNHML0uptfH9vqYfh/b7+OzHPUBSZ9Ziae0gaGBwggKKBBU7vvAfOfDTuW+69eLBwDes9DjvStj5P8n0JOs+VzgZzcevP9la//wjZ8qnnjslHLP41QH9irjITbLpXP6TmZf+BJmf+Z1VBtjhg/uojx4kGJxkWJxL+X+RdzKKq5yjIoNxtWYEaqViJQqWolIpUZLRBxQij7shPf8l9XyVoDfme/8cAGrKiKTj6zKcpDl+U8A7167+1vP3vvhG7aNdj/UC8cOEdbWMYoai1gBAyoByXoZg4svZ+t7P0i1NuTEvXdTHDigxYH94pYO6OqeJ2Rt9RhOHUHRUkQqIsgK0VKNVBDBo1RC4eGX3rda/PUP1aVDCA1Y59yCMea/icjri4MHOrve/PrOxv13GQkBVLGgYkUUEQXUgBURmyk+KBv3/gMrf34TZ/zxZwiuUn9iXdzanD716INSrq8hBhTUC+JV8CISAA8SgBDfa0AkQLcSvfWdm3s//oHj43vtD5t9VfVqEe4MZfXj373urZ1d1/yHfLy4KEpQVRUUFCRo3BxF0BjbKIKJW4Dbv5fejz2fhStfLTo/5//+HW8T9QVWaGLVIQQRcSo4BI+oA7wgHsSJqFfEi+DQs1/YtbeaHxZYVRVV/RVfjG9e+tIX7P+69Fzd88nPSLCoN+AD4lTwCj5AUHABnCpVCDjVeHnBI5SHlykP7ENBN192uf25R77nxmc/xy1JT4/anq7Yvo5sV0vJ1YtRFaM1eyuogqog3oBHg4NXOyPnmf/XmG3l1euG+5665bvX/0b2nauv0o29B4QBGgK1J0NQNEhzT4AQhBAieB8UBwTimpYlAgLopnPOyd9w9y551s/8rPdB1QFOjHibKVmueZbpILM6a63OGqMDER0oOqOqfUV6Cr3A67Mf1KrGmCZmVfWGpb/50rt2f+D3OXrHXWr7iMlQFFFAohvX2wQqkCJYAyAxjoPEQsSn79f684tC/GioVJX9yXf9rs7Pz1cP3/bXmTqHqEqyakx9oBako6IW1U5QuqqUqFTwmh8I8Elgb3r4j37v1x7/2Ecoji2rGTTfJhLARGwRtMQLUWI4py8QAzMoBFGCghiQwQzVwQOUe76HXzkufvW4hrU1Offcs/PuS3/S3/2/v26NCKKqJMLyquJVqUB8ChWvigdV1efJD5p6VNUCn777umve9NRf3YIvKzWx/sEomPQqGkHXxk0FUiQfjSQlAoggAtZAhtKdm+P8W24jHDzEePfDhPUV9RsbEkZDfFloKEtOHF8p77lnlwkhZIholcA6jWxf84JX1KE4Vcl+QLCDcnXlv+56zzvf9MQtn1KJBhdCDDhJ4Bofq0HLxL0DIKIEFZoaScBg8F7pXXQZlBVrX/8a7thhfFWJL0t8VaovSglVQSjLziXP2u4feHKpHPnQcdHC6kIQp9CAVxWn4FQ1+9fmWVXdsrG49927b/zAbzxy06c0GyBojEqR6FdCtHAKVZDorpLWjLb3I7o3RjEIPihiYOYFL6HYt5e1Rx4iVI5QlfhyjK+chKokOKcagqhqdt7CQJ5aHxVHC9eJzI96EAfqUakUjaD1X1Z41KWiqu4sVpbf//BHbnjjwx/+mOYzzVNLHadNjMgkbmmvt9Y0enL8YRW8KBYwcwvMXfpC1u+7h40Di6gqwXvUh5SvFVUk5h5VUHNGP+8I+H2jSpyq9aBOg7jozuJV1Sn/vEu3wJ4H3Hz3u97+8sf+7K80n514oqHlytLgivGa3Ldel5Qka1efbJBiVFBRBhdcROfMszn2wRsoh0MQE39VBNi6QFUlwlY5zUpmu5l7dFj6SoP1oF6D+IC6+Lv89wXsE9jg/eXATd+89hcvfeJzn1M7mKQb04pXOYkCJdSxmjag/taanGsQyRuCARHLGde+nY1HH2H1u/djBjNo8ISg6WdroIEQd0GjteP6nKi9sGfYNQplGbQTonujYLrKIfP9LGuNwXt/mRhzy72/9zuX7vni59VkiMaYbcDWQMPEkKjGvFoDMy0eS1mIVP82rwSlf+55bPv3V7G++yFcUCrncS7gfbzie49L750P4n1ac47KeckC5uLc5KcYirEmmgAGylL2/XJtSj2v23Pbf7/wHz/9cfXOSWTSFLPEfFl7rOgEtCSXDTYlyHrtJGvXrioCroJL/vTzjJf2c/jv/hYnBql8HbPEcI33QUPkAdXa0uleJd6qOV/oGqFaVDIVGCgH/7kYvmzlsd3X7frQHzBeWReToSZhNNpKPzKxWuPerTitmbmNNMjE4gqEEZzzq29i7sLncfSOr3PwjrvpntYH7xPASezWKVI1VWqEVnxTE5oC8mxD3lPYozD7TBZukZSUa6vvvO9DNwz2/59d2pmLrY2RCdOqtpP0SexMy6otZq7Zub4Q8CNYuOi5XPyhTxCqiqX/+UUkQ13lhRZBtUGH9D6kstwn71JSiZoewyvSBc4IkPEMFq6ViuCqK7/3lS9d9dAtt2AHiG9ZVGQStzJdHrcWJ2xM25ptwALqIT9lhov+8CNI3sGPRvrEV77MqkFsUU2nNCJQTeC8xth3rcun9ViEEJUPYEcMq2kLt6UZNxrffMfv/hYuuYYJk/ijRT4hMXUNTmryqvPspFSmbvZVpzfgOb95Pae+/MrIDCLYhS1hVGIlT3lZwLYJkYk123tcFzsBsHUWEOgqZPH9snkm66rqm7523a+fvfzUEcXE9g4lxkurn23yqUzIp36AINOlZc3agZh+1IB3cMbP/zznv+23MNYiqiJZpjv+zSuCAfKUx226MomXIdbiJm2wkYnXTRU/aWPmAuTxUVayky2rqrPrSwc+sevPPqt5L7pybSlb76BACKmjaYEWJhavc1FoxW9opS8jsPmy53PZRz6F7fVxRw6jzikaZNull6kHutEjtE4G7eIm1KEVJp5Vf60N2Cp0tPGM1axt2fTvnV/4pdebQARrWkysEmMkq6untG5a5ESLjKY4zaSfIcZt78yzuPxPPkN3bp7R/ffihydAVUQDHTfO5hf6ZbUx6gASJMZhpxUeUxniaV3O5KXTAgysZidZ98eOPvzQW/ffdXdOlGWaEjEwuXwrblIVQ617aGu3tbWWJCx8AbNn7eTFN3+WhXOfw+pXb8evrCT3SF3rxgZbL7jQ7PvOvZg8CXYgpcRYtM3UYpIt6tjVk+r3fnLnFI0r2UnWvebBj9+4rWsNwyrGrCSr2lT6hbZrpXxcDwLETBcUQaNb1xYPY5g/90x+4k//Bwtbd3Dk5htxy0cRY8F70JRPg5dBt2tCSM+u0coBGDOJU1sDkRaJyYRLTKyuaNntRNbqdV9Zrh6/au2+e8xMp6PBBxmVFT7En/QCNoA3/4QXSdwUaTUPJEbGgB/DwnPO4aWfvY3u8RUOffT9uLU1xJimSkoxISKilhBUEK/Y0AKhOilHaf3ONmvX93OhaV7qCPCZqhJC6ANXrD344I58PNLZTkdS5ca4rGq9DZdIwpsJKNvugloNRNMwGAgONj33PF72l19E9jzB0uc+C95FfNGkkyotilzS7ebS39RnOByRVqV+jtBy30omKakuQupY64cJbwBrBjRLKsZLgOuLR3bTrUqZ7fc1VioxIIqywqFNCiDlZCsthraTelpqrSc9zMLzLuTlt3wBfeIJDnzyY2hZTlm16YIaeRMyVRnMzOiJjZFqC+xUzMoEZM0dSCw8TvUTnklc8iQQskS6ZwHijy/TFyF0u1EOT0wjQFFVuKj8TlQLmWhWjXalEy3LWthxxb/l5Z++lbWv/R37/vj9qHcNpWtt3aYpmPSMBhH1XlQadVcadbdNovV7nViz7yepsVW+7wkJMCIS54mZpTM/j5zYiD1Vq7oQie5dBcWbaN2awBqxLoGuhjC3YwvP/eWredHvf5DFm25k74f+EMnyBDYkdw6NhdvWJWln4itNsSt1bLbB1iQVdEKSmsEmN+GZloX31AJAUKgAsvlNdE7fgTl6FD2uDVGIkeYDigQ6mOg6NjF0lnJHOYadL76EF/7n93L2q1/DI+/6TZb+/DNIlqEiLRFg0vXQek+SswVkxkgwRowLKm3L1uTkdVLl1SQ26FiqEOiUOl0Gw/dqlw4CGwCd7afT27GTIssiiuVjiAjGSJN2jAjDssS36kgboEqMeOk7/iMvevd7GGzdzt2veQUr3/4Wkllwbqqpmn6apz2coGjXGGONqAatgTWW9q0cXHdL3a5grcFnCqU2la0REUH2CoRMNFAdOlyYXo/uGTvpnnXWFNWa5WNJSxasCEYMIrBRJNCAL2Hz2dt57a23s+3SywH41it/itX7741pJ4SmLZy0Fy3c+vQyKYa3SGYMQYIGxdQWngKbXDTPhV7Hoqq4LHK2AawxYqOOPDbGkK1+5XYoK6NlQff5l9E759moczGGJAkzy8sYEUxySUl7PSwDttvhlTd9VC+5+i0CaHHsCPdfc7Usf+fbKnku+FBDmMKkJ9eC+jTDiwp0DBoUiZ1xtHAbrE95vtuxjaFUAhaDtYIVk4wkDiA7cefXoXRGh0O0qph/6RVoUTQHTqKqEEGrd9Drkc/Osnnrds76hddyybW/Tj4YoKDl8rJ++y1v5vCXv0p3U0/E+zaOiVL7DH6tz3DjgYFI7Q/SjtsmHQH9riGzSfZJG5JnFiMGKwYbOah8w3hINrrnHgihg/OUS0vMverfsfmnr2T46G6Gjz3GeH4T+ZYthNGIzQsLmB076V9wIduvfAW20421b1mKBq/Ld90n993+ZUxPmBlVdEwUlXKTuEqnXFj06cF8kvWVTESMoGWseVRlkpNj3Bp6uYn1fZwz4QVyY+P5EhGsMQjJwuWJdRGRzQDu8d06fOgBmfvpV7Bw2lZmL74Et7JCKEtMntHZcipmdgaKAr8xpDq+jJaVaFmqjsey/I1vMkfO0AfW8IoiXQMdIxigmzYAVWnUHp3S6J+GWAWZERjHTCa1ZZ1Cr2MYdC2hFrhrOVWUkAldbBtwzESVK40g20DwxsrhW25m5sUvTZSoZIMB5DlaFlRL+6EoCFWFliValWhZoWUp6ir23v5VOmZAYEyBFyHy1SgVLEOv2KheaN9ANwFoqR9tQVMA8aA9YhzXfbBX6OSGftdGRVOY2iFEGWXKpmDBxDhOPRCZK5wA2xDFWMPjf/O3nPqtO+mdchpaFmhZEqoyAawiyKqCor1e4k5scPDBXeTdPhk5XktUdMpNNZ3DqCAMfaTrGUF6guZ1ZSrTVhdB+oo60jhZEZsJ/a5F0Elf3gCPv2loAh0sagQjpkgtNVlZFUYgWlgDh0vHN9/4Zq64+aO44UYDmCJZtHJxI6oKqpLgKgiBxbsextJFiSOTTCxxnp8k2TQLCooaMCRlcU1hOYAVdLMQcsVYmRhNY3FjuqBjYpYbdC3WaEs7q/co8rggFOLIbLSwEbNfREbRpatSBNlqgMPDcWwWVtc4dscdDDYvEIpxBOocOId6DyGk13hvjOHQ7icxmUXxBFEEi8E18pBPFZHRRpePdbegNhKRHEq9a6aEzQLddMZFQE8xyH6g37VkRlLcSgJLI3yLCl4CFsEYGxla5IDAKLl0aRC2VUFZLUpMsJBZdn/+di557atwwxH4gGqIr8EnKcSjzqMhYARWjxxHLOlBQnIswSRCMTqtMrbkXGlJRQqIU+xS0gK6gswo0hfodyx5ZmI5Kqkcm9Zx02anD7ZCbjIE2S+iEXAIXhC2bpSBwgdmyAgG1g4fYeXxPQzmBtGNQ4hTgPRKCM2Jlf37l1NNXJeAmvJonKNqKx2Fk0CfpH9JSwKTIMiJAIWgz5Ku7MBwTB1BYkyohulatLWPkj4kNxYR9lNbWAQpvG49XoVYLzf6sbD44KNccNH5lKMRWls1ga0BWwOH9y1H67ZmCvX/MokE4aTJwMnaci3jtrsjrzBnDDttR3qSYVToeMNBW+KTcKOmrVdPmvUgsKGOU+wMii5esu/xAiAbOjVlUOvi5F0lxMY/oKwsH2d1cT/dPEM1TM1niYMrghHGpUtUrKiEBixpCDbV1UgLHE8X3po8C8yL5YysSy4Wm/LpZsmZocMTuo4zKXMnL6oJSyWS2JoW2jWZOMKJ5jTtoXEklngYNR4FTPM6Cldx6OgKZ5y6CZeGWu1JngBH10qCD1OurK0OqJZ2aZeEJ1nYt2rj2v03GcuZWZ9cDLaumMQgRpgTy8VmCw/4ZZyEiRLRmrCrBta0kF6WUah3DWBtKltJc9SUPSWehFkbjtg87jejmBptSOdIjx0bqU5mDy3Q092AZ9qy7UlE3eLVUs0Wm3NG1sMa23RoVhLrCqk+NrzIbucBd5TVUESikommJcSWNQjkJqsmgJUgInuBswSRSRzGZLg6HHNs9QRz/Q7BN027qKqGoDgfRNORg+hKbdgTPbop/NsTvtZ7p9ARYXPWYWfWT5qZxZgE2JqUagQrMd0YDC/u7eD+8jBL1Qls62CJJlevCDpruw1gI0IFfLWO+5BOwIUUxy54NoqCsqioyoqyKCmLkqqsGA4rfEgRKFMwm55XmHia1tM9pjWpIsC8zTi7M2BnPkCMkFlLZg2ZseTWkosls5bcZOTGkomNr9bygsEOzu9uaboTEY0jLQ0UEqosy6vWiXiJgIVrCdFG9clWgGCEo+OCrggZBtWgQeOhwOHIxN5etMXLYcrC7elAfUKgid0kiu3Mu5yW9eiY2Lu2u5y6n81EmtP1jZun+0wsl82ezty4y73DpXreJV6UoYSy1+tPAL91ZcQnF/pL7alfw5oIQZRSPevjihlJrX90Z62qTl0IabuEnxJsWqMIMZNY9Qo9YzinM8OMzZre1aRi35jYx1oma9bYRnmx9ea0Nub5s6czk3X5xuqTGMBrYIOq6vf75dTfPATRx0W5DcwveEkWCzEX1xZaCRWZl0TOKiGY9LV4ZoqWlTnJudvNvZUoN52e9zizM4hivknWSgAbwImZjU1rSPO9JlnctlhcRbhgYTunzm7iiwcewIXAmi/W8tn5tSaGP7G5z7XHx8si3ORh5OJBTLygTkM87ImwatCRq6QqKi2LUp0LGuq+vonf5M46LefU3VIQyLKMhZkeric8ZUYsmYKjUrJCyQn1lKJURnGiOIFKFIfiUYLRqHyaCLxjLF2b07U5eadL3u9CJ9Ptc1v4xfNfQifLOV6O1piZW52uZoG/ODXPV6v8fU75ba2rVGkOzUgQ6JdeTx15PCDZrGK7ErQSL46gHi+OgMPjpyorL0KwBnILJvKAikzYfbJVqGpsKDCSiSFDyEwkr44YcolAOyajI5aOzehkGXmWkWU53Syn2+nR73TZ8CW7jjx555b+/Btf+eAji41Lf2Khxy8fHVcf35S9TxDv0OtrsAGadLPWMZwy8ipiRaRDSB7QWDlNEppzW5nBpRaN1pRSRJrTc4a6AZaUS9NWCzj1VJL0+jCp3NLw7JCiB71yyAoHBzY/ONvpL83nvY3Ngzl/6sy8O212i7/49HOXrvnmnUeeZuEb5/u8bW3EJxf6vTXPC7yRLwV0QacUfmGu8n77EKvdzSGok4CXoA5PRRBPYQNlZmL8Szrkrc80cdSYN+EYsFeERZB9Jt7vG6s/DuriX+Roc25FwVkRZ0QKIzLOxIy7WTae6wzGb3jyUPlPnb96/2kZv33ETT/Hn8z3efvaCIA/2tSfVfQFCq9T5OeAs1SQTNGz1jOv+SAvpaKUSivjjnqjh73RIwqHgSMicgg4AqwIsiawboQ1i1nvYlaPhaLITdQB4iVqjYQuJsxl3XCwOqEztsfFM9u4/MlH/3V/YfOW/4T59Pue8Wv/F9jGEQFesRfqAAAAAElFTkSuQmCC" />
284
- <h1>Ruby Zucker 13.1</h1>
285
- <h2 style="clear:both">What is it?</h2>
286
- <p class="text">Zucker (<a href="http://www.forvo.com/word/zucker/">pronunciation</a>) is the German word for sugar. It adds syntactic sugar in the form of independent, small scripts that make Ruby even more sweet. It adds a lot of useful helper methods for an improved readability and usage of your every-day Ruby code. Almost every Zucker feature comes with specs. Everything is documented on this page. The source is available at <a href="http://github.com/janlelis/zucker">github</a>.</p>
287
-
288
- <h2>Install</h2>
289
- <p class="text">
290
- <code class="scode">gem install zucker --no-rdoc --no-ri # might need sudo</code>
291
- </p>
292
- <h3 style="padding-left:1.3em">Quickstart</h3>
293
- <p class="text">
294
- <code class="scode">require 'zucker/all'</code>
295
- </p>
296
- <h2>Usage / organisation</h2>
297
- <p class="text">The gem consists of many small snippets, called <em>cubes</em>, which are bundled in <em>packs</em>. Since there aren't any dependencies within the gem, you can easily require only the packs or cubes you want:
298
- <code class="scode">require 'zucker/cube_or_pack_name'</code>
299
- </p>
300
- <h3 style="padding-left:1.3em">Packs</h3>
301
- <ul class="text">
302
- <li><a href="#control" style="text-decoration:none;font-weight:bold;">control</a>: Cubes that change program behaviour.</li>
303
- <li><a href="#env" style="text-decoration:none;font-weight:bold;">env</a>: Cubes for collecting information.</li>
304
- <li><a href="#extensions" style="text-decoration:none;font-weight:bold;">extensions</a>: More extensions for Ruby base classes.</li>
305
- <li><a href="#object" style="text-decoration:none;font-weight:bold;">object</a>: Extensions for <code>Object</code>.</li>
306
- <li><a href="#to_proc" style="text-decoration:none;font-weight:bold;">to_proc</a>: Adds some <code>to_proc</code> extensions to use with <code>&amp;</code>.</li>
307
- <li><a href="#shortcuts" style="text-decoration:none;font-weight:bold;">shortcuts</a>: Cubes that save you keystrokes or disambiguate things.</li>
308
- <li><a href="#debug" style="text-decoration:none;font-weight:bold;">debug</a>: Print debugging tools.</li>
309
- </ul>
310
- <p class="text">
311
- Furthermore, there are two meta packs available: <strong>all</strong> simply requires all cubes and <strong>default</strong> requires all cubes except <strong>debug</strong>.</p>
312
-
313
- <h3 style="padding-left:1.3em">Overview: Which methods and constants are added directly to the global namespace by the <em>default</em> pack?</h3>
314
- <p class="text">
315
-
316
- <code class="scode">Info, RubyVersion, RubyEngine, OS, Infinity, NaN, alias_for, aliases_for, egonil, nn, iterate, instance_variables_from, ivars, activate_warnings!, deactivate_warnings!, executed_directly?, directly_executed?, standalone?, library?, ignore_sigint!, ignore_sigquit!, sandbox, tap_on, make_new, (blank?, present?, mcopy)</code>
317
- </p>
318
-
319
-
320
- <h2 title="require 'zucker/control'" id="control">Cubes[control]</h2>
321
-
322
- <div class="cubes">
323
- <h3 title="require 'zucker/egonil'" id="egonil">egonil</h3>
324
- <table class="cube_table"
325
- id="egonil_cube"
326
- title="require 'zucker/egonil'"> <tr><th>Summary</th> <td>Creates a block, where <code>nil</code> does not raise <code>NoMethodError</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>egonil, nn</h5><div class="usage source" style="display:block"><div class="CodeRay">
327
- <div class="code"><pre>egonil <span style="color:#080;font-weight:bold">do</span>
328
- <span style="color:#069">nil</span>.some_methods.that[<span style="color:#A60">:do</span>].not.exist
329
- <span style="color:#080;font-weight:bold">end</span> <span style="color:#777"># =&gt; nil</span>
330
-
331
- nn{ <span style="color:#069">nil</span>.some_methods.that[<span style="color:#A60">:do</span>].not.exist } <span style="color:#777"># =&gt; nil</span>
332
- </pre></div>
333
- </div>
334
- </div></td></tr><tr><th>Information</th> <td>See <a href="http://rbjl.net/26-the-28-bytes-of-ruby-joy">this post</a> for more information and discussion.</td></tr><tr><th>Specification</th> <td> <span id="show_egonil_spec">(<a href="javascript:show('egonil_spec')">show</a>)</span>
335
- <div class="source" id="egonil_spec"><div class="CodeRay">
336
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/egonil</span><span style="color:#710">'</span></span>
337
-
338
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">egonil</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
339
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should not raise nil exceptions in the block</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
340
- proc <span style="color:#080;font-weight:bold">do</span>
341
- egonil{ <span style="color:#069">nil</span>.some_methods.that[<span style="color:#A60">:do</span>].not.exist }
342
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
343
- <span style="color:#080;font-weight:bold">end</span>
344
-
345
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should restore default behaviour after the block</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
346
- proc <span style="color:#080;font-weight:bold">do</span>
347
- egonil{ <span style="color:#069">nil</span>.some_methods.that[<span style="color:#A60">:do</span>].not.exist }
348
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
349
-
350
- proc <span style="color:#080;font-weight:bold">do</span>
351
- <span style="color:#069">nil</span>.a_method
352
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">NoMethodError</span>
353
- <span style="color:#080;font-weight:bold">end</span>
354
-
355
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">raise NoMethodError for non-nil objects</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
356
- proc <span style="color:#080;font-weight:bold">do</span>
357
- egonil{ <span style="color:#00D">5</span>.a_method }
358
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">NoMethodError</span>
359
- <span style="color:#080;font-weight:bold">end</span>
360
-
361
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should raise other Exceptions</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
362
- proc <span style="color:#080;font-weight:bold">do</span>
363
- egonil{ <span style="color:#00D">5</span> / <span style="color:#00D">0</span> }
364
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">ZeroDivisionError</span>
365
- <span style="color:#080;font-weight:bold">end</span>
366
- <span style="color:#080;font-weight:bold">end</span></pre></div>
367
- </div>
368
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_egonil_source">(<a href="javascript:show('egonil_source')">show</a>)</span>
369
- <div class="source" id="egonil_source"><div class="CodeRay">
370
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
371
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Egonil</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
372
-
373
- <span style="color:#777"># code by Yohan, slightly edited and comments by me</span>
374
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">egonil</span>(&amp;block)
375
- <span style="color:#777"># grip methods</span>
376
- ori_method_missing = <span style="color:#036;font-weight:bold">NilClass</span>.instance_method(<span style="color:#A60">:method_missing</span>)
377
- catch_method_missing = <span style="color:#036;font-weight:bold">NilClass</span>.instance_method(<span style="color:#A60">:catch_method_missing</span>)
378
- <span style="color:#777"># activate ego mode</span>
379
- <span style="color:#036;font-weight:bold">NilClass</span>.send <span style="color:#A60">:define_method</span>, <span style="color:#A60">:method_missing</span>, catch_method_missing
380
- <span style="color:#777"># run code</span>
381
- <span style="color:#080;font-weight:bold">yield</span>
382
- <span style="color:#080;font-weight:bold">ensure</span>
383
- <span style="color:#777"># no matter what happens: restore default nil behaviour</span>
384
- <span style="color:#036;font-weight:bold">NilClass</span>.send <span style="color:#A60">:define_method</span>, <span style="color:#A60">:method_missing</span>, ori_method_missing
385
- <span style="color:#080;font-weight:bold">end</span>
386
-
387
- <span style="color:#777"># this is the ego nil</span>
388
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">NilClass</span>
389
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">catch_method_missing</span>(m, *args, &amp;block)
390
- <span style="color:#069">nil</span>
391
- <span style="color:#080;font-weight:bold">end</span>
392
- <span style="color:#080;font-weight:bold">end</span>
393
-
394
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">nn</span> <span style="color:#06B;font-weight:bold">egonil</span>
395
-
396
- <span style="color:#777"># J-_-L</span></pre></div>
397
- </div>
398
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/egonil">github wiki</a></td></tr></table> <h3 title="require 'zucker/iterate'" id="iterate">iterate</h3>
399
- <table class="cube_table"
400
- id="iterate_cube"
401
- title="require 'zucker/iterate'"> <tr><th>Summary</th> <td>Iterate over one or more collections.</td></tr><tr><th>Why?</th> <td>It's like <code>.each</code> with two differences: It feels more like a control structure and you can easily iterate over multiple objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>iterate</h5><div class="usage source" style="display:block"><div class="CodeRay">
402
- <div class="code"><pre>iterate [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>], [<span style="color:#00D">3</span>,<span style="color:#00D">4</span>,<span style="color:#00D">5</span>] <span style="color:#080;font-weight:bold">do</span> |e,f|
403
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>e<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">,</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>f<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>
404
- <span style="color:#080;font-weight:bold">end</span>
405
- <span style="color:#777"># outputs</span>
406
- <span style="color:#777"># 1,3</span>
407
- <span style="color:#777"># 2,4</span>
408
- <span style="color:#777"># ,5</span>
409
- </pre></div>
410
- </div>
411
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_spec">(<a href="javascript:show('iterate_spec')">show</a>)</span>
412
- <div class="source" id="iterate_spec"><div class="CodeRay">
413
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/iterate</span><span style="color:#710">'</span></span>
414
-
415
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#iterate</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
416
- let <span style="color:#A60">:a</span> <span style="color:#080;font-weight:bold">do</span> [<span style="color:#00D">1</span>, <span style="color:#00D">2</span>, <span style="color:#00D">3</span>] <span style="color:#080;font-weight:bold">end</span>
417
- let <span style="color:#A60">:b</span> <span style="color:#080;font-weight:bold">do</span> <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">a b c d</span><span style="color:#710">|</span></span> <span style="color:#080;font-weight:bold">end</span>
418
- let <span style="color:#A60">:res</span> <span style="color:#080;font-weight:bold">do</span> <span style="color:#036;font-weight:bold">Hash</span>.new {[]} <span style="color:#080;font-weight:bold">end</span>
419
-
420
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should behave like Enumerable#each for a single argument</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
421
- iterate a <span style="color:#080;font-weight:bold">do</span> |ele|
422
- res[<span style="color:#A60">:iter</span>] &lt;&lt; ele
423
- <span style="color:#080;font-weight:bold">end</span>
424
-
425
- a.each <span style="color:#080;font-weight:bold">do</span> |ele|
426
- res[<span style="color:#A60">:each</span>] &lt;&lt; ele
427
- <span style="color:#080;font-weight:bold">end</span>
428
-
429
- res[<span style="color:#A60">:iter</span>].should == res[<span style="color:#A60">:each</span>]
430
- <span style="color:#080;font-weight:bold">end</span>
431
-
432
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should pass the right params to the block</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
433
- res = <span style="color:#036;font-weight:bold">Hash</span>.new {[]} <span style="color:#777"># TODO: why?</span>
434
- res[<span style="color:#A60">:iter_a_b</span>] = [] <span style="color:#777"># ....</span>
435
- res[<span style="color:#A60">:iter_b_a</span>] = [] <span style="color:#777"># ....</span>
436
-
437
-
438
- iterate a, b <span style="color:#080;font-weight:bold">do</span> |e,f|
439
- res[<span style="color:#A60">:iter_a_b</span>] &lt;&lt; [e, f]
440
- <span style="color:#080;font-weight:bold">end</span>
441
-
442
- res[<span style="color:#A60">:iter_a_b</span>].should == [
443
- [<span style="color:#00D">1</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>],
444
- [<span style="color:#00D">2</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span>],
445
- [<span style="color:#00D">3</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">c</span><span style="color:#710">'</span></span>],
446
- [<span style="color:#069">nil</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">d</span><span style="color:#710">'</span></span>],
447
- ]
448
-
449
- iterate b, a <span style="color:#080;font-weight:bold">do</span> |e,f|
450
- res[<span style="color:#A60">:iter_b_a</span>] &lt;&lt; [e, f]
451
- <span style="color:#080;font-weight:bold">end</span>
452
-
453
- res[<span style="color:#A60">:iter_b_a</span>].should == [
454
- [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>, <span style="color:#00D">1</span>],
455
- [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span>, <span style="color:#00D">2</span>],
456
- [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">c</span><span style="color:#710">'</span></span>, <span style="color:#00D">3</span>],
457
- [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">d</span><span style="color:#710">'</span></span>, <span style="color:#069">nil</span>],
458
- ]
459
-
460
- <span style="color:#080;font-weight:bold">end</span>
461
-
462
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return enumerators if no block is applied</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
463
- res = <span style="color:#036;font-weight:bold">Hash</span>.new {[]} <span style="color:#777"># TODO: why?</span>
464
- res[<span style="color:#A60">:iter_a_b</span>] = [] <span style="color:#777"># ....</span>
465
- res[<span style="color:#A60">:iter_b_a</span>] = [] <span style="color:#777"># ....</span>
466
-
467
- enumerator = iterate a,b
468
- enumerator.should be_kind_of(<span style="color:#069">RUBY_VERSION</span> &lt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1.9</span><span style="color:#710">'</span></span> ? <span style="color:#036;font-weight:bold">Enumerable</span>::<span style="color:#036;font-weight:bold">Enumerator</span> : <span style="color:#036;font-weight:bold">Enumerator</span>)
469
- enumerator.to_a.should == [[<span style="color:#00D">1</span>,<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>], [<span style="color:#00D">2</span>,<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span>], [<span style="color:#00D">3</span>,<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">c</span><span style="color:#710">'</span></span>], [<span style="color:#069">nil</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">d</span><span style="color:#710">'</span></span>]]
470
- <span style="color:#080;font-weight:bold">end</span>
471
- <span style="color:#080;font-weight:bold">end</span></pre></div>
472
- </div>
473
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_iterate_source">(<a href="javascript:show('iterate_source')">show</a>)</span>
474
- <div class="source" id="iterate_source"><div class="CodeRay">
475
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
476
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Iterate</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
477
-
478
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">iterate</span>(*params)
479
- <span style="color:#777"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
480
- raise <span style="color:#036;font-weight:bold">ArgumentError</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">wrong number of arguments (0)</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">if</span> params.empty?
481
-
482
- first = params.shift
483
- <span style="color:#080;font-weight:bold">if</span> params.empty? <span style="color:#777"># single param - like each</span>
484
- <span style="color:#080;font-weight:bold">if</span> block_given?
485
- first.map{|e| <span style="color:#080;font-weight:bold">yield</span> e }
486
- <span style="color:#080;font-weight:bold">else</span>
487
- first.map.to_enum
488
- <span style="color:#080;font-weight:bold">end</span>
489
- <span style="color:#080;font-weight:bold">else</span> <span style="color:#777"># multiple params</span>
490
- max_size = [first, *params].max_by(&amp;<span style="color:#A60">:count</span>).size
491
- padded_first = first.to_a + [<span style="color:#069">nil</span>]*(max_size - first.count) <span style="color:#777"># append nils</span>
492
- obj = padded_first.zip(*params)
493
- <span style="color:#080;font-weight:bold">if</span> block_given?
494
- obj.map{|es| <span style="color:#080;font-weight:bold">yield</span>(*es) }
495
- <span style="color:#080;font-weight:bold">else</span>
496
- obj.map.to_enum
497
- <span style="color:#080;font-weight:bold">end</span>
498
- <span style="color:#080;font-weight:bold">end</span>
499
- <span style="color:#080;font-weight:bold">end</span>
500
-
501
- <span style="color:#777"># J-_-L</span></pre></div>
502
- </div>
503
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/iterate">github wiki</a></td></tr></table> <h3 title="require 'zucker/kernel'" id="kernel">kernel</h3>
504
- <table class="cube_table"
505
- id="kernel_cube"
506
- title="require 'zucker/kernel'"> <tr><th>Summary</th> <td>Some useful general shortcut methods.</td></tr><tr><th>Why?</th> <td>Readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>activate_warnings!</h5><div class="usage source" style="display:block"><div class="CodeRay">
507
- <div class="code"><pre>activate_warnings! <span style="color:#777"># sets $VERBOSE to 1</span></pre></div>
508
- </div>
509
- </div><h5>deactivate_warnings!</h5><div class="usage source" style="display:block"><div class="CodeRay">
510
- <div class="code"><pre>deactivate_warnings! <span style="color:#777"># sets $VERBOSE to 0</span></pre></div>
511
- </div>
512
- </div><h5>executed_directly?, directly_executed?, standalone?</h5><div class="usage source" style="display:block"><div class="CodeRay">
513
- <div class="code"><pre>executed_directly? <span style="color:#777"># checks, if the current file is run directly -&gt; true</span></pre></div>
514
- </div>
515
- </div><h5>library?</h5><div class="usage source" style="display:block"><div class="CodeRay">
516
- <div class="code"><pre>library? <span style="color:#777"># checks, if the current file is run directly -&gt; false</span></pre></div>
517
- </div>
518
- </div><h5>irb?</h5><div class="usage source" style="display:block"><div class="CodeRay">
519
- <div class="code"><pre>irb? <span style="color:#777"># returns false if not called from IRB or RIPL</span></pre></div>
520
- </div>
521
- </div><h5>ignore_sigint!</h5><div class="usage source" style="display:block"><div class="CodeRay">
522
- <div class="code"><pre>ignore_sigint! <span style="color:#777"># blocks CTRL+C</span></pre></div>
523
- </div>
524
- </div><h5>ignore_sigquit!</h5><div class="usage source" style="display:block"><div class="CodeRay">
525
- <div class="code"><pre>ignore_sigquit! <span style="color:#777"># blocks CTRL+\</span></pre></div>
526
- </div>
527
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_kernel_spec">(<a href="javascript:show('kernel_spec')">show</a>)</span>
528
- <div class="source" id="kernel_spec"><div class="CodeRay">
529
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/kernel</span><span style="color:#710">'</span></span>
530
-
531
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">activate_warnings!</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
532
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should set $VERBOSE to true</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
533
- activate_warnings!
534
- <span style="color:#d70">$VERBOSE</span>.should == <span style="color:#069">true</span>
535
- <span style="color:#080;font-weight:bold">end</span>
536
- <span style="color:#080;font-weight:bold">end</span>
537
-
538
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">deactivate_warnings!</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
539
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should set $VERBOSE to false</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
540
- deactivate_warnings!
541
- <span style="color:#d70">$VERBOSE</span>.should == <span style="color:#069">false</span>
542
- <span style="color:#080;font-weight:bold">end</span>
543
- <span style="color:#080;font-weight:bold">end</span>
544
-
545
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">library?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
546
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return false if the file is invoked directly</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
547
- library?.should == ( <span style="color:#069">__FILE__</span> != <span style="color:#d70">$PROGRAM_NAME</span> )
548
- <span style="color:#080;font-weight:bold">end</span>
549
- <span style="color:#080;font-weight:bold">end</span>
550
-
551
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">executed_directly?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
552
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if the file is invoked directly</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
553
- executed_directly?.should == ( <span style="color:#069">__FILE__</span> == <span style="color:#d70">$PROGRAM_NAME</span> )
554
- <span style="color:#080;font-weight:bold">end</span>
555
- <span style="color:#080;font-weight:bold">end</span>
556
-
557
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">irb?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
558
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if called in IRB or RIPL</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
559
- <span style="color:#080;font-weight:bold">end</span>
560
-
561
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ignore_sigint!</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
562
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should catch ctrl+c signals</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
563
- <span style="color:#080;font-weight:bold">end</span>
564
-
565
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ignore_sigquit!</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
566
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should catch ctrl+</span><span style="color:#D20">\ </span><span style="color:#D20">signals</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
567
- <span style="color:#080;font-weight:bold">end</span>
568
-
569
-
570
- <span style="color:#777"># J-_-L</span></pre></div>
571
- </div>
572
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_kernel_source">(<a href="javascript:show('kernel_source')">show</a>)</span>
573
- <div class="source" id="kernel_source"><div class="CodeRay">
574
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
575
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Kernel</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
576
-
577
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
578
- private
579
-
580
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">activate_warnings!</span>
581
- <span style="color:#d70">$VERBOSE</span> = <span style="color:#069">true</span>
582
- <span style="color:#080;font-weight:bold">end</span>
583
-
584
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">deactivate_warnings!</span>
585
- <span style="color:#d70">$VERBOSE</span> = <span style="color:#069">false</span>
586
- <span style="color:#080;font-weight:bold">end</span>
587
-
588
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">library?</span>
589
- caller[<span style="color:#00D">0</span>].rindex(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:</span><span style="color:#D20">\d</span><span style="color:#808">+(:in `.*')?$</span><span style="color:#404">/</span></span>)
590
- <span style="color:#d70">$PROGRAM_NAME</span> != <span style="color:#d70">$`</span> <span style="color:#777"># __FILE__</span>
591
- <span style="color:#080;font-weight:bold">end</span>
592
-
593
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">executed_directly?</span>
594
- caller[<span style="color:#00D">0</span>].rindex(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:</span><span style="color:#D20">\d</span><span style="color:#808">+(:in `.*')?$</span><span style="color:#404">/</span></span>)
595
- <span style="color:#d70">$PROGRAM_NAME</span> == <span style="color:#d70">$`</span> <span style="color:#777"># __FILE__</span>
596
- <span style="color:#080;font-weight:bold">end</span>
597
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">standalone?</span> <span style="color:#06B;font-weight:bold">executed_directly?</span>
598
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">directly_executed?</span> <span style="color:#06B;font-weight:bold">executed_directly?</span>
599
-
600
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">irb?</span>
601
- (<span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#036;font-weight:bold">IRB</span>) &amp;&amp; <span style="color:#d70">$0</span> =~ <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">irb</span><span style="color:#404">/</span></span>) || (<span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#036;font-weight:bold">Ripl</span>) &amp;&amp; <span style="color:#d70">$0</span> =~ <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">ripl</span><span style="color:#404">/</span></span>)
602
- <span style="color:#080;font-weight:bold">end</span>
603
-
604
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ignore_sigint!</span> <span style="color:#777"># ctrl+c</span>
605
- <span style="color:#036;font-weight:bold">Signal</span>.trap(*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">SIGINT IGNORE</span><span style="color:#710">|</span></span>)
606
- <span style="color:#069">true</span>
607
- <span style="color:#080;font-weight:bold">end</span>
608
-
609
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ignore_sigquit!</span> <span style="color:#777"># ctrl+\</span>
610
- <span style="color:#036;font-weight:bold">Signal</span>.trap(*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">SIGQUIT IGNORE</span><span style="color:#710">|</span></span>)
611
- <span style="color:#069">true</span>
612
- <span style="color:#080;font-weight:bold">end</span>
613
- <span style="color:#080;font-weight:bold">end</span>
614
-
615
- <span style="color:#777"># J-_-L</span></pre></div>
616
- </div>
617
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/kernel">github wiki</a></td></tr></table> <h3 title="require 'zucker/sandbox'" id="sandbox">sandbox</h3>
618
- <table class="cube_table"
619
- id="sandbox_cube"
620
- title="require 'zucker/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (-&gt; integrated) in the <code>$SAFE</code> concept.</td></tr><tr><th>Methods/Usage</th> <td><h5>sandbox</h5><div class="usage source" style="display:block"><div class="CodeRay">
621
- <div class="code"><pre>sandbox <span style="color:#080;font-weight:bold">do</span>
622
- <span style="color:#777"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
623
- <span style="color:#080;font-weight:bold">end</span>
624
- <span style="color:#777"># everything's normal again</span>
625
- </pre></div>
626
- </div>
627
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_sandbox_spec">(<a href="javascript:show('sandbox_spec')">show</a>)</span>
628
- <div class="source" id="sandbox_spec"><div class="CodeRay">
629
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/sandbox</span><span style="color:#710">'</span></span>
630
-
631
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">sandbox</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
632
- <span style="color:#080;font-weight:bold">unless</span> <span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#069">RUBY_ENGINE</span>) &amp;&amp; ( <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">jruby</span><span style="color:#710">'</span></span> || <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbx</span><span style="color:#710">'</span></span> )
633
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should throw a SecurityError if bad commands are issued</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
634
- proc <span style="color:#080;font-weight:bold">do</span>
635
- sandbox <span style="color:#080;font-weight:bold">do</span>
636
- <span style="background-color:hsla(120,100%,50%,0.06)"><span style="color:#161">`</span><span style="color:#2B2">ls</span><span style="color:#161">`</span></span>
637
- <span style="color:#080;font-weight:bold">end</span>
638
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">SecurityError</span>
639
- <span style="color:#080;font-weight:bold">end</span>
640
- <span style="color:#080;font-weight:bold">end</span>
641
- <span style="color:#080;font-weight:bold">end</span></pre></div>
642
- </div>
643
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_sandbox_source">(<a href="javascript:show('sandbox_source')">show</a>)</span>
644
- <div class="source" id="sandbox_source"><div class="CodeRay">
645
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
646
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Sandbox</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
647
-
648
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
649
- private
650
-
651
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">sandbox</span>
652
- warn <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">zucker: sandbox does not work in rbx and jruby</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">if</span> <span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#069">RUBY_ENGINE</span>) &amp;&amp; ( <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">jruby</span><span style="color:#710">'</span></span> || <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbx</span><span style="color:#710">'</span></span> )
653
- <span style="color:#036;font-weight:bold">Thread</span>.start <span style="color:#080;font-weight:bold">do</span>
654
- <span style="color:#d70">$SAFE</span> = <span style="color:#00D">4</span>
655
- <span style="color:#080;font-weight:bold">yield</span>
656
- <span style="color:#080;font-weight:bold">end</span>.value
657
- <span style="color:#080;font-weight:bold">end</span>
658
- <span style="color:#080;font-weight:bold">end</span>
659
-
660
- <span style="color:#777"># J-_-L</span></pre></div>
661
- </div>
662
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8, Does not work on JRuby, since $SAFE is not supported there</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/sandbox">github wiki</a></td></tr></table> <h3 title="require 'zucker/tap'" id="tap">tap</h3>
663
- <table class="cube_table"
664
- id="tap_cube"
665
- title="require 'zucker/tap'"> <tr><th>Summary</th> <td>This cube adds the two <code>tap</code> variants <code>tap_on</code> (known as <code>returning</code> in ActiveSupport) and <code>make_new</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>tap_on</h5><div class="usage source" style="display:block"><div class="CodeRay">
666
- <div class="code"><pre>tap_on [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>] <span style="color:#080;font-weight:bold">do</span> |obj|
667
- obj[<span style="color:#00D">4</span>] = <span style="color:#00D">5</span>
668
- <span style="color:#080;font-weight:bold">end</span> <span style="color:#777">#=&gt; [1, 2, nil, nil, 5]</span>
669
- </pre></div>
670
- </div>
671
- </div><h5>make_new</h5><div class="usage source" style="display:block"><div class="CodeRay">
672
- <div class="code"><pre>make_new <span style="color:#036;font-weight:bold">Hash</span> <span style="color:#080;font-weight:bold">do</span> |obj|
673
- obj[<span style="color:#00D">1</span>] = <span style="color:#00D">2</span>
674
- <span style="color:#080;font-weight:bold">end</span> <span style="color:#777">#=&gt; {1 =&gt; 2}</span>
675
- </pre></div>
676
- </div>
677
- </div></td></tr><tr><th>Information</th> <td>Read more about using tap on the <a href="http://blog.rubybestpractices.com/posts/gregory/011-tap-that-hash.html">ruby best practices</a> blog.</td></tr><tr><th>Specification</th> <td> <span id="show_tap_spec">(<a href="javascript:show('tap_spec')">show</a>)</span>
678
- <div class="source" id="tap_spec"><div class="CodeRay">
679
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/tap</span><span style="color:#710">'</span></span>
680
-
681
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">tap_on</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
682
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should call tap on the argument and apply the block</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
683
- obj = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">an_object</span><span style="color:#710">&quot;</span></span>
684
- block = <span style="color:#A60">:reverse</span>.to_proc
685
-
686
- tap_on( obj, &amp;block ).should == obj.tap( &amp;block )
687
- <span style="color:#080;font-weight:bold">end</span>
688
- <span style="color:#080;font-weight:bold">end</span>
689
-
690
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make_new</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
691
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create a new instance of the class given as argument, apply the block on it and return result</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
692
- make_new <span style="color:#036;font-weight:bold">Hash</span> <span style="color:#080;font-weight:bold">do</span> |obj|
693
- obj[<span style="color:#00D">1</span>] = <span style="color:#00D">2</span>
694
- <span style="color:#080;font-weight:bold">end</span>.should.send(<span style="color:#A60"><span style="color:#A60">:</span><span style="color:#630">'</span><span style="color:#A60">==</span><span style="color:#630">'</span></span>, { <span style="color:#00D">1</span> =&gt; <span style="color:#00D">2</span>})
695
- <span style="color:#080;font-weight:bold">end</span>
696
- <span style="color:#080;font-weight:bold">end</span></pre></div>
697
- </div>
698
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_tap_source">(<a href="javascript:show('tap_source')">show</a>)</span>
699
- <div class="source" id="tap_source"><div class="CodeRay">
700
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
701
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Tap</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
702
-
703
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">tap_on</span>(obj, &amp;block)
704
- obj.tap(&amp;block)
705
- <span style="color:#080;font-weight:bold">end</span>
706
-
707
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">make_new</span>(what, *args, &amp;block)
708
- what.new(*args).tap(&amp;block)
709
- <span style="color:#080;font-weight:bold">end</span>
710
-
711
- <span style="color:#777"># J-_-L</span></pre></div>
712
- </div>
713
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/tap">github wiki</a></td></tr></table>
714
- </div>
715
- <h2 title="require 'zucker/env'" id="env">Cubes[env]</h2>
716
-
717
- <div class="cubes">
718
- <h3 title="require 'zucker/engine'" id="engine">engine</h3>
719
- <table class="cube_table"
720
- id="engine_cube"
721
- title="require 'zucker/engine'"> <tr><th>Summary</th> <td>Adds a <code>RubyEngine</code> class for easy compatibility checks.</td></tr><tr><th>Methods/Usage</th> <td><h5>RubyEngine</h5><div class="usage source" style="display:block"><div class="CodeRay">
722
- <div class="code"><pre><span style="color:#777"># outputs the interpreter name</span></pre></div>
723
- </div>
724
- </div><h5>RubyEngine.mri?</h5><div class="usage source" style="display:block"><div class="CodeRay">
725
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.mri? <span style="color:#777"># true if using the official Ruby interpreter</span></pre></div>
726
- </div>
727
- </div><h5>RubyEngine.jruby?</h5><div class="usage source" style="display:block"><div class="CodeRay">
728
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.jruby? <span style="color:#777"># true if using JRuby</span></pre></div>
729
- </div>
730
- </div><h5>RubyEngine.rubinius?</h5><div class="usage source" style="display:block"><div class="CodeRay">
731
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.rubinius? <span style="color:#777"># true if using rubinius</span></pre></div>
732
- </div>
733
- </div><h5>RubyEngine.ree?</h5><div class="usage source" style="display:block"><div class="CodeRay">
734
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.ree? <span style="color:#777"># true if using enterprise edition</span></pre></div>
735
- </div>
736
- </div><h5>RubyEngine.ironruby?</h5><div class="usage source" style="display:block"><div class="CodeRay">
737
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.ironruby? <span style="color:#777"># true if using IronRuby</span></pre></div>
738
- </div>
739
- </div><h5>RubyEngine.macruby?</h5><div class="usage source" style="display:block"><div class="CodeRay">
740
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.macruby? <span style="color:#777"># true if using MacRuby</span></pre></div>
741
- </div>
742
- </div><h5>RubyEngine.cardinal?</h5><div class="usage source" style="display:block"><div class="CodeRay">
743
- <div class="code"><pre><span style="color:#036;font-weight:bold">RubyEngine</span>.cardinal? <span style="color:#777"># true if using parrot</span></pre></div>
744
- </div>
745
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_engine_spec">(<a href="javascript:show('engine_spec')">show</a>)</span>
746
- <div class="source" id="engine_spec"><div class="CodeRay">
747
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/engine</span><span style="color:#710">'</span></span>
748
-
749
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">RubyEngine</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
750
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display the current ruby interpreter (to_s)</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
751
- <span style="color:#036;font-weight:bold">RubyEngine</span>
752
- <span style="color:#080;font-weight:bold">end</span>
753
-
754
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create many method aliases for asking for a specific interpreter</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
755
- <span style="color:#777"># please see the source or description file</span>
756
- <span style="color:#080;font-weight:bold">end</span>
757
- <span style="color:#080;font-weight:bold">end</span></pre></div>
758
- </div>
759
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_engine_source">(<a href="javascript:show('engine_source')">show</a>)</span>
760
- <div class="source" id="engine_source"><div class="CodeRay">
761
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
762
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Engine</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
763
-
764
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">RubyEngine</span>
765
- <span style="color:#777"># try to guess the interpreter</span>
766
- <span style="color:#33B">@interpreter</span> = <span style="color:#080;font-weight:bold">case</span>
767
- <span style="color:#080;font-weight:bold">when</span> <span style="color:#069">RUBY_PLATFORM</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">parrot</span><span style="color:#710">'</span></span>
768
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">cardinal</span><span style="color:#710">'</span></span>
769
- <span style="color:#080;font-weight:bold">when</span> <span style="color:#036;font-weight:bold">Object</span>.const_defined?(<span style="color:#A60">:RUBY_ENGINE</span>)
770
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ruby</span><span style="color:#710">'</span></span>
771
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">RUBY_DESCRIPTION</span> =~ <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Enterprise</span><span style="color:#404">/</span></span>
772
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ree</span><span style="color:#710">'</span></span>
773
- <span style="color:#080;font-weight:bold">else</span>
774
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mri</span><span style="color:#710">'</span></span>
775
- <span style="color:#080;font-weight:bold">end</span>
776
- <span style="color:#080;font-weight:bold">else</span>
777
- <span style="color:#069">RUBY_ENGINE</span>.to_s <span style="color:#777"># jruby, rbx, ironruby, macruby, etc.</span>
778
- <span style="color:#080;font-weight:bold">end</span>
779
- <span style="color:#080;font-weight:bold">else</span> <span style="color:#777"># probably 1.8</span>
780
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mri</span><span style="color:#710">'</span></span>
781
- <span style="color:#080;font-weight:bold">end</span>
782
-
783
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">self</span>
784
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">is?</span>(what)
785
- what === <span style="color:#33B">@interpreter</span>
786
- <span style="color:#080;font-weight:bold">end</span>
787
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">is</span> <span style="color:#06B;font-weight:bold">is?</span>
788
-
789
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_s</span>
790
- <span style="color:#33B">@interpreter</span>.to_s
791
- <span style="color:#080;font-weight:bold">end</span>
792
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">inspect</span> <span style="color:#06B;font-weight:bold">to_s</span>
793
-
794
- <span style="color:#777"># ask methods</span>
795
-
796
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">mri?</span>
797
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mri</span><span style="color:#710">'</span></span>
798
- <span style="color:#080;font-weight:bold">end</span>
799
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">official_ruby?</span> <span style="color:#06B;font-weight:bold">mri?</span>
800
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">ruby?</span> <span style="color:#06B;font-weight:bold">mri?</span>
801
-
802
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">jruby?</span>
803
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">jruby</span><span style="color:#710">'</span></span>
804
- <span style="color:#080;font-weight:bold">end</span>
805
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">java?</span> <span style="color:#06B;font-weight:bold">jruby?</span>
806
-
807
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">rubinius?</span>
808
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbx</span><span style="color:#710">'</span></span>
809
- <span style="color:#080;font-weight:bold">end</span>
810
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">rbx?</span> <span style="color:#06B;font-weight:bold">rubinius?</span>
811
-
812
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ree?</span>
813
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ree</span><span style="color:#710">'</span></span>
814
- <span style="color:#080;font-weight:bold">end</span>
815
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">enterprise?</span> <span style="color:#06B;font-weight:bold">ree?</span>
816
-
817
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ironruby?</span>
818
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ironruby</span><span style="color:#710">'</span></span>
819
- <span style="color:#080;font-weight:bold">end</span>
820
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">iron_ruby?</span> <span style="color:#06B;font-weight:bold">ironruby?</span>
821
-
822
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">cardinal?</span>
823
- <span style="color:#036;font-weight:bold">RubyEngine</span>.is? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">cardinal</span><span style="color:#710">'</span></span>
824
- <span style="color:#080;font-weight:bold">end</span>
825
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">parrot?</span> <span style="color:#06B;font-weight:bold">cardinal?</span>
826
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">perl?</span> <span style="color:#06B;font-weight:bold">cardinal?</span>
827
- <span style="color:#080;font-weight:bold">end</span>
828
- <span style="color:#080;font-weight:bold">end</span>
829
-
830
- <span style="color:#777"># J-_-L</span></pre></div>
831
- </div>
832
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/engine">github wiki</a></td></tr></table> <h3 title="require 'zucker/info'" id="info">info</h3>
833
- <table class="cube_table"
834
- id="info_cube"
835
- title="require 'zucker/info'"> <tr><th>Summary</th> <td>Access environment information with the <code>Info</code> module.</td></tr><tr><th>Why?</th> <td>You don't need to remember in which global variable, constant or special method the information you are searching for is hidden.</td></tr><tr><th>Methods/Usage</th> <td><h5>Info</h5><div class="usage source" style="display:block"><div class="CodeRay">
836
- <div class="code"><pre><span style="color:#777"># for example</span>
837
- <span style="color:#036;font-weight:bold">Info</span>.working_directory <span style="color:#777"># Dir.pwd</span>
838
- <span style="color:#036;font-weight:bold">Info</span>.load_path <span style="color:#777"># $:</span>
839
- <span style="color:#036;font-weight:bold">Info</span>.platform <span style="color:#777"># RUBY_PLATFORM</span>
840
- <span style="color:#036;font-weight:bold">Info</span>.current_file <span style="color:#777"># similar to __FILE__</span>
841
- <span style="color:#777"># you could also add them to the global namespace with: include Info</span>
842
- <span style="color:#777"># see the source file for the list of accessors or</span>
843
- <span style="color:#036;font-weight:bold">Info</span>.list
844
- </pre></div>
845
- </div>
846
- </div></td></tr><tr><th>Information</th> <td>Please note that it is not possible to put some of __THOSE_KEYWORDS__ in methods, so the result might slightly differ. This might affect <code>current_file</code>, <code>current_file_directory</code>, <code>current_line</code> and <code>current_method</code>.<br/>Thanks to Facets for the <code>caller</code> Regexp.</td></tr><tr><th>Specification</th> <td> <span id="show_info_spec">(<a href="javascript:show('info_spec')">show</a>)</span>
847
- <div class="source" id="info_spec"><div class="CodeRay">
848
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/info</span><span style="color:#710">'</span></span>
849
-
850
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Info</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
851
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should define accessors for global variables, constants and some special methods/keywords</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
852
- <span style="color:#777"># see sourcefile for the list</span>
853
- <span style="color:#080;font-weight:bold">end</span>
854
- <span style="color:#080;font-weight:bold">end</span></pre></div>
855
- </div>
856
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_info_source">(<a href="javascript:show('info_source')">show</a>)</span>
857
- <div class="source" id="info_source"><div class="CodeRay">
858
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
859
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbconfig</span><span style="color:#710">'</span></span>
860
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">etc</span><span style="color:#710">'</span></span>
861
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Info</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
862
-
863
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Info</span>
864
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">self</span>
865
- <span style="color:#777"># hash like access</span>
866
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">[]</span>(what)
867
- send what
868
- <span style="color:#080;font-weight:bold">end</span>
869
-
870
- <span style="color:#777"># list available info methods</span>
871
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">list</span>
872
- singleton_methods - [<span style="color:#A60">:[]</span>, <span style="color:#A60">:list</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">[]</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">list</span><span style="color:#710">'</span></span>]
873
- <span style="color:#080;font-weight:bold">end</span>
874
- <span style="color:#080;font-weight:bold">end</span>
875
-
876
- module_function
877
-
878
- <span style="color:#777"># input</span>
879
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">last_input_file</span>
880
- <span style="color:#d70">$FILENAME</span>
881
- <span style="color:#080;font-weight:bold">end</span>
882
-
883
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">last_input_line_number</span>
884
- <span style="color:#d70">$.</span>
885
- <span style="color:#080;font-weight:bold">end</span>
886
-
887
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">last_input</span>
888
- <span style="color:#d70">$_</span>
889
- <span style="color:#080;font-weight:bold">end</span>
890
-
891
- <span style="color:#777"># program</span>
892
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">program_name</span>
893
- <span style="color:#d70">$0</span>
894
- <span style="color:#080;font-weight:bold">end</span>
895
-
896
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">program_arguments</span>
897
- <span style="color:#d70">$:</span>
898
- <span style="color:#080;font-weight:bold">end</span>
899
-
900
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">loaded_programs</span>
901
- <span style="color:#d70">$&quot;</span>
902
- <span style="color:#080;font-weight:bold">end</span>
903
-
904
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">program_data</span>
905
- ::<span style="color:#036;font-weight:bold">DATA</span>
906
- <span style="color:#080;font-weight:bold">end</span>
907
-
908
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">child_program_status</span>
909
- <span style="color:#d70">$CHILD_STATUS</span>
910
- <span style="color:#080;font-weight:bold">end</span>
911
-
912
- <span style="color:#777"># system info</span>
913
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">environment</span>
914
- ::<span style="color:#036;font-weight:bold">ENV</span>
915
- <span style="color:#080;font-weight:bold">end</span>
916
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">env</span> <span style="color:#06B;font-weight:bold">environment</span>
917
-
918
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">working_directory</span>
919
- <span style="color:#036;font-weight:bold">Dir</span>.pwd
920
- <span style="color:#080;font-weight:bold">end</span>
921
-
922
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">platform</span>
923
- ::<span style="color:#036;font-weight:bold">RUBY_PLATFORM</span>
924
- <span style="color:#080;font-weight:bold">end</span>
925
-
926
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">os</span>
927
- <span style="color:#036;font-weight:bold">RbConfig</span>::<span style="color:#036;font-weight:bold">CONFIG</span>[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">host_os</span><span style="color:#710">'</span></span>]
928
- <span style="color:#080;font-weight:bold">end</span>
929
-
930
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">process_id</span>
931
- <span style="color:#d70">$$</span>
932
- <span style="color:#080;font-weight:bold">end</span>
933
-
934
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">load_path</span>
935
- <span style="color:#d70">$:</span>
936
- <span style="color:#080;font-weight:bold">end</span>
937
-
938
- <span style="color:#777"># user</span>
939
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">user_login</span>
940
- <span style="color:#036;font-weight:bold">Etc</span>.getlogin
941
- <span style="color:#080;font-weight:bold">end</span>
942
-
943
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">user_name</span>
944
- <span style="color:#036;font-weight:bold">Etc</span>.getpwnam(user_login).gecos.split(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">,</span><span style="color:#710">'</span></span>)[<span style="color:#00D">0</span>]
945
- <span style="color:#080;font-weight:bold">end</span>
946
-
947
- <span style="color:#777"># current</span>
948
-
949
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">current_file</span> <span style="color:#777"># __FILE__</span>
950
- <span style="color:#080;font-weight:bold">return</span> <span style="color:#d70">$`</span> <span style="color:#080;font-weight:bold">if</span> caller[<span style="color:#00D">0</span>].rindex(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:</span><span style="color:#D20">\d</span><span style="color:#808">+(:in `.*')?$</span><span style="color:#404">/</span></span>)
951
- <span style="color:#080;font-weight:bold">end</span>
952
-
953
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">current_file_directory</span>
954
- <span style="color:#080;font-weight:bold">if</span> current_file[<span style="color:#00D">0</span>,<span style="color:#00D">1</span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">(</span><span style="color:#710">'</span></span> &amp;&amp; current_file[<span style="color:#00D">-1</span>,<span style="color:#00D">1</span>] == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">)</span><span style="color:#710">'</span></span>
955
- current_file
956
- <span style="color:#080;font-weight:bold">else</span>
957
- <span style="color:#036;font-weight:bold">File</span>.dirname(current_file)
958
- <span style="color:#080;font-weight:bold">end</span>
959
- <span style="color:#080;font-weight:bold">end</span>
960
-
961
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">current_line</span> <span style="color:#777"># __LINE__</span>
962
- <span style="color:#080;font-weight:bold">return</span> <span style="color:#d70">$1</span>.to_i <span style="color:#080;font-weight:bold">if</span> caller[<span style="color:#00D">0</span>].rindex( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:(</span><span style="color:#D20">\d</span><span style="color:#808">+)(:in `.*')?$</span><span style="color:#404">/</span></span> )
963
- <span style="color:#080;font-weight:bold">end</span>
964
-
965
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">current_method</span> <span style="color:#777"># __method__ (except aliases)</span>
966
- <span style="color:#080;font-weight:bold">return</span> <span style="color:#d70">$1</span>.to_sym <span style="color:#080;font-weight:bold">if</span> caller(<span style="color:#00D">1</span>)[<span style="color:#00D">0</span>].rindex( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\`</span><span style="color:#808">([^</span><span style="color:#D20">\'</span><span style="color:#808">]+)</span><span style="color:#D20">\'</span><span style="color:#404">/</span></span> )
967
- <span style="color:#080;font-weight:bold">end</span>
968
-
969
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">current_callstack</span>
970
- caller
971
- <span style="color:#080;font-weight:bold">end</span>
972
-
973
- <span style="color:#777"># dealing with strings</span>
974
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">gets_separator</span>
975
- <span style="color:#d70">$/</span>
976
- <span style="color:#080;font-weight:bold">end</span>
977
-
978
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">join_separator</span>
979
- <span style="color:#d70">$,</span>
980
- <span style="color:#080;font-weight:bold">end</span>
981
-
982
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">print_separator</span>
983
- <span style="color:#d70">$,</span>
984
- <span style="color:#080;font-weight:bold">end</span>
985
-
986
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">split_separator</span>
987
- <span style="color:#d70">$;</span>
988
- <span style="color:#080;font-weight:bold">end</span>
989
-
990
- <span style="color:#777"># misc</span>
991
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">security_level</span>
992
- <span style="color:#d70">$SAFE</span>
993
- <span style="color:#080;font-weight:bold">end</span>
994
-
995
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">warnings_activated?</span>
996
- <span style="color:#d70">$VERBOSE</span>
997
- <span style="color:#080;font-weight:bold">end</span>
998
-
999
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">debug_activated?</span>
1000
- <span style="color:#d70">$DEBUG</span>
1001
- <span style="color:#080;font-weight:bold">end</span>
1002
-
1003
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">last_exception</span>
1004
- <span style="color:#d70">$!</span>
1005
- <span style="color:#080;font-weight:bold">end</span>
1006
-
1007
- <span style="color:#777"># defined objects</span>
1008
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">global_variables</span>
1009
- <span style="color:#036;font-weight:bold">Object</span>.send <span style="color:#A60">:global_variables</span>
1010
- <span style="color:#080;font-weight:bold">end</span>
1011
-
1012
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">global_constants</span>
1013
- <span style="color:#036;font-weight:bold">Object</span>.constants
1014
- <span style="color:#080;font-weight:bold">end</span>
1015
-
1016
- <span style="color:#777"># encoding (1.9)</span>
1017
- <span style="color:#777">#def source_encoding</span>
1018
- <span style="color:#777"># __ENCODING__</span>
1019
- <span style="color:#777">#end</span>
1020
-
1021
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">external_encoding</span>
1022
- <span style="color:#036;font-weight:bold">Encoding</span>.default_external
1023
- <span style="color:#080;font-weight:bold">end</span>
1024
-
1025
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">internal_encoding</span>
1026
- <span style="color:#036;font-weight:bold">Encoding</span>.default_internal
1027
- <span style="color:#080;font-weight:bold">end</span>
1028
-
1029
- <span style="color:#777"># ruby version info</span>
1030
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ruby_version</span> <span style="color:#777"># also see the RubyVersion cube</span>
1031
- ::<span style="color:#036;font-weight:bold">RUBY_VERSION</span>
1032
- <span style="color:#080;font-weight:bold">end</span>
1033
-
1034
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ruby_patchlevel</span>
1035
- ::<span style="color:#036;font-weight:bold">RUBY_PATCHLEVEL</span>
1036
- <span style="color:#080;font-weight:bold">end</span>
1037
-
1038
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ruby_description</span>
1039
- ::<span style="color:#036;font-weight:bold">RUBY_DESCRIPTION</span>
1040
- <span style="color:#080;font-weight:bold">end</span>
1041
-
1042
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ruby_release_date</span>
1043
- ::<span style="color:#036;font-weight:bold">RUBY_RELEASE_DATE</span>
1044
- <span style="color:#080;font-weight:bold">end</span>
1045
-
1046
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ruby_engine</span> <span style="color:#777"># warning! not support by every implementation. It's saver to use the RubyEngine cube</span>
1047
- ::<span style="color:#036;font-weight:bold">RUBY_ENGINE</span>
1048
- <span style="color:#080;font-weight:bold">end</span>
1049
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1050
- </div>
1051
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (not all information)</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/info">github wiki</a></td></tr></table> <h3 title="require 'zucker/os'" id="os">os</h3>
1052
- <table class="cube_table"
1053
- id="os_cube"
1054
- title="require 'zucker/os'"> <tr><th>Summary</th> <td>Detect the operating system.</td></tr><tr><th>Why?</th> <td>Checking for <code>RUBY_PLATFORM</code> does not report the OS when using JRuby and for better readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>OS</h5><div class="usage source" style="display:block"><div class="CodeRay">
1055
- <div class="code"><pre><span style="color:#777"># displays the operating system</span></pre></div>
1056
- </div>
1057
- </div><h5>OS.linux?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1058
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.linux? <span style="color:#777"># true if you are on a linux machine</span></pre></div>
1059
- </div>
1060
- </div><h5>OS.mac?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1061
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.mac? <span style="color:#777"># true for macs</span></pre></div>
1062
- </div>
1063
- </div><h5>OS.bsd?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1064
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.bsd? <span style="color:#777"># true for bsds</span></pre></div>
1065
- </div>
1066
- </div><h5>OS.windows?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1067
- <div class="code"><pre>include <span style="color:#036;font-weight:bold">OS</span>; windows? <span style="color:#777"># true for windows. Example of including OS to get the os methods into the global namespace</span></pre></div>
1068
- </div>
1069
- </div><h5>OS.solaris?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1070
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.solaris? <span style="color:#777"># true for solaris/sunos</span></pre></div>
1071
- </div>
1072
- </div><h5>OS.posix?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1073
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.posix? <span style="color:#777"># true if system is posix compatible</span></pre></div>
1074
- </div>
1075
- </div><h5>OS.is?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1076
- <div class="code"><pre><span style="color:#036;font-weight:bold">OS</span>.is? <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">x</span><span style="color:#404">/</span></span> <span style="color:#777"># matches the regex against the os string</span></pre></div>
1077
- </div>
1078
- </div></td></tr><tr><th>Information</th> <td>This cube depends on system information and may not be 100% accurate.<br/>Syntax and semantic inspired by Roger Pack's <a href="http://github.com/rdp/os">os gem</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_os_spec">(<a href="javascript:show('os_spec')">show</a>)</span>
1079
- <div class="source" id="os_spec"><div class="CodeRay">
1080
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/os</span><span style="color:#710">'</span></span>
1081
-
1082
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">OS</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1083
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display the used operation system (to_s)</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1084
- <span style="color:#080;font-weight:bold">end</span>
1085
-
1086
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">linux?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1087
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is a linux</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1088
- <span style="color:#080;font-weight:bold">end</span>
1089
-
1090
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mac?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1091
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is a mac or darwin</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1092
- <span style="color:#080;font-weight:bold">end</span>
1093
-
1094
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">bsd?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1095
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is a bsd</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1096
- <span style="color:#080;font-weight:bold">end</span>
1097
-
1098
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">windows?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1099
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is a windows</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1100
- <span style="color:#080;font-weight:bold">end</span>
1101
-
1102
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">solaris?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1103
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is a solaris or sunos</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1104
- <span style="color:#080;font-weight:bold">end</span>
1105
-
1106
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">posix?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1107
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return true if OS is posix compatible</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
1108
- <span style="color:#080;font-weight:bold">end</span>
1109
-
1110
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">OS.is?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1111
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should test if RbConfig::CONFIG['host_os'] matches the regex (boolean value)</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
1112
- <span style="color:#036;font-weight:bold">OS</span>.is?(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">linux</span><span style="color:#404">/</span></span>).should ==
1113
- !!( <span style="color:#036;font-weight:bold">RbConfig</span>::<span style="color:#036;font-weight:bold">CONFIG</span>[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">host_os</span><span style="color:#710">'</span></span>] =~ <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">linux</span><span style="color:#404">/</span></span> )
1114
- <span style="color:#080;font-weight:bold">end</span>
1115
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1116
- </div>
1117
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_os_source">(<a href="javascript:show('os_source')">show</a>)</span>
1118
- <div class="source" id="os_source"><div class="CodeRay">
1119
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1120
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbconfig</span><span style="color:#710">'</span></span>
1121
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Os</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1122
-
1123
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">OS</span>
1124
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">self</span>
1125
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">is?</span>(what)
1126
- what === <span style="color:#036;font-weight:bold">RbConfig</span>::<span style="color:#036;font-weight:bold">CONFIG</span>[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">host_os</span><span style="color:#710">'</span></span>]
1127
- <span style="color:#080;font-weight:bold">end</span>
1128
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">is</span> <span style="color:#06B;font-weight:bold">is?</span>
1129
-
1130
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_s</span>
1131
- <span style="color:#036;font-weight:bold">RbConfig</span>::<span style="color:#036;font-weight:bold">CONFIG</span>[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">host_os</span><span style="color:#710">'</span></span>]
1132
- <span style="color:#080;font-weight:bold">end</span>
1133
- <span style="color:#080;font-weight:bold">end</span>
1134
-
1135
- module_function
1136
-
1137
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">linux?</span>
1138
- <span style="color:#036;font-weight:bold">OS</span>.is?( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">linux|cygwin</span><span style="color:#404">/</span></span> )
1139
- <span style="color:#080;font-weight:bold">end</span>
1140
-
1141
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">mac?</span>
1142
- <span style="color:#036;font-weight:bold">OS</span>.is?( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">mac|darwin</span><span style="color:#404">/</span></span> )
1143
- <span style="color:#080;font-weight:bold">end</span>
1144
-
1145
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">bsd?</span>
1146
- <span style="color:#036;font-weight:bold">OS</span>.is?( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">bsd</span><span style="color:#404">/</span></span> )
1147
- <span style="color:#080;font-weight:bold">end</span>
1148
-
1149
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">windows?</span>
1150
- <span style="color:#036;font-weight:bold">OS</span>.is?( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">mswin|mingw</span><span style="color:#404">/</span></span> )
1151
- <span style="color:#080;font-weight:bold">end</span>
1152
-
1153
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">solaris?</span>
1154
- <span style="color:#036;font-weight:bold">OS</span>.is?( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">solaris|sunos</span><span style="color:#404">/</span></span> )
1155
- <span style="color:#080;font-weight:bold">end</span>
1156
-
1157
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">posix?</span>
1158
- linux? <span style="color:#080;font-weight:bold">or</span> mac? <span style="color:#080;font-weight:bold">or</span> bsd? <span style="color:#080;font-weight:bold">or</span> solaris? <span style="color:#080;font-weight:bold">or</span> <span style="color:#080;font-weight:bold">begin</span>
1159
- fork <span style="color:#080;font-weight:bold">do</span> <span style="color:#080;font-weight:bold">end</span>
1160
- <span style="color:#069">true</span>
1161
- <span style="color:#080;font-weight:bold">rescue</span> <span style="color:#036;font-weight:bold">NotImplementedError</span>, <span style="color:#036;font-weight:bold">NoMethodError</span>
1162
- <span style="color:#069">false</span>
1163
- <span style="color:#080;font-weight:bold">end</span>
1164
- <span style="color:#080;font-weight:bold">end</span>
1165
-
1166
- <span style="color:#777">#def symbian?</span>
1167
- <span style="color:#777">#TODO who knows what symbian returns?</span>
1168
- <span style="color:#777">#end</span>
1169
-
1170
- <span style="color:#777"># ...</span>
1171
- <span style="color:#080;font-weight:bold">end</span>
1172
-
1173
- <span style="color:#777"># J-_-L</span></pre></div>
1174
- </div>
1175
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/os">github wiki</a></td></tr></table>
1176
- </div>
1177
- <h2 title="require 'zucker/extensions'" id="extensions">Cubes[extensions]</h2>
1178
-
1179
- <div class="cubes">
1180
- <h3 title="require 'zucker/array'" id="array">array</h3>
1181
- <table class="cube_table"
1182
- id="array_cube"
1183
- title="require 'zucker/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code>.</td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&amp;</code> and <code>|</code>, but why does it not have <code>^</code>?</p></td></tr><tr><th>Methods/Usage</th> <td><h5>Array#^</h5><div class="usage source" style="display:block"><div class="CodeRay">
1184
- <div class="code"><pre>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>] ^ [<span style="color:#00D">3</span>,<span style="color:#00D">4</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>] <span style="color:#777"># =&gt; [1,2,5,6]</span></pre></div>
1185
- </div>
1186
- </div><h5>Array#chrs</h5><div class="usage source" style="display:block"><div class="CodeRay">
1187
- <div class="code"><pre>[<span style="color:#00D">72</span>, <span style="color:#00D">97</span>, <span style="color:#00D">108</span>, <span style="color:#00D">108</span>, <span style="color:#00D">111</span>].chrs <span style="color:#777"># =&gt; 'Hallo'</span></pre></div>
1188
- </div>
1189
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_array_spec">(<a href="javascript:show('array_spec')">show</a>)</span>
1190
- <div class="source" id="array_spec"><div class="CodeRay">
1191
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/array</span><span style="color:#710">'</span></span>
1192
-
1193
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Array#^</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1194
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should do an exclusive or</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1195
- a = [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>]
1196
- b = [<span style="color:#00D">3</span>,<span style="color:#00D">4</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>]
1197
- (a^b).should == [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>]
1198
- <span style="color:#080;font-weight:bold">end</span>
1199
- <span style="color:#080;font-weight:bold">end</span>
1200
-
1201
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Array#chrs</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1202
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should convert the array to a string, using each element as ord value for the char</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1203
- [<span style="color:#00D">72</span>, <span style="color:#00D">97</span>, <span style="color:#00D">108</span>, <span style="color:#00D">108</span>, <span style="color:#00D">111</span>].chrs.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hallo</span><span style="color:#710">'</span></span>
1204
- <span style="color:#080;font-weight:bold">end</span>
1205
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1206
- </div>
1207
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_array_source">(<a href="javascript:show('array_source')">show</a>)</span>
1208
- <div class="source" id="array_source"><div class="CodeRay">
1209
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1210
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Array</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1211
-
1212
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Array</span>
1213
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">^</span>(other)
1214
- (<span style="color:#069">self</span> - other) + (other - <span style="color:#069">self</span>)
1215
- <span style="color:#080;font-weight:bold">end</span>
1216
-
1217
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">chrs</span>
1218
- <span style="color:#069">self</span>.pack <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">C*</span><span style="color:#710">'</span></span>
1219
- <span style="color:#080;font-weight:bold">end</span>
1220
- <span style="color:#080;font-weight:bold">end</span>
1221
-
1222
- <span style="color:#777"># J-_-L</span></pre></div>
1223
- </div>
1224
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/array">github wiki</a></td></tr></table> <h3 title="require 'zucker/case'" id="case">case</h3>
1225
- <table class="cube_table"
1226
- id="case_cube"
1227
- title="require 'zucker/case'"> <tr><th>Summary</th> <td>Provides <code>String#to_camel</code> and <code>String#to_snake</code>.</td></tr><tr><th>Why?</th> <td>It kind of lies in the nature of Ruby.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#to_camel</h5><div class="usage source" style="display:block"><div class="CodeRay">
1228
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">was_snake_string</span><span style="color:#710">'</span></span>.to_camel <span style="color:#777"># =&gt; 'WasSnakeString'</span></pre></div>
1229
- </div>
1230
- </div><h5>String#to_snake</h5><div class="usage source" style="display:block"><div class="CodeRay">
1231
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">WasCamelString</span><span style="color:#710">'</span></span> <span style="color:#777"># =&gt; 'was_camel_string'</span></pre></div>
1232
- </div>
1233
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_case_spec">(<a href="javascript:show('case_spec')">show</a>)</span>
1234
- <div class="source" id="case_spec"><div class="CodeRay">
1235
- <div class="code"><pre><span style="color:#080;font-weight:bold">if</span> <span style="color:#080;font-weight:bold">defined?</span> <span style="color:#036;font-weight:bold">BasicObject</span> <span style="color:#777"># 1.9 only</span>
1236
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/case</span><span style="color:#710">'</span></span>
1237
-
1238
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#to_camel</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1239
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should turn a snake_cased string to CamelCase</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1240
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">was_snake_case</span><span style="color:#710">'</span></span>.to_camel.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">WasSnakeCase</span><span style="color:#710">'</span></span>
1241
- <span style="color:#080;font-weight:bold">end</span>
1242
- <span style="color:#080;font-weight:bold">end</span>
1243
-
1244
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#to_snake</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1245
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should turn a CamelCased string to snake_case</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1246
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">WasCamelCase</span><span style="color:#710">'</span></span>.to_snake.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">was_camel_case</span><span style="color:#710">'</span></span>
1247
- <span style="color:#080;font-weight:bold">end</span>
1248
- <span style="color:#080;font-weight:bold">end</span>
1249
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1250
- </div>
1251
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_case_source">(<a href="javascript:show('case_source')">show</a>)</span>
1252
- <div class="source" id="case_source"><div class="CodeRay">
1253
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1254
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Case</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1255
-
1256
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">String</span>
1257
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_snake</span>
1258
- gsub(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">(?&lt;!^)[A-Z]</span><span style="color:#404">/</span></span>) <span style="color:#080;font-weight:bold">do</span> <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">_</span><span style="color:#710">#</span><span style="color:#d70">$&amp;</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">end</span>.downcase
1259
- <span style="color:#080;font-weight:bold">end</span>
1260
-
1261
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_camel</span>
1262
- gsub(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">(?:^|_)([a-z])</span><span style="color:#404">/</span></span>) <span style="color:#080;font-weight:bold">do</span> <span style="color:#d70">$1</span>.upcase <span style="color:#080;font-weight:bold">end</span>
1263
- <span style="color:#080;font-weight:bold">end</span>
1264
- <span style="color:#080;font-weight:bold">end</span>
1265
-
1266
- <span style="color:#777"># J-_-L</span></pre></div>
1267
- </div>
1268
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/case">github wiki</a></td></tr></table> <h3 title="require 'zucker/enumerable'" id="enumerable">enumerable</h3>
1269
- <table class="cube_table"
1270
- id="enumerable_cube"
1271
- title="require 'zucker/enumerable'"> <tr><th>Summary</th> <td><code>Enumerable</code> extensions.</td></tr><tr><th>Methods/Usage</th> <td><h5>Enumerable#mash</h5><div class="usage source" style="display:block"><div class="CodeRay">
1272
- <div class="code"><pre>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].mash{|e| [e, e.to_s] } <span style="color:#777"># =&gt; {1=&gt;'1',2=&gt;'2',3=&gt;'3'}</span></pre></div>
1273
- </div>
1274
- </div></td></tr><tr><th>Information</th> <td>Inspired by Ruby Facets' <code>mash</code>.</td></tr><tr><th>Specification</th> <td> <span id="show_enumerable_spec">(<a href="javascript:show('enumerable_spec')">show</a>)</span>
1275
- <div class="source" id="enumerable_spec"><div class="CodeRay">
1276
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/enumerable</span><span style="color:#710">'</span></span>
1277
-
1278
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Enumerable#mash</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1279
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should &quot;map&quot; a hash</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1280
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].mash{|e| [e, e.to_s] }.should == {<span style="color:#00D">1</span>=&gt;<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1</span><span style="color:#710">'</span></span>,<span style="color:#00D">2</span>=&gt;<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">2</span><span style="color:#710">'</span></span>,<span style="color:#00D">3</span>=&gt;<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">3</span><span style="color:#710">'</span></span>,}
1281
- <span style="color:#080;font-weight:bold">end</span>
1282
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1283
- </div>
1284
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_enumerable_source">(<a href="javascript:show('enumerable_source')">show</a>)</span>
1285
- <div class="source" id="enumerable_source"><div class="CodeRay">
1286
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1287
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Enumerable</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1288
-
1289
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Enumerable</span>
1290
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">mash</span>
1291
- ret = {}
1292
- each{ |kv|
1293
- ret.store( *(<span style="color:#080;font-weight:bold">yield</span>(kv)[<span style="color:#00D">0</span>,<span style="color:#00D">2</span>]) )
1294
- }
1295
- ret
1296
- <span style="color:#080;font-weight:bold">end</span>
1297
- <span style="color:#080;font-weight:bold">end</span>
1298
-
1299
- <span style="color:#777"># J-_-L</span></pre></div>
1300
- </div>
1301
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/enumerable">github wiki</a></td></tr></table> <h3 title="require 'zucker/file'" id="file">file</h3>
1302
- <table class="cube_table"
1303
- id="file_cube"
1304
- title="require 'zucker/file'"> <tr><th>Summary</th> <td>Extensions for <code>File</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>File.gsub</h5><div class="usage source" style="display:block"><div class="CodeRay">
1305
- <div class="code"><pre><span style="color:#036;font-weight:bold">File</span>.gsub <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">some_filename</span><span style="color:#710">'</span></span>, <span style="color:#777"># will read the file and substitute the hash keys with their values</span>
1306
- <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">hi</span><span style="color:#404">/</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">cu</span><span style="color:#710">'</span></span>,
1307
- <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span> =&gt; proc{ |m| (m.to_i+<span style="color:#00D">1</span>).to_s }
1308
- </pre></div>
1309
- </div>
1310
- </div><h5>File.delete!</h5><div class="usage source" style="display:block"><div class="CodeRay">
1311
- <div class="code"><pre><span style="color:#036;font-weight:bold">File</span>.delete! <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">some_filename</span><span style="color:#710">'</span></span> <span style="color:#777"># will delete the file, don't care if it exist.</span></pre></div>
1312
- </div>
1313
- </div></td></tr><tr><th>Information</th> <td>Idea for <code>File.delete!</code> from sugar-high.</td></tr><tr><th>Specification</th> <td> <span id="show_file_spec">(<a href="javascript:show('file_spec')">show</a>)</span>
1314
- <div class="source" id="file_spec"><div class="CodeRay">
1315
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/file</span><span style="color:#710">'</span></span>
1316
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">fileutils</span><span style="color:#710">'</span></span>
1317
-
1318
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">File.gsub</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1319
- let <span style="color:#A60">:random_filename</span> <span style="color:#080;font-weight:bold">do</span>
1320
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">test_</span><span style="color:#710">'</span></span> + (<span style="color:#00D">0</span>..<span style="color:#00D">20</span>).map{|e| [*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>..<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">z</span><span style="color:#710">'</span></span>].send <span style="color:#069">RUBY_VERSION</span> &gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1.9</span><span style="color:#710">'</span></span> ? <span style="color:#A60">:sample</span> : <span style="color:#A60">:choice</span> }*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>
1321
- <span style="color:#080;font-weight:bold">end</span>
1322
-
1323
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should read filename in arg1, substitute every key in the arg2 with its value and save the file</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1324
- <span style="color:#036;font-weight:bold">File</span>.open(random_filename,<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">w</span><span style="color:#710">'</span></span>){ |file|
1325
- file.print <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should read filename in arg1, substitute every key in the arg2 with its value and save the file</span><span style="color:#710">'</span></span>
1326
- }
1327
- <span style="color:#036;font-weight:bold">File</span>.gsub random_filename,
1328
- <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">read</span><span style="color:#404">/</span></span> =&gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">write</span><span style="color:#710">'</span></span>,
1329
- <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\d</span><span style="color:#808">+</span><span style="color:#404">/</span></span> =&gt; proc{|m| (m.to_i+<span style="color:#00D">1</span>).to_s }
1330
-
1331
- <span style="color:#036;font-weight:bold">File</span>.read(random_filename).should ==
1332
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should write filename in arg2, substitute every key in the arg3 with its value and save the file</span><span style="color:#710">'</span></span>
1333
-
1334
- <span style="color:#036;font-weight:bold">FileUtils</span>.rm random_filename
1335
- <span style="color:#080;font-weight:bold">end</span>
1336
- <span style="color:#080;font-weight:bold">end</span>
1337
-
1338
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">File.delete!</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1339
- let <span style="color:#A60">:random_filename</span> <span style="color:#080;font-weight:bold">do</span>
1340
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">test_</span><span style="color:#710">'</span></span> + (<span style="color:#00D">0</span>..<span style="color:#00D">20</span>).map{|e| [*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>..<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">z</span><span style="color:#710">'</span></span>].send <span style="color:#069">RUBY_VERSION</span> &gt; <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1.9</span><span style="color:#710">'</span></span> ? <span style="color:#A60">:sample</span> : <span style="color:#A60">:choice</span> }*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>
1341
- <span style="color:#080;font-weight:bold">end</span>
1342
-
1343
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should delete the filename given as argument if it exists + return non-nil</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1344
- <span style="color:#036;font-weight:bold">FileUtils</span>.touch random_filename
1345
- res = <span style="color:#069">false</span>
1346
- proc <span style="color:#080;font-weight:bold">do</span>
1347
- res = <span style="color:#036;font-weight:bold">File</span>.delete! random_filename
1348
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
1349
- res.should be_true
1350
- <span style="color:#080;font-weight:bold">end</span>
1351
-
1352
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should do nothing if the filename given as argument does not exist + return nil</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1353
- res = <span style="color:#069">false</span>
1354
- proc <span style="color:#080;font-weight:bold">do</span>
1355
- res = <span style="color:#036;font-weight:bold">File</span>.delete! random_filename
1356
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
1357
- res.should be_false
1358
- <span style="color:#080;font-weight:bold">end</span>
1359
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1360
- </div>
1361
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_file_source">(<a href="javascript:show('file_source')">show</a>)</span>
1362
- <div class="source" id="file_source"><div class="CodeRay">
1363
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1364
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">File</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1365
-
1366
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">File</span>
1367
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">gsub</span>(filename, regex_hash)
1368
- data = <span style="color:#036;font-weight:bold">File</span>.read filename
1369
- <span style="color:#036;font-weight:bold">File</span>.open(filename,<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">w</span><span style="color:#710">'</span></span>){ |file|
1370
- regex_hash.each{ |regex, new_string|
1371
- regex = regex.to_s <span style="color:#080;font-weight:bold">unless</span> regex.is_a? <span style="color:#036;font-weight:bold">Regexp</span>
1372
-
1373
- <span style="color:#080;font-weight:bold">if</span> new_string.is_a? <span style="color:#036;font-weight:bold">Proc</span>
1374
- data.gsub! regex, &amp;new_string
1375
- <span style="color:#080;font-weight:bold">else</span>
1376
- data.gsub! regex, new_string
1377
- <span style="color:#080;font-weight:bold">end</span>
1378
- }
1379
- file.print data
1380
- }
1381
- <span style="color:#080;font-weight:bold">end</span>
1382
-
1383
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">delete!</span> filename
1384
- <span style="color:#080;font-weight:bold">return</span> <span style="color:#069">nil</span> <span style="color:#080;font-weight:bold">if</span> !<span style="color:#036;font-weight:bold">File</span>.exist?(filename)
1385
- <span style="color:#036;font-weight:bold">File</span>.delete filename
1386
- <span style="color:#080;font-weight:bold">end</span>
1387
- <span style="color:#080;font-weight:bold">end</span>
1388
-
1389
- <span style="color:#777"># J-_-L</span></pre></div>
1390
- </div>
1391
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/file">github wiki</a></td></tr></table> <h3 title="require 'zucker/hash'" id="hash">hash</h3>
1392
- <table class="cube_table"
1393
- id="hash_cube"
1394
- title="require 'zucker/hash'"> <tr><th>Summary</th> <td>Some sugar for dealing with hashes.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash.zip</h5><div class="usage source" style="display:block"><div class="CodeRay">
1395
- <div class="code"><pre><span style="color:#036;font-weight:bold">Hash</span>.zip [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>], [<span style="color:#00D">4</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>] <span style="color:#777"># =&gt; {1=&gt;4, 2=&gt;5, 3=&gt;6}</span></pre></div>
1396
- </div>
1397
- </div><h5>Hash#&lt;&lt;</h5><div class="usage source" style="display:block"><div class="CodeRay">
1398
- <div class="code"><pre>{<span style="color:#00D">1</span>=&gt;<span style="color:#00D">2</span>} &lt;&lt; [<span style="color:#00D">3</span>, <span style="color:#00D">4</span>] <span style="color:#777"># =&gt; { 1=&gt;2, 3=&gt;4 }</span>
1399
- {<span style="color:#00D">1</span>=&gt;<span style="color:#00D">2</span>} &lt;&lt; { <span style="color:#00D">5</span>=&gt;<span style="color:#00D">6</span> } <span style="color:#777"># =&gt; { 1=&gt;2, 5=&gt;6 }</span>
1400
- </pre></div>
1401
- </div>
1402
- </div><h5>Hash#&amp;</h5><div class="usage source" style="display:block"><div class="CodeRay">
1403
- <div class="code"><pre>{ <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">5</span>, <span style="color:#00D">3</span>=&gt;<span style="color:#00D">6</span> } &amp; { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">7</span> } <span style="color:#777"># =&gt; { 1=&gt;4 }</span></pre></div>
1404
- </div>
1405
- </div></td></tr><tr><th>Information</th> <td>Some of the operators are inspired by Ruby Facets.</td></tr><tr><th>Specification</th> <td> <span id="show_hash_spec">(<a href="javascript:show('hash_spec')">show</a>)</span>
1406
- <div class="source" id="hash_spec"><div class="CodeRay">
1407
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/hash</span><span style="color:#710">'</span></span>
1408
-
1409
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hash.zip</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1410
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should zip together both given enumerables and take them as key=&gt;values for a new hash</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1411
- <span style="color:#036;font-weight:bold">Hash</span>.zip( [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>], [<span style="color:#00D">4</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>] ).should == { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">5</span>, <span style="color:#00D">3</span>=&gt;<span style="color:#00D">6</span> }
1412
- <span style="color:#080;font-weight:bold">end</span>
1413
- <span style="color:#080;font-weight:bold">end</span>
1414
-
1415
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hash#&lt;&lt;</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1416
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should append new elements to the hash</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1417
- a = { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">5</span>, <span style="color:#00D">3</span>=&gt;<span style="color:#00D">6</span> }
1418
- a &lt;&lt; { <span style="color:#00D">4</span>=&gt;<span style="color:#00D">7</span> }
1419
- a &lt;&lt; [<span style="color:#00D">5</span>, <span style="color:#00D">8</span>]
1420
- a.should == { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">5</span>, <span style="color:#00D">3</span>=&gt;<span style="color:#00D">6</span>, <span style="color:#00D">4</span>=&gt;<span style="color:#00D">7</span>, <span style="color:#00D">5</span>=&gt;<span style="color:#00D">8</span> }
1421
- <span style="color:#080;font-weight:bold">end</span>
1422
- <span style="color:#080;font-weight:bold">end</span>
1423
-
1424
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hash#&amp;</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1425
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should select a sub hash containt only equal key-value pairs</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1426
- a = { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">5</span>, <span style="color:#00D">3</span>=&gt;<span style="color:#00D">6</span> }
1427
- b= { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span>, <span style="color:#00D">2</span>=&gt;<span style="color:#00D">7</span> }
1428
- (a &amp; b).should == { <span style="color:#00D">1</span>=&gt;<span style="color:#00D">4</span> }
1429
- <span style="color:#080;font-weight:bold">end</span>
1430
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1431
- </div>
1432
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_hash_source">(<a href="javascript:show('hash_source')">show</a>)</span>
1433
- <div class="source" id="hash_source"><div class="CodeRay">
1434
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1435
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Hash</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1436
-
1437
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Hash</span>
1438
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#069">self</span>.<span style="color:#06B;font-weight:bold">zip</span>(keys,values)
1439
- <span style="color:#036;font-weight:bold">Hash</span>[ *keys.zip( values ).flatten ]
1440
- <span style="color:#080;font-weight:bold">end</span>
1441
-
1442
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">&lt;&lt;</span>(other)
1443
- <span style="color:#080;font-weight:bold">case</span>
1444
- <span style="color:#080;font-weight:bold">when</span> other.is_a?(<span style="color:#036;font-weight:bold">Hash</span>)
1445
- merge! other
1446
- <span style="color:#080;font-weight:bold">when</span> other.is_a?(<span style="color:#036;font-weight:bold">Enumerable</span>) || other.respond_to?(<span style="color:#A60">:to_splat</span>)
1447
- merge! <span style="color:#036;font-weight:bold">Hash</span>[*other]
1448
- <span style="color:#080;font-weight:bold">else</span>
1449
- raise <span style="color:#036;font-weight:bold">TypeError</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">can only append other Hashs and Enumerables (or Classes that implement to_splat)</span><span style="color:#710">'</span></span>
1450
- <span style="color:#080;font-weight:bold">end</span>
1451
- <span style="color:#080;font-weight:bold">end</span>
1452
-
1453
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">&amp;</span>(other)
1454
- <span style="color:#036;font-weight:bold">Hash</span>[ *select{ |k,v|
1455
- other[k] == v
1456
- }.flatten ]
1457
- <span style="color:#080;font-weight:bold">end</span>
1458
- <span style="color:#080;font-weight:bold">end</span>
1459
-
1460
- <span style="color:#777"># J-_-L</span></pre></div>
1461
- </div>
1462
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/hash">github wiki</a></td></tr></table> <h3 title="require 'zucker/string'" id="string">string</h3>
1463
- <table class="cube_table"
1464
- id="string_cube"
1465
- title="require 'zucker/string'"> <tr><th>Summary</th> <td>Generic <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><div class="usage source" style="display:block"><div class="CodeRay">
1466
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1234abc5678</span><span style="color:#710">'</span></span> - <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span> - <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span> <span style="color:#777"># =&gt; ac</span></pre></div>
1467
- </div>
1468
- </div><h5>String#^</h5><div class="usage source" style="display:block"><div class="CodeRay">
1469
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Yes vs No</span><span style="color:#710">'</span></span>^<span style="color:#00D">2</span> <span style="color:#777"># =&gt; 's vs No'</span></pre></div>
1470
- </div>
1471
- </div><h5>String#lchomp</h5><div class="usage source" style="display:block"><div class="CodeRay">
1472
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> Yes</span><span style="color:#710">'</span></span>.lchomp <span style="color:#777"># =&gt; ' Yes'</span></pre></div>
1473
- </div>
1474
- </div><h5>String#lchomp!</h5><div class="usage source" style="display:block"><div class="CodeRay">
1475
- <div class="code"><pre><span style="color:#777"># mutable lchomp version</span></pre></div>
1476
- </div>
1477
- </div><h5>String#ords</h5><div class="usage source" style="display:block"><div class="CodeRay">
1478
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hallo</span><span style="color:#710">'</span></span>.ords <span style="color:#777"># =&gt; [72, 97, 108, 108, 111]</span></pre></div>
1479
- </div>
1480
- </div><h5>String#constantize</h5><div class="usage source" style="display:block"><div class="CodeRay">
1481
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object</span><span style="color:#710">'</span></span>.constantize <span style="color:#777"># =&gt; Object</span>
1482
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Spec::VERSION</span><span style="color:#710">'</span></span>.constantize <span style="color:#777"># =&gt; Spec::VERSION if rspec is loaded</span>
1483
- <span style="color:#777"># you can also pass a parameter or block to define what happens, when constant does not exist</span>
1484
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">IdontExist</span><span style="color:#710">'</span></span>.constantize(<span style="color:#036;font-weight:bold">Array</span>) <span style="color:#777"># =&gt; Array</span>
1485
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String5</span><span style="color:#710">'</span></span>.constantize <span style="color:#080;font-weight:bold">do</span> |string|
1486
- string.chop.constantize
1487
- <span style="color:#080;font-weight:bold">end</span> <span style="color:#777"># =&gt; String</span>
1488
- </pre></div>
1489
- </div>
1490
- </div></td></tr><tr><th>Information</th> <td><code>constantize</code> is an improved version of ActiveSupport's one.</td></tr><tr><th>Specification</th> <td> <span id="show_string_spec">(<a href="javascript:show('string_spec')">show</a>)</span>
1491
- <div class="source" id="string_spec"><div class="CodeRay">
1492
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/string</span><span style="color:#710">'</span></span>
1493
-
1494
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#-</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1495
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should remove the applied Regexp or String from self via gsub</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1496
- (<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1234abc5678</span><span style="color:#710">'</span></span> - <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span> - <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ac</span><span style="color:#710">'</span></span>
1497
- <span style="color:#080;font-weight:bold">end</span>
1498
- <span style="color:#080;font-weight:bold">end</span>
1499
-
1500
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#^</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1501
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should give C-like substring access to strings</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1502
- string = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Theoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1503
-
1504
- (string^<span style="color:#00D">0</span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Theoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1505
- (string^<span style="color:#00D">1</span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">heoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1506
- (string^<span style="color:#00D">13</span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1507
- (string^<span style="color:#00D">-1</span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Theoretische Informatik ist voll geil</span><span style="color:#710">'</span></span>
1508
- (string^<span style="color:#00D">38</span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>
1509
- (string^<span style="color:#00D">99</span>).should == <span style="color:#069">nil</span>
1510
- <span style="color:#080;font-weight:bold">end</span>
1511
- <span style="color:#080;font-weight:bold">end</span>
1512
-
1513
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#lchomp</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1514
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should chomp on the left side</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1515
- string = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Theoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1516
- string.lchomp(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">T</span><span style="color:#710">'</span></span>).should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">heoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1517
- <span style="color:#080;font-weight:bold">end</span>
1518
- <span style="color:#080;font-weight:bold">end</span>
1519
-
1520
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#ords</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1521
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should unpack characters</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1522
- string = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Theoretische Informatik ist voll geil!</span><span style="color:#710">'</span></span>
1523
- string.ords.should == [<span style="color:#00D">84</span>, <span style="color:#00D">104</span>, <span style="color:#00D">101</span>, <span style="color:#00D">111</span>, <span style="color:#00D">114</span>, <span style="color:#00D">101</span>, <span style="color:#00D">116</span>, <span style="color:#00D">105</span>, <span style="color:#00D">115</span>, <span style="color:#00D">99</span>, <span style="color:#00D">104</span>, <span style="color:#00D">101</span>, <span style="color:#00D">32</span>, <span style="color:#00D">73</span>, <span style="color:#00D">110</span>, <span style="color:#00D">102</span>, <span style="color:#00D">111</span>, <span style="color:#00D">114</span>, <span style="color:#00D">109</span>, <span style="color:#00D">97</span>, <span style="color:#00D">116</span>, <span style="color:#00D">105</span>, <span style="color:#00D">107</span>, <span style="color:#00D">32</span>, <span style="color:#00D">105</span>, <span style="color:#00D">115</span>, <span style="color:#00D">116</span>, <span style="color:#00D">32</span>, <span style="color:#00D">118</span>, <span style="color:#00D">111</span>, <span style="color:#00D">108</span>, <span style="color:#00D">108</span>, <span style="color:#00D">32</span>, <span style="color:#00D">103</span>, <span style="color:#00D">101</span>, <span style="color:#00D">105</span>, <span style="color:#00D">108</span>, <span style="color:#00D">33</span>]
1524
- <span style="color:#080;font-weight:bold">end</span>
1525
-
1526
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#constantize</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1527
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return the constant with that name</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1528
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object</span><span style="color:#710">'</span></span>.constantize.should == <span style="color:#036;font-weight:bold">Object</span>
1529
- <span style="color:#080;font-weight:bold">end</span>
1530
-
1531
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should also work for nested constants</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1532
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Zucker::VERSION</span><span style="color:#710">'</span></span>.constantize.should == <span style="color:#036;font-weight:bold">Zucker</span>::<span style="color:#036;font-weight:bold">VERSION</span>
1533
- <span style="color:#080;font-weight:bold">end</span>
1534
-
1535
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should throw name error if constant does not exist (and no parameter is given)</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1536
- proc <span style="color:#080;font-weight:bold">do</span>
1537
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span style="color:#710">'</span></span>.constantize
1538
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">NameError</span>
1539
- <span style="color:#080;font-weight:bold">end</span>
1540
-
1541
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should call the block (and not raise an error) if constant does not exist and block given</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1542
- proc <span style="color:#080;font-weight:bold">do</span>
1543
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span style="color:#710">'</span></span>.constantize <span style="color:#080;font-weight:bold">do</span> |string|
1544
- <span style="color:#036;font-weight:bold">Default</span> = [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>]
1545
- <span style="color:#080;font-weight:bold">end</span>.should == [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>]
1546
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception <span style="color:#036;font-weight:bold">NameError</span>
1547
- <span style="color:#080;font-weight:bold">end</span>
1548
-
1549
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should return the second parameter (and not raise an error) if constant does not exist and parameter given</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1550
- proc <span style="color:#080;font-weight:bold">do</span>
1551
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span style="color:#710">'</span></span>.constantize(<span style="color:#036;font-weight:bold">Array</span>).should == <span style="color:#036;font-weight:bold">Array</span>
1552
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception <span style="color:#036;font-weight:bold">NameError</span>
1553
- <span style="color:#080;font-weight:bold">end</span>
1554
- <span style="color:#080;font-weight:bold">end</span>
1555
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1556
- </div>
1557
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_string_source">(<a href="javascript:show('string_source')">show</a>)</span>
1558
- <div class="source" id="string_source"><div class="CodeRay">
1559
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1560
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">String</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1561
-
1562
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">String</span>
1563
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">-</span>(rem)
1564
- gsub( <span style="color:#036;font-weight:bold">Regexp</span> === rem ? rem : rem.to_s, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span> )
1565
- <span style="color:#080;font-weight:bold">end</span>
1566
-
1567
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">^</span>(pos)
1568
- pos = pos.to_i
1569
- <span style="color:#080;font-weight:bold">if</span> pos &gt;= <span style="color:#00D">0</span>
1570
- <span style="color:#069">self</span>[pos..<span style="color:#00D">-1</span>]
1571
- <span style="color:#080;font-weight:bold">else</span>
1572
- <span style="color:#069">self</span>[<span style="color:#00D">0</span>...pos]
1573
- <span style="color:#080;font-weight:bold">end</span>
1574
- <span style="color:#080;font-weight:bold">end</span>
1575
-
1576
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">lchomp</span>(arg = <span style="color:#d70">$/</span>)
1577
- reverse.chomp(arg).reverse
1578
- <span style="color:#080;font-weight:bold">end</span>
1579
-
1580
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">lchomp!</span>(arg = <span style="color:#d70">$/</span>)
1581
- reverse.chomp!(arg).reverse
1582
- <span style="color:#080;font-weight:bold">end</span>
1583
-
1584
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">ords</span>
1585
- unpack <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">C*</span><span style="color:#710">'</span></span>
1586
- <span style="color:#777"># bytes.to_a</span>
1587
- <span style="color:#080;font-weight:bold">end</span>
1588
-
1589
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">constantize</span>(default_value = <span style="color:#069">nil</span>) <span style="color:#777"># always uses global scope as in AS... is this good?</span>
1590
- get_constant = lambda{
1591
- <span style="color:#069">self</span>.split(<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">::</span><span style="color:#404">/</span></span>).inject( <span style="color:#036;font-weight:bold">Object</span> ){ |base_constant, current_constant|
1592
- base_constant.const_get current_constant
1593
- }
1594
- }
1595
-
1596
- <span style="color:#080;font-weight:bold">if</span> !default_value &amp;&amp; !block_given?
1597
- get_constant.call
1598
- <span style="color:#080;font-weight:bold">else</span>
1599
- <span style="color:#080;font-weight:bold">begin</span>
1600
- get_constant.call
1601
- <span style="color:#080;font-weight:bold">rescue</span> <span style="color:#036;font-weight:bold">NameError</span>
1602
- <span style="color:#080;font-weight:bold">if</span> block_given?
1603
- <span style="color:#080;font-weight:bold">yield</span> <span style="color:#069">self</span>
1604
- <span style="color:#080;font-weight:bold">else</span>
1605
- default_value
1606
- <span style="color:#080;font-weight:bold">end</span>
1607
- <span style="color:#080;font-weight:bold">end</span>
1608
- <span style="color:#080;font-weight:bold">end</span>
1609
- <span style="color:#080;font-weight:bold">end</span>
1610
- <span style="color:#080;font-weight:bold">end</span>
1611
-
1612
- <span style="color:#777"># J-_-L</span></pre></div>
1613
- </div>
1614
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/string">github wiki</a></td></tr></table> <h3 title="require 'zucker/unary'" id="unary">unary</h3>
1615
- <table class="cube_table"
1616
- id="unary_cube"
1617
- title="require 'zucker/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Sometimes, you do not care if you get a <code>String</code> or <code>Symbol</code> as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators <code>String#-@</code> and <code>Symbol#+@</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#+@</h5><div class="usage source" style="display:block"><div class="CodeRay">
1618
- <div class="code"><pre>+<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">was_string</span><span style="color:#710">'</span></span> <span style="color:#777"># =&gt; 'was_string'</span></pre></div>
1619
- </div>
1620
- </div><h5>String#-@</h5><div class="usage source" style="display:block"><div class="CodeRay">
1621
- <div class="code"><pre>-<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">was_string</span><span style="color:#710">'</span></span> <span style="color:#777"># =&gt; :was_string</span></pre></div>
1622
- </div>
1623
- </div><h5>Symbol#+@</h5><div class="usage source" style="display:block"><div class="CodeRay">
1624
- <div class="code"><pre>+<span style="color:#A60">:was_symbol</span> <span style="color:#777"># =&gt; 'was_symbol'</span></pre></div>
1625
- </div>
1626
- </div><h5>Symbol#-@</h5><div class="usage source" style="display:block"><div class="CodeRay">
1627
- <div class="code"><pre>-<span style="color:#A60">:was_symbol</span> <span style="color:#777"># =&gt; :was_symbol</span></pre></div>
1628
- </div>
1629
- </div></td></tr><tr><th>Information</th> <td>Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)</td></tr><tr><th>Specification</th> <td> <span id="show_unary_spec">(<a href="javascript:show('unary_spec')">show</a>)</span>
1630
- <div class="source" id="unary_spec"><div class="CodeRay">
1631
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/unary</span><span style="color:#710">'</span></span>
1632
-
1633
- describe <span style="color:#036;font-weight:bold">Symbol</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">#+@</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1634
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should convert to_s</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1635
- +(<span style="color:#A60">:matz</span>) == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">matz</span><span style="color:#710">'</span></span> <span style="color:#777"># () 1.8 bug</span>
1636
- <span style="color:#080;font-weight:bold">end</span>
1637
- <span style="color:#080;font-weight:bold">end</span>
1638
-
1639
- describe <span style="color:#036;font-weight:bold">Symbol</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">#-@</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1640
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should do nothing</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1641
- -<span style="color:#A60">:matz</span> == <span style="color:#A60">:matz</span>
1642
- <span style="color:#080;font-weight:bold">end</span>
1643
- <span style="color:#080;font-weight:bold">end</span>
1644
-
1645
- describe <span style="color:#036;font-weight:bold">String</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">#+@</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1646
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should do nothing</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1647
- +<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">matz</span><span style="color:#710">'</span></span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">matz</span><span style="color:#710">'</span></span>
1648
- <span style="color:#080;font-weight:bold">end</span>
1649
- <span style="color:#080;font-weight:bold">end</span>
1650
-
1651
- describe <span style="color:#036;font-weight:bold">String</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">#-@</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1652
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should convert to_sym</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1653
- -<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">matz</span><span style="color:#710">'</span></span> == <span style="color:#A60">:matz</span>
1654
- <span style="color:#080;font-weight:bold">end</span>
1655
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1656
- </div>
1657
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_unary_source">(<a href="javascript:show('unary_source')">show</a>)</span>
1658
- <div class="source" id="unary_source"><div class="CodeRay">
1659
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1660
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Unary</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1661
-
1662
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">String</span>
1663
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">+@</span>
1664
- <span style="color:#069">self</span>
1665
- <span style="color:#080;font-weight:bold">end</span>
1666
-
1667
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">-@</span>
1668
- to_sym
1669
- <span style="color:#080;font-weight:bold">end</span>
1670
- <span style="color:#080;font-weight:bold">end</span>
1671
-
1672
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Symbol</span>
1673
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">+@</span>
1674
- to_s
1675
- <span style="color:#080;font-weight:bold">end</span>
1676
-
1677
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">-@</span>
1678
- <span style="color:#069">self</span>
1679
- <span style="color:#080;font-weight:bold">end</span>
1680
- <span style="color:#080;font-weight:bold">end</span>
1681
-
1682
- <span style="color:#777"># J-_-L</span></pre></div>
1683
- </div>
1684
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (<code>+:literal</code> not possible)</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/unary">github wiki</a></td></tr></table> <h3 title="require 'zucker/union'" id="union">union</h3>
1685
- <table class="cube_table"
1686
- id="union_cube"
1687
- title="require 'zucker/union'"> <tr><th>Summary</th> <td>Easy creation of <code>Regexp.union</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#|, String#|</h5><div class="usage source" style="display:block"><div class="CodeRay">
1688
- <div class="code"><pre><span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Ruby</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span> | <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">test</span><span style="color:#404">/</span><span style="color:#C2C">i</span></span> | <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">cheat</span><span style="color:#710">&quot;</span></span>
1689
- <span style="color:#777"># creates a Regexp similar to:</span>
1690
- <span style="color:#777"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
1691
- </pre></div>
1692
- </div>
1693
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_union_spec">(<a href="javascript:show('union_spec')">show</a>)</span>
1694
- <div class="source" id="union_spec"><div class="CodeRay">
1695
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/union</span><span style="color:#710">'</span></span>
1696
-
1697
- shared_examples_for <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">Regexp.union operator</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
1698
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should create an Regexp.union of both operands</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
1699
- (<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Ruby</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span> | <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">test</span><span style="color:#404">/</span><span style="color:#C2C">i</span></span> | <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">cheat</span><span style="color:#710">&quot;</span></span>).should ==
1700
- <span style="color:#036;font-weight:bold">Regexp</span>.union( <span style="color:#036;font-weight:bold">Regexp</span>.union( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">Ruby</span><span style="color:#D20">\d</span><span style="color:#404">/</span></span>, <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">test</span><span style="color:#404">/</span><span style="color:#C2C">i</span></span> ), <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">cheat</span><span style="color:#710">&quot;</span></span> )
1701
- <span style="color:#080;font-weight:bold">end</span>
1702
- <span style="color:#080;font-weight:bold">end</span>
1703
-
1704
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Regexp#|</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1705
- it_should_behave_like <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Regexp.union operator</span><span style="color:#710">'</span></span>
1706
- <span style="color:#080;font-weight:bold">end</span>
1707
-
1708
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">String#|</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1709
- it_should_behave_like <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Regexp.union operator</span><span style="color:#710">'</span></span>
1710
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1711
- </div>
1712
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_union_source">(<a href="javascript:show('union_source')">show</a>)</span>
1713
- <div class="source" id="union_source"><div class="CodeRay">
1714
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1715
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Union</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1716
-
1717
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Regexp</span>
1718
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">|</span>(arg)
1719
- <span style="color:#036;font-weight:bold">Regexp</span>.union <span style="color:#069">self</span>, arg.is_a?(<span style="color:#036;font-weight:bold">Regexp</span>) ? arg : arg.to_s
1720
- <span style="color:#080;font-weight:bold">end</span>
1721
- <span style="color:#080;font-weight:bold">end</span>
1722
-
1723
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">String</span>
1724
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">|</span>(arg)
1725
- <span style="color:#036;font-weight:bold">Regexp</span>.union <span style="color:#069">self</span>, arg.is_a?(<span style="color:#036;font-weight:bold">Regexp</span>) ? arg : arg.to_s
1726
- <span style="color:#080;font-weight:bold">end</span>
1727
- <span style="color:#080;font-weight:bold">end</span>
1728
-
1729
- <span style="color:#777"># J-_-L</span></pre></div>
1730
- </div>
1731
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/union">github wiki</a></td></tr></table>
1732
- </div>
1733
- <h2 title="require 'zucker/object'" id="object">Cubes[object]</h2>
1734
-
1735
- <div class="cubes">
1736
- <h3 title="require 'zucker/blank'" id="blank">blank</h3>
1737
- <table class="cube_table"
1738
- id="blank_cube"
1739
- title="require 'zucker/blank'"> <tr><th>Summary</th> <td>Does pretty the same as in ActiveSupport (Every object can be asked if it is blank or present).</td></tr><tr><th>Why?</th> <td>It's too familiar ;)</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#blank?</h5><div class="usage source" style="display:block"><div class="CodeRay">
1740
- <div class="code"><pre>[<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">'an object'.blank? # =&gt; false</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">nil.present? # =&gt; false</span><span style="color:#710">&quot;</span></span>]</pre></div>
1741
- </div>
1742
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_blank_spec">(<a href="javascript:show('blank_spec')">show</a>)</span>
1743
- <div class="source" id="blank_spec"><div class="CodeRay">
1744
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/blank</span><span style="color:#710">'</span></span>
1745
-
1746
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#blank?</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1747
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should be blank for blank values</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1748
- blank_values = [ <span style="color:#069">nil</span>, <span style="color:#069">false</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> </span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20"> </span><span style="color:#b0b">\n</span><span style="color:#b0b">\t</span><span style="color:#D20"> </span><span style="color:#b0b">\r</span><span style="color:#D20"> </span><span style="color:#710">&quot;</span></span>, [], {}, <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#404">/</span></span> ]
1749
-
1750
- blank_values.each{ |blank|
1751
- blank.blank?.should == <span style="color:#069">true</span>
1752
- }
1753
- <span style="color:#080;font-weight:bold">end</span>
1754
-
1755
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should not be blank for non blank values</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1756
- present_values = [ <span style="color:#036;font-weight:bold">Object</span>.new, <span style="color:#069">true</span>, <span style="color:#00D">0</span>, <span style="color:#00D">1</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>, [<span style="color:#069">nil</span>], { <span style="color:#069">nil</span> =&gt; <span style="color:#069">nil</span> } ]
1757
-
1758
- present_values.each{ |present|
1759
- present.blank?.should == <span style="color:#069">false</span>
1760
- }
1761
- <span style="color:#080;font-weight:bold">end</span>
1762
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1763
- </div>
1764
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_blank_source">(<a href="javascript:show('blank_source')">show</a>)</span>
1765
- <div class="source" id="blank_source"><div class="CodeRay">
1766
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1767
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Blank</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1768
-
1769
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Object</span>
1770
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">blank?</span>
1771
- <span style="color:#080;font-weight:bold">if</span> respond_to? <span style="color:#A60">:empty?</span> <span style="color:#080;font-weight:bold">then</span> empty? <span style="color:#080;font-weight:bold">else</span> !<span style="color:#069">self</span> <span style="color:#080;font-weight:bold">end</span>
1772
- <span style="color:#080;font-weight:bold">end</span>
1773
-
1774
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">present?</span>
1775
- !blank?
1776
- <span style="color:#080;font-weight:bold">end</span>
1777
- <span style="color:#080;font-weight:bold">end</span>
1778
-
1779
-
1780
- { <span style="color:#777"># what to do # for which classes</span>
1781
- lambda{ <span style="color:#069">true</span> } =&gt; [<span style="color:#036;font-weight:bold">FalseClass</span>, <span style="color:#036;font-weight:bold">NilClass</span>],
1782
- lambda{ <span style="color:#069">false</span> } =&gt; [<span style="color:#036;font-weight:bold">TrueClass</span>, <span style="color:#036;font-weight:bold">Numeric</span>],
1783
- lambda{ empty? } =&gt; [<span style="color:#036;font-weight:bold">Array</span>, <span style="color:#036;font-weight:bold">Hash</span>],
1784
- lambda{ <span style="color:#069">self</span> !~ <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\S</span><span style="color:#404">/</span></span> } =&gt; [<span style="color:#036;font-weight:bold">String</span>],
1785
- lambda{ <span style="color:#069">self</span> == <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#404">/</span></span> } =&gt; [<span style="color:#036;font-weight:bold">Regexp</span>],
1786
-
1787
- }.each{ |action, klass_array|
1788
- klass_array.each{ |klass|
1789
- klass.send <span style="color:#A60">:define_method</span>, <span style="color:#A60">:blank?</span>, &amp;action
1790
- }
1791
- }
1792
-
1793
- <span style="color:#777"># J-_-L</span></pre></div>
1794
- </div>
1795
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/blank">github wiki</a></td></tr></table> <h3 title="require 'zucker/mcopy'" id="mcopy">mcopy</h3>
1796
- <table class="cube_table"
1797
- id="mcopy_cube"
1798
- title="require 'zucker/mcopy'"> <tr><th>Summary</th> <td>Adds <code>Object#mcopy</code> to create a deep copy using <code>Marshal</code>.</td></tr><tr><th>Why?</th> <td>Every Ruby book describes, you can do <code>Marshal.load Marshal.dump object</code> to create a deep copy... But who needs this verbose syntax in everyday coding?</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#mcopy</h5><div class="usage source" style="display:block"><div class="CodeRay">
1799
- <div class="code"><pre>a = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w[</span><span style="color:#D20">hello world</span><span style="color:#710">]</span></span>
1800
- b = a.mcopy
1801
- </pre></div>
1802
- </div>
1803
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_spec">(<a href="javascript:show('mcopy_spec')">show</a>)</span>
1804
- <div class="source" id="mcopy_spec"><div class="CodeRay">
1805
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/mcopy</span><span style="color:#710">'</span></span>
1806
-
1807
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#mcopy</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1808
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">create a (deep) copy via marshalling</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1809
- a = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w[</span><span style="color:#D20">hello world</span><span style="color:#710">]</span></span>
1810
- b = a.mcopy
1811
- b.should == a
1812
-
1813
- b[<span style="color:#00D">0</span>][<span style="color:#00D">1</span>,<span style="color:#00D">1</span>] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>
1814
- b.should_not == a
1815
- <span style="color:#080;font-weight:bold">end</span>
1816
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1817
- </div>
1818
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_mcopy_source">(<a href="javascript:show('mcopy_source')">show</a>)</span>
1819
- <div class="source" id="mcopy_source"><div class="CodeRay">
1820
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1821
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Mcopy</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1822
-
1823
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Object</span>
1824
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">mcopy</span>
1825
- <span style="color:#036;font-weight:bold">Marshal</span>.load <span style="color:#036;font-weight:bold">Marshal</span>.dump <span style="color:#069">self</span>
1826
- <span style="color:#080;font-weight:bold">end</span>
1827
- <span style="color:#080;font-weight:bold">end</span>
1828
-
1829
- <span style="color:#777"># J-_-L</span></pre></div>
1830
- </div>
1831
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/mcopy">github wiki</a></td></tr></table> <h3 title="require 'zucker/not'" id="not">not</h3>
1832
- <table class="cube_table"
1833
- id="not_cube"
1834
- title="require 'zucker/not'"> <tr><th>Summary</th> <td><code>not</code> returns an object on which all methods are redirected to it's receiver object, but returns the inverted boolean.</td></tr><tr><th>Why?</th> <td>Better readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#not</h5><div class="usage source" style="display:block"><div class="CodeRay">
1835
- <div class="code"><pre>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].not.empty? <span style="color:#777"># true</span>
1836
- </pre></div>
1837
- </div>
1838
- </div></td></tr><tr><th>Information</th> <td>See <a href="http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html">this article by Jay Field</a> for more information.</td></tr><tr><th>Specification</th> <td> <span id="show_not_spec">(<a href="javascript:show('not_spec')">show</a>)</span>
1839
- <div class="source" id="not_spec"><div class="CodeRay">
1840
- <div class="code"><pre><span style="color:#080;font-weight:bold">if</span> <span style="color:#080;font-weight:bold">defined?</span> <span style="color:#036;font-weight:bold">BasicObject</span> <span style="color:#777"># 1.9 only</span>
1841
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/not</span><span style="color:#710">'</span></span>
1842
-
1843
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#not</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1844
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should return an object on which all methods are redirected to it's receiver object, but return an inverted boolean</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
1845
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].not.empty?.should == <span style="color:#069">true</span>
1846
- <span style="color:#069">nil</span>.not.nil?.should == <span style="color:#069">false</span>
1847
- <span style="color:#080;font-weight:bold">end</span>
1848
- <span style="color:#080;font-weight:bold">end</span>
1849
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1850
- </div>
1851
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_not_source">(<a href="javascript:show('not_source')">show</a>)</span>
1852
- <div class="source" id="not_source"><div class="CodeRay">
1853
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1854
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Not</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1855
-
1856
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Object</span>
1857
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">not</span>
1858
- <span style="color:#036;font-weight:bold">NotClass</span>.new <span style="color:#069">self</span>
1859
- <span style="color:#080;font-weight:bold">end</span>
1860
-
1861
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">NotClass</span> &lt; <span style="color:#036;font-weight:bold">BasicObject</span>
1862
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">initialize</span>(receiver)
1863
- <span style="color:#33B">@receiver</span> = receiver
1864
- <span style="color:#080;font-weight:bold">end</span>
1865
-
1866
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">method_missing</span>(m, *args, &amp;block)
1867
- <span style="color:#080;font-weight:bold">not</span> <span style="color:#33B">@receiver</span>.public_send( m, *args, &amp;block )
1868
- <span style="color:#080;font-weight:bold">end</span>
1869
- <span style="color:#080;font-weight:bold">end</span>
1870
- <span style="color:#080;font-weight:bold">end</span>
1871
-
1872
- <span style="color:#777"># J-_-L</span></pre></div>
1873
- </div>
1874
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/not">github wiki</a></td></tr></table>
1875
- </div>
1876
- <h2 title="require 'zucker/to_proc'" id="to_proc">Cubes[to_proc]</h2>
1877
-
1878
- <div class="cubes">
1879
- <h3 title="require 'zucker/array_to_proc'" id="array_to_proc">array_to_proc</h3>
1880
- <table class="cube_table"
1881
- id="array_to_proc_cube"
1882
- title="require 'zucker/array_to_proc'"> <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><div class="usage source" style="display:block"><div class="CodeRay">
1883
- <div class="code"><pre>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map &amp;[<span style="color:#A60">:*</span>, <span style="color:#00D">5</span>] <span style="color:#777"># =&gt; [5, 10, 15, 20]</span>
1884
- <span style="color:#777"># you can also chain them, if the first parameter is an Array</span>
1885
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map &amp;[[<span style="color:#A60">:to_s</span>, <span style="color:#00D">2</span>],[<span style="color:#A60">:+</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">b</span><span style="color:#710">'</span></span>]] <span style="color:#777"># =&gt; [&quot;1b&quot;, &quot;10b&quot;, &quot;11b&quot;, &quot;100b&quot;]</span>
1886
- </pre></div>
1887
- </div>
1888
- </div></td></tr><tr><th>Information</th> <td>Inspired by <a href="http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby">this article</a>.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.<br/>Chaining inspired by <a href="http://github.com/eregon/Classes/blob/master/to_proc.rb">eregon</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_array_to_proc_spec">(<a href="javascript:show('array_to_proc_spec')">show</a>)</span>
1889
- <div class="source" id="array_to_proc_spec"><div class="CodeRay">
1890
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/array_to_proc</span><span style="color:#710">'</span></span>
1891
-
1892
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Array#to_proc</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1893
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should call the method of the first symbol, using the remaining elements as paramaters</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1894
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map( &amp;[<span style="color:#A60">:to_s</span>, <span style="color:#00D">2</span>] ).should == [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">10</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">11</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">100</span><span style="color:#710">&quot;</span></span>]
1895
- <span style="color:#080;font-weight:bold">end</span>
1896
-
1897
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should convert each element to a proc and chain it, if the first parameter is an array</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
1898
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map( &amp;[[<span style="color:#A60">:*</span>,<span style="color:#00D">2</span>],[<span style="color:#A60">:+</span>,<span style="color:#00D">4</span>]] ).should == [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map{|i| i*<span style="color:#00D">2</span> + <span style="color:#00D">4</span> }
1899
- <span style="color:#080;font-weight:bold">end</span>
1900
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1901
- </div>
1902
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_array_to_proc_source">(<a href="javascript:show('array_to_proc_source')">show</a>)</span>
1903
- <div class="source" id="array_to_proc_source"><div class="CodeRay">
1904
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1905
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">ArrayToProc</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1906
-
1907
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Array</span>
1908
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_proc</span>
1909
- <span style="color:#036;font-weight:bold">Proc</span>.new{ |obj|
1910
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">self</span>.first.is_a? <span style="color:#036;font-weight:bold">Array</span>
1911
- <span style="color:#069">self</span>.inject(obj){ |result, nested_array|
1912
- nested_array.to_proc.call result
1913
- }
1914
- <span style="color:#080;font-weight:bold">else</span>
1915
- obj.send(*<span style="color:#069">self</span>)
1916
- <span style="color:#080;font-weight:bold">end</span>
1917
- }
1918
- <span style="color:#080;font-weight:bold">end</span>
1919
- <span style="color:#080;font-weight:bold">end</span>
1920
-
1921
- <span style="color:#777"># J-_-L</span></pre></div>
1922
- </div>
1923
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/array_to_proc">github wiki</a></td></tr></table> <h3 title="require 'zucker/class_to_proc'" id="class_to_proc">class_to_proc</h3>
1924
- <table class="cube_table"
1925
- id="class_to_proc_cube"
1926
- title="require 'zucker/class_to_proc'"> <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><div class="usage source" style="display:block"><div class="CodeRay">
1927
- <div class="code"><pre>[ [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>],[<span style="color:#00D">3</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>,<span style="color:#00D">7</span>,<span style="color:#00D">3</span>] ].map(&amp;<span style="color:#036;font-weight:bold">Set</span>) <span style="color:#777"># =&gt; [ Set[1,2], Set[5,6,7,3] ]</span></pre></div>
1928
- </div>
1929
- </div></td></tr><tr><th>Information</th> <td>Inspired by Ruby Facets.<br/>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_class_to_proc_spec">(<a href="javascript:show('class_to_proc_spec')">show</a>)</span>
1930
- <div class="source" id="class_to_proc_spec"><div class="CodeRay">
1931
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/class_to_proc</span><span style="color:#710">'</span></span>
1932
- require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">set</span><span style="color:#710">'</span></span>
1933
-
1934
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Class#to_proc</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1935
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create new instances of the class</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1936
- [ [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>],[<span style="color:#00D">3</span>,<span style="color:#00D">5</span>,<span style="color:#00D">6</span>,<span style="color:#00D">7</span>,<span style="color:#00D">3</span>] ].map(&amp;<span style="color:#036;font-weight:bold">Set</span>).should == [ <span style="color:#036;font-weight:bold">Set</span>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>], <span style="color:#036;font-weight:bold">Set</span>[<span style="color:#00D">5</span>,<span style="color:#00D">6</span>,<span style="color:#00D">7</span>,<span style="color:#00D">3</span>] ]
1937
- <span style="color:#080;font-weight:bold">end</span>
1938
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1939
- </div>
1940
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_class_to_proc_source">(<a href="javascript:show('class_to_proc_source')">show</a>)</span>
1941
- <div class="source" id="class_to_proc_source"><div class="CodeRay">
1942
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1943
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">ClassToProc</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1944
-
1945
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Class</span>
1946
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_proc</span>
1947
- <span style="color:#036;font-weight:bold">Proc</span>.new <span style="color:#080;font-weight:bold">do</span> |*args|
1948
- <span style="color:#069">self</span>.new(*args)
1949
- <span style="color:#080;font-weight:bold">end</span>
1950
- <span style="color:#080;font-weight:bold">end</span>
1951
- <span style="color:#080;font-weight:bold">end</span>
1952
-
1953
- <span style="color:#777"># J-_-L</span></pre></div>
1954
- </div>
1955
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/class_to_proc">github wiki</a></td></tr></table> <h3 title="require 'zucker/hash_to_proc'" id="hash_to_proc">hash_to_proc</h3>
1956
- <table class="cube_table"
1957
- id="hash_to_proc_cube"
1958
- title="require 'zucker/hash_to_proc'"> <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><div class="usage source" style="display:block"><div class="CodeRay">
1959
- <div class="code"><pre>[<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map(&amp;{
1960
- <span style="color:#00D">2</span> =&gt; lambda {|e| e + <span style="color:#00D">1000</span>},
1961
- <span style="color:#00D">4</span> =&gt; <span style="color:#A60">:to_s</span>,
1962
- }) <span style="color:#777"># =&gt; [1, 1002, 3, '4']</span></pre></div>
1963
- </div>
1964
- </div></td></tr><tr><th>Information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_hash_to_proc_spec">(<a href="javascript:show('hash_to_proc_spec')">show</a>)</span>
1965
- <div class="source" id="hash_to_proc_spec"><div class="CodeRay">
1966
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/hash_to_proc</span><span style="color:#710">'</span></span>
1967
-
1968
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hash#to_proc</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1969
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should run the proc given in the value for a key in the hash</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
1970
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>,<span style="color:#00D">4</span>].map(&amp;{
1971
- <span style="color:#00D">4</span> =&gt; <span style="color:#A60">:to_s</span>,
1972
- <span style="color:#777"># 3 =&gt; [:to_s, 2] # &quot;11&quot; =&gt; if array2proc is used</span>
1973
- <span style="color:#00D">2</span> =&gt; lambda {|e| e + <span style="color:#00D">1000</span>}
1974
- }).should == [<span style="color:#00D">1</span>, <span style="color:#00D">1002</span>, <span style="color:#00D">3</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">4</span><span style="color:#710">&quot;</span></span>]
1975
- <span style="color:#080;font-weight:bold">end</span>
1976
- <span style="color:#080;font-weight:bold">end</span></pre></div>
1977
- </div>
1978
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_hash_to_proc_source">(<a href="javascript:show('hash_to_proc_source')">show</a>)</span>
1979
- <div class="source" id="hash_to_proc_source"><div class="CodeRay">
1980
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
1981
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">HashToProc</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
1982
-
1983
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Hash</span>
1984
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_proc</span>
1985
- <span style="color:#036;font-weight:bold">Proc</span>.new{ |obj|
1986
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">self</span>.member? obj
1987
- <span style="color:#069">self</span>[obj].to_proc.call obj
1988
- <span style="color:#080;font-weight:bold">else</span>
1989
- obj
1990
- <span style="color:#080;font-weight:bold">end</span>
1991
- }
1992
- <span style="color:#080;font-weight:bold">end</span>
1993
- <span style="color:#080;font-weight:bold">end</span>
1994
-
1995
- <span style="color:#777"># J-_-L</span></pre></div>
1996
- </div>
1997
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/hash_to_proc">github wiki</a></td></tr></table> <h3 title="require 'zucker/regexp_to_proc'" id="regexp_to_proc">regexp_to_proc</h3>
1998
- <table class="cube_table"
1999
- id="regexp_to_proc_cube"
2000
- title="require 'zucker/regexp_to_proc'"> <tr><th>Summary</th> <td>Use <code>&amp;/regex/</code> to match it against strings.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#to_proc</h5><div class="usage source" style="display:block"><div class="CodeRay">
2001
- <div class="code"><pre><span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">just another string array</span><span style="color:#710">|</span></span>.map &amp;<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">[jy]</span><span style="color:#404">/</span></span> <span style="color:#777"># =&gt; [&quot;j&quot;, nil, nil, &quot;y&quot;]</span>
2002
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">just another string array</span><span style="color:#710">|</span></span>.select &amp;<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">[jy]</span><span style="color:#404">/</span></span> <span style="color:#777"># =&gt; [&quot;just&quot;, &quot;array&quot;]</span>
2003
- </pre></div>
2004
- </div>
2005
- </div></td></tr><tr><th>Information</th> <td>More about <a href="http://rbjl.net/29-become-a-proc-star">to_proc</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_regexp_to_proc_spec">(<a href="javascript:show('regexp_to_proc_spec')">show</a>)</span>
2006
- <div class="source" id="regexp_to_proc_spec"><div class="CodeRay">
2007
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/regexp_to_proc</span><span style="color:#710">'</span></span>
2008
-
2009
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Regexp#to_proc</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2010
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should match the regex</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2011
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">just another string array</span><span style="color:#710">|</span></span>.map( &amp;<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">[jy]</span><span style="color:#404">/</span></span>).should == [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">j</span><span style="color:#710">&quot;</span></span>, <span style="color:#069">nil</span>, <span style="color:#069">nil</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">y</span><span style="color:#710">&quot;</span></span>]
2012
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%w|</span><span style="color:#D20">just another string array</span><span style="color:#710">|</span></span>.select( &amp;<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">[jy]</span><span style="color:#404">/</span></span>).should == [<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">just</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">array</span><span style="color:#710">&quot;</span></span>]
2013
- <span style="color:#080;font-weight:bold">end</span>
2014
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2015
- </div>
2016
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_regexp_to_proc_source">(<a href="javascript:show('regexp_to_proc_source')">show</a>)</span>
2017
- <div class="source" id="regexp_to_proc_source"><div class="CodeRay">
2018
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2019
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">RegexpToProc</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2020
-
2021
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Regexp</span>
2022
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">to_proc</span>
2023
- proc <span style="color:#080;font-weight:bold">do</span> |e|
2024
- e.to_s[<span style="color:#069">self</span>]
2025
- <span style="color:#080;font-weight:bold">end</span>
2026
- <span style="color:#080;font-weight:bold">end</span>
2027
- <span style="color:#080;font-weight:bold">end</span>
2028
-
2029
- <span style="color:#777"># J-_-L</span></pre></div>
2030
- </div>
2031
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/regexp_to_proc">github wiki</a></td></tr></table>
2032
- </div>
2033
- <h2 title="require 'zucker/shortcuts'" id="shortcuts">Cubes[shortcuts]</h2>
2034
-
2035
- <div class="cubes">
2036
- <h3 title="require 'zucker/alias_for'" id="alias_for">alias_for</h3>
2037
- <table class="cube_table"
2038
- id="alias_for_cube"
2039
- title="require 'zucker/alias_for'"> <tr><th>Summary</th> <td>A different way to create aliases: Reversed order and you can pass multiple alias names.</td></tr><tr><th>Why?</th> <td>It's clearer in which order to put the arguments.</td></tr><tr><th>Methods/Usage</th> <td><h5>alias_for, aliases_for</h5><div class="usage source" style="display:block"><div class="CodeRay">
2040
- <div class="code"><pre>alias_for <span style="color:#A60">:methods</span>, <span style="color:#A60">:ms</span> <span style="color:#777"># creates an alias for the method :methods with the name ms</span>
2041
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Enumerable</span>
2042
- aliases_for <span style="color:#A60">:zip</span>, <span style="color:#A60">:with</span>, <span style="color:#A60">:%</span>
2043
- <span style="color:#080;font-weight:bold">end</span> <span style="color:#777"># creates multiple aliases</span>
2044
- </pre></div>
2045
- </div>
2046
- </div><h5>Module#method_alias_for, Module#method_aliases_for</h5><div class="usage source" style="display:block"><div class="CodeRay">
2047
- <div class="code"><pre><span style="color:#777"># same as alias_for, but using Module#alias_method instead of alias</span>
2048
- </pre></div>
2049
- </div>
2050
- </div></td></tr><tr><th>Information</th> <td>Thanks to Kristian Mandrup for the idea and alias method specs.</td></tr><tr><th>Specification</th> <td> <span id="show_alias_for_spec">(<a href="javascript:show('alias_for_spec')">show</a>)</span>
2051
- <div class="source" id="alias_for_spec"><div class="CodeRay">
2052
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/alias_for</span><span style="color:#710">'</span></span>
2053
-
2054
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">alias_for</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2055
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create an alias for global methods</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2056
- <span style="color:#777"># rspec bug? </span>
2057
- <span style="color:#777"># def m1</span>
2058
- <span style="color:#777"># 1</span>
2059
- <span style="color:#777"># end</span>
2060
- <span style="color:#777"># alias_for :m1, :a1</span>
2061
- <span style="color:#777">#</span>
2062
- <span style="color:#777"># proc do</span>
2063
- <span style="color:#777"># a1.should == 1</span>
2064
- <span style="color:#777"># end.should_not raise_exception</span>
2065
- <span style="color:#080;font-weight:bold">end</span>
2066
-
2067
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create an alias for instance methods</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2068
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Array</span>
2069
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">m2</span>
2070
- <span style="color:#00D">2</span>
2071
- <span style="color:#080;font-weight:bold">end</span>
2072
- aliases_for <span style="color:#A60">:m2</span>, <span style="color:#A60">:a2</span>, <span style="color:#A60"><span style="color:#A60">:</span><span style="color:#630">'</span><span style="color:#A60">a-2</span><span style="color:#630">'</span></span>
2073
- <span style="color:#080;font-weight:bold">end</span>
2074
- proc <span style="color:#080;font-weight:bold">do</span>
2075
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].a2.should == <span style="color:#00D">2</span>
2076
- [<span style="color:#00D">1</span>,<span style="color:#00D">2</span>,<span style="color:#00D">3</span>].send(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a-2</span><span style="color:#710">'</span></span>).should == <span style="color:#00D">2</span>
2077
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
2078
- <span style="color:#080;font-weight:bold">end</span>
2079
-
2080
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create an alias for class (singleton) methods</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2081
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Array</span>
2082
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">Array</span>
2083
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">m3</span>
2084
- <span style="color:#00D">3</span>
2085
- <span style="color:#080;font-weight:bold">end</span>
2086
- alias_for <span style="color:#A60">:m3</span>, <span style="color:#A60">:a3</span>
2087
- <span style="color:#080;font-weight:bold">end</span>
2088
- <span style="color:#080;font-weight:bold">end</span>
2089
-
2090
- proc <span style="color:#080;font-weight:bold">do</span>
2091
- <span style="color:#036;font-weight:bold">Array</span>.a3.should == <span style="color:#00D">3</span>
2092
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
2093
- <span style="color:#080;font-weight:bold">end</span>
2094
-
2095
-
2096
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create aliases for the first argument with all other arguments</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2097
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Object</span>
2098
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">m4</span>
2099
- <span style="color:#00D">4</span>
2100
- <span style="color:#080;font-weight:bold">end</span>
2101
- alias_for <span style="color:#A60">:m4</span>, <span style="color:#A60">:ma</span>, <span style="color:#A60">:mb</span>, <span style="color:#A60">:mc</span>
2102
- <span style="color:#080;font-weight:bold">end</span>
2103
- proc <span style="color:#080;font-weight:bold">do</span>
2104
- <span style="color:#00D">1</span>.ma.should == <span style="color:#00D">4</span>
2105
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1</span><span style="color:#710">&quot;</span></span>.mb.should == <span style="color:#00D">4</span>
2106
- [<span style="color:#00D">1</span>].mc.should == <span style="color:#00D">4</span>
2107
- <span style="color:#080;font-weight:bold">end</span>.should_not raise_exception
2108
- <span style="color:#080;font-weight:bold">end</span>
2109
- <span style="color:#080;font-weight:bold">end</span>
2110
-
2111
- <span style="color:#777"># alias_method</span>
2112
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Blip</span>
2113
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">blip</span>
2114
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">blip</span><span style="color:#710">'</span></span>
2115
- <span style="color:#080;font-weight:bold">end</span>
2116
- alias_methods_for <span style="color:#A60">:blip</span>, <span style="color:#A60">:blap</span>, <span style="color:#A60">:blup</span>
2117
-
2118
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">self</span>
2119
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">self_blip</span>
2120
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">blip</span><span style="color:#710">'</span></span>
2121
- <span style="color:#080;font-weight:bold">end</span>
2122
- alias_methods_for <span style="color:#A60">:self_blip</span>, <span style="color:#A60">:self_blap</span>, <span style="color:#A60">:self_blup</span>
2123
- <span style="color:#080;font-weight:bold">end</span>
2124
- <span style="color:#080;font-weight:bold">end</span>
2125
-
2126
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Hello</span>
2127
- include <span style="color:#036;font-weight:bold">Blip</span>
2128
-
2129
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">hello</span>
2130
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">hello</span><span style="color:#710">'</span></span>
2131
- <span style="color:#080;font-weight:bold">end</span>
2132
- alias_methods_for <span style="color:#A60">:hello</span>, <span style="color:#A60">:hi</span>, <span style="color:#A60">:howdy</span>
2133
-
2134
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">self</span>
2135
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">self_hello</span>
2136
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">hello</span><span style="color:#710">'</span></span>
2137
- <span style="color:#080;font-weight:bold">end</span>
2138
- alias_methods_for <span style="color:#A60">:self_hello</span>, <span style="color:#A60">:self_hi</span>, <span style="color:#A60">:self_howdy</span>
2139
- <span style="color:#080;font-weight:bold">end</span>
2140
- <span style="color:#080;font-weight:bold">end</span>
2141
-
2142
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">alias_methods_for</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2143
- let(<span style="color:#A60">:h</span>) { <span style="color:#036;font-weight:bold">Hello</span>.new }
2144
-
2145
- context <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">module context</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2146
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should alias instance methods</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2147
- h.blap.should == h.blip
2148
- h.blup.should == h.blip
2149
- <span style="color:#080;font-weight:bold">end</span>
2150
-
2151
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should alias class methods</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2152
- <span style="color:#036;font-weight:bold">Blip</span>.self_blap.should == <span style="color:#036;font-weight:bold">Blip</span>.self_blip
2153
- <span style="color:#036;font-weight:bold">Blip</span>.self_blup.should == <span style="color:#036;font-weight:bold">Blip</span>.self_blip
2154
- <span style="color:#080;font-weight:bold">end</span>
2155
- <span style="color:#080;font-weight:bold">end</span>
2156
-
2157
- context <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">class context</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2158
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should alias instance methods</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2159
- h.hi.should == h.hello
2160
- h.howdy.should == h.hello
2161
- <span style="color:#080;font-weight:bold">end</span>
2162
-
2163
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should alias class methods</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2164
- <span style="color:#036;font-weight:bold">Hello</span>.self_hi.should == <span style="color:#036;font-weight:bold">Hello</span>.self_hello
2165
- <span style="color:#036;font-weight:bold">Hello</span>.self_howdy.should == <span style="color:#036;font-weight:bold">Hello</span>.self_hello
2166
- <span style="color:#080;font-weight:bold">end</span>
2167
- <span style="color:#080;font-weight:bold">end</span>
2168
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2169
- </div>
2170
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_alias_for_source">(<a href="javascript:show('alias_for_source')">show</a>)</span>
2171
- <div class="source" id="alias_for_source"><div class="CodeRay">
2172
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2173
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">AliasFor</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2174
-
2175
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">alias_for</span>(m, *aliases)
2176
- aliases.each{ |a|
2177
- class_eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">alias :'</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span> a.to_s <span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">' :'</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span> m.to_s <span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">'</span><span style="color:#710">&quot;</span></span>
2178
- }
2179
- <span style="color:#080;font-weight:bold">end</span>
2180
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">aliases_for</span> <span style="color:#06B;font-weight:bold">alias_for</span>
2181
-
2182
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Module</span>
2183
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">alias_method_for</span>(m, *alias_methods)
2184
- alias_methods.each{ |a|
2185
- class_eval <span style="color:#080;font-weight:bold">do</span>
2186
- alias_method a.to_sym, m.to_sym
2187
- <span style="color:#080;font-weight:bold">end</span>
2188
- }
2189
- <span style="color:#080;font-weight:bold">end</span>
2190
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">alias_methods_for</span> <span style="color:#06B;font-weight:bold">alias_method_for</span>
2191
- <span style="color:#080;font-weight:bold">end</span>
2192
-
2193
- <span style="color:#777"># J-_-L</span></pre></div>
2194
- </div>
2195
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/alias_for">github wiki</a></td></tr></table> <h3 title="require 'zucker/aliases'" id="aliases">aliases</h3>
2196
- <table class="cube_table"
2197
- id="aliases_cube"
2198
- title="require 'zucker/aliases'"> <tr><th>Summary</th> <td>Some convenient aliases (and constants) for existing methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#is_an?</h5><div class="usage source" style="display:block"><div class="CodeRay">
2199
- <div class="code"><pre><span style="color:#036;font-weight:bold">Object</span>.is_a?</pre></div>
2200
- </div>
2201
- </div><h5>Enumerable#with</h5><div class="usage source" style="display:block"><div class="CodeRay">
2202
- <div class="code"><pre><span style="color:#036;font-weight:bold">Enumerable</span><span style="color:#777">#zip</span></pre></div>
2203
- </div>
2204
- </div><h5>Enumerable#%</h5><div class="usage source" style="display:block"><div class="CodeRay">
2205
- <div class="code"><pre><span style="color:#036;font-weight:bold">Enumerable</span><span style="color:#777">#zip</span></pre></div>
2206
- </div>
2207
- </div><h5>Array#**</h5><div class="usage source" style="display:block"><div class="CodeRay">
2208
- <div class="code"><pre><span style="color:#036;font-weight:bold">Array</span><span style="color:#777">#product</span></pre></div>
2209
- </div>
2210
- </div><h5>Hash#+</h5><div class="usage source" style="display:block"><div class="CodeRay">
2211
- <div class="code"><pre><span style="color:#036;font-weight:bold">Hash</span><span style="color:#777">#merge</span></pre></div>
2212
- </div>
2213
- </div><h5>Binding#[]</h5><div class="usage source" style="display:block"><div class="CodeRay">
2214
- <div class="code"><pre><span style="color:#036;font-weight:bold">Binding</span><span style="color:#777">#eval</span></pre></div>
2215
- </div>
2216
- </div><h5>File.filename</h5><div class="usage source" style="display:block"><div class="CodeRay">
2217
- <div class="code"><pre><span style="color:#036;font-weight:bold">File</span>.basename</pre></div>
2218
- </div>
2219
- </div><h5>Dir.join</h5><div class="usage source" style="display:block"><div class="CodeRay">
2220
- <div class="code"><pre><span style="color:#036;font-weight:bold">File</span>.join</pre></div>
2221
- </div>
2222
- </div><h5>Dir.split</h5><div class="usage source" style="display:block"><div class="CodeRay">
2223
- <div class="code"><pre><span style="color:#036;font-weight:bold">File</span>.split</pre></div>
2224
- </div>
2225
- </div><h5>Infinity</h5><div class="usage source" style="display:block"><div class="CodeRay">
2226
- <div class="code"><pre><span style="color:#60E">1.0</span> / <span style="color:#60E">0.0</span></pre></div>
2227
- </div>
2228
- </div><h5>NaN</h5><div class="usage source" style="display:block"><div class="CodeRay">
2229
- <div class="code"><pre><span style="color:#60E">0.0</span> / <span style="color:#60E">0.0</span></pre></div>
2230
- </div>
2231
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_aliases_spec">(<a href="javascript:show('aliases_spec')">show</a>)</span>
2232
- <div class="source" id="aliases_spec"><div class="CodeRay">
2233
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/aliases</span><span style="color:#710">'</span></span>
2234
-
2235
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">(aliases)</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2236
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should create these aliases</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2237
- <span style="color:#777"># see aliases.rb for aliase list</span>
2238
- <span style="color:#080;font-weight:bold">end</span>
2239
-
2240
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should define these constants</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2241
- <span style="color:#036;font-weight:bold">Infinity</span>.finite?.should == <span style="color:#069">false</span>
2242
- <span style="color:#036;font-weight:bold">NaN</span>.nan?.should == <span style="color:#069">true</span>
2243
- <span style="color:#080;font-weight:bold">end</span>
2244
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2245
- </div>
2246
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_aliases_source">(<a href="javascript:show('aliases_source')">show</a>)</span>
2247
- <div class="source" id="aliases_source"><div class="CodeRay">
2248
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2249
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Aliases</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2250
-
2251
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Object</span>
2252
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">is_an?</span> <span style="color:#06B;font-weight:bold">is_a?</span> <span style="color:#777"># thanks to utility_belt</span>
2253
- <span style="color:#080;font-weight:bold">end</span>
2254
-
2255
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Enumerable</span>
2256
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">with</span> <span style="color:#06B;font-weight:bold">zip</span>
2257
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">%</span> <span style="color:#06B;font-weight:bold">zip</span>
2258
- <span style="color:#080;font-weight:bold">end</span>
2259
-
2260
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Array</span>
2261
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">**</span> <span style="color:#06B;font-weight:bold">product</span>
2262
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">contains?</span> <span style="color:#06B;font-weight:bold">include?</span>
2263
- <span style="color:#080;font-weight:bold">end</span>
2264
-
2265
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">String</span>
2266
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">contains?</span> <span style="color:#06B;font-weight:bold">include?</span>
2267
- <span style="color:#080;font-weight:bold">end</span>
2268
-
2269
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Hash</span>
2270
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">+</span> <span style="color:#06B;font-weight:bold">merge</span>
2271
- <span style="color:#080;font-weight:bold">end</span>
2272
-
2273
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Binding</span>
2274
- <span style="color:#777">#alias [] eval</span>
2275
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">[]</span>(expr)
2276
- <span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>expr<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>
2277
- <span style="color:#080;font-weight:bold">end</span>
2278
- <span style="color:#080;font-weight:bold">end</span>
2279
-
2280
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">File</span>
2281
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">filename</span> <span style="color:#06B;font-weight:bold">basename</span> <span style="color:#777"># thanks rdp :)</span>
2282
- <span style="color:#080;font-weight:bold">end</span>
2283
-
2284
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">Dir</span>
2285
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">join</span>(*args)
2286
- <span style="color:#036;font-weight:bold">File</span>.join(*args)
2287
- <span style="color:#080;font-weight:bold">end</span>
2288
-
2289
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">split</span>(*args)
2290
- <span style="color:#036;font-weight:bold">File</span>.split(*args)
2291
- <span style="color:#080;font-weight:bold">end</span>
2292
- <span style="color:#080;font-weight:bold">end</span>
2293
-
2294
- <span style="color:#777"># constants - who would use these in real-world code for other things?</span>
2295
- <span style="color:#036;font-weight:bold">Infinity</span> = <span style="color:#60E">1.0</span> / <span style="color:#60E">0.0</span> <span style="color:#777"># or 2*Float::MAX or Float::INFINITY</span>
2296
- <span style="color:#036;font-weight:bold">NaN</span> = <span style="color:#60E">0.0</span> / <span style="color:#60E">0.0</span>
2297
-
2298
- <span style="color:#777"># J-_-L</span></pre></div>
2299
- </div>
2300
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/aliases">github wiki</a></td></tr></table> <h3 title="require 'zucker/ivars'" id="ivars">ivars</h3>
2301
- <table class="cube_table"
2302
- id="ivars_cube"
2303
- title="require 'zucker/ivars'"> <tr><th>Summary</th> <td>This method lets you easily assign lots of instance variables.</td></tr><tr><th>Why?</th> <td>Often, you have to write boilerplate code for assigning instance variables, 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><div class="usage source" style="display:block"><div class="CodeRay">
2304
- <div class="code"><pre><span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">a_method</span>(a = <span style="color:#00D">1</span>, b = <span style="color:#00D">2</span>)
2305
- instance_variables_from binding <span style="color:#777"># assigns @a and @b</span>
2306
-
2307
- params = {<span style="color:#A60">:c</span> =&gt; <span style="color:#00D">3</span>, <span style="color:#A60">:d</span> =&gt; <span style="color:#00D">4</span>}
2308
- instance_variables_from params <span style="color:#777"># # assigns @c and @d</span>
2309
- <span style="color:#080;font-weight:bold">end</span>
2310
- </pre></div>
2311
- </div>
2312
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_spec">(<a href="javascript:show('ivars_spec')">show</a>)</span>
2313
- <div class="source" id="ivars_spec"><div class="CodeRay">
2314
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/ivars</span><span style="color:#710">'</span></span>
2315
-
2316
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">instance_variables_from</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2317
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should tansform the given parameter to instance variables</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2318
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">a_method</span>(a = <span style="color:#00D">1</span>, b = <span style="color:#00D">2</span>)
2319
- instance_variables_from binding <span style="color:#777"># assigns @a and @b</span>
2320
-
2321
- params = {<span style="color:#A60">:c</span> =&gt; <span style="color:#00D">3</span>, <span style="color:#A60">:d</span> =&gt; <span style="color:#00D">4</span>}
2322
- ivars params <span style="color:#777"># # assigns @c and @d</span>
2323
- <span style="color:#080;font-weight:bold">end</span>
2324
-
2325
- a_method
2326
- <span style="color:#33B">@a</span>.should == <span style="color:#00D">1</span>
2327
- <span style="color:#33B">@b</span>.should == <span style="color:#00D">2</span>
2328
- <span style="color:#33B">@c</span>.should == <span style="color:#00D">3</span>
2329
- <span style="color:#33B">@d</span>.should == <span style="color:#00D">4</span>
2330
-
2331
- <span style="color:#080;font-weight:bold">end</span>
2332
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2333
- </div>
2334
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_ivars_source">(<a href="javascript:show('ivars_source')">show</a>)</span>
2335
- <div class="source" id="ivars_source"><div class="CodeRay">
2336
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2337
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Ivars</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2338
-
2339
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">instance_variables_from</span>(obj, *only)
2340
- iter =
2341
- <span style="color:#080;font-weight:bold">if</span> obj.is_a? <span style="color:#036;font-weight:bold">Binding</span>
2342
- obj.eval(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">local_variables</span><span style="color:#710">'</span></span>).map{|e| [obj.eval(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>e<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>), e] }
2343
- <span style="color:#080;font-weight:bold">elsif</span> obj.is_a? <span style="color:#036;font-weight:bold">Hash</span>
2344
- obj.map{|k,v| [v,k] }
2345
- <span style="color:#080;font-weight:bold">else</span>
2346
- <span style="color:#777"># elsif obj.is_a? Enumerable</span>
2347
- obj.each.with_index
2348
- <span style="color:#080;font-weight:bold">end</span>
2349
-
2350
- ret = []
2351
- iter.each{ |value, arg|
2352
- arg = arg.to_s
2353
- <span style="color:#080;font-weight:bold">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
2354
- arg = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">_</span><span style="color:#710">'</span></span> + arg <span style="color:#080;font-weight:bold">if</span> (<span style="color:#00D">48</span>..<span style="color:#00D">57</span>).member? arg.unpack(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">C</span><span style="color:#710">'</span></span>)[<span style="color:#00D">0</span>] <span style="color:#777"># 1.8+1.9</span>
2355
- ret &lt;&lt; ivar = <span style="color:#A60"><span style="color:#A60">:</span><span style="color:#630">&quot;</span><span style="color:#A60">@</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>arg<span style="font-weight:bold;color:#666">}</span></span><span style="color:#630">&quot;</span></span>
2356
- <span style="color:#069">self</span>.instance_variable_set ivar, value
2357
- <span style="color:#080;font-weight:bold">end</span>
2358
- }
2359
- ret
2360
- <span style="color:#080;font-weight:bold">end</span>
2361
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">ivars</span> <span style="color:#06B;font-weight:bold">instance_variables_from</span>
2362
-
2363
- <span style="color:#777"># J-_-L</span></pre></div>
2364
- </div>
2365
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/ivars">github wiki</a></td></tr></table> <h3 title="require 'zucker/square_brackets_for'" id="square_brackets_for">square_brackets_for</h3>
2366
- <table class="cube_table"
2367
- id="square_brackets_for_cube"
2368
- title="require 'zucker/square_brackets_for'"> <tr><th>Summary</th> <td>This helper methods defines <code>[]</code> and <code>[]=</code> for accesing an instance variable.</td></tr><tr><th>Methods/Usage</th> <td><h5>square_brackets_for</h5><div class="usage source" style="display:block"><div class="CodeRay">
2369
- <div class="code"><pre><span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Klass</span>
2370
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">initialize</span>
2371
- <span style="color:#33B">@var</span> = {
2372
- <span style="color:#A60">:a_key</span> =&gt; <span style="color:#00D">1</span>,
2373
- <span style="color:#A60">:another_one</span> =&gt; <span style="color:#00D">2</span>,
2374
- }
2375
- <span style="color:#080;font-weight:bold">end</span>
2376
-
2377
- square_brackets_for <span style="color:#A60">:var</span> <span style="color:#777"># creates [] and []=</span>
2378
- <span style="color:#777"># square_brackets_for :var, false # would create only []</span>
2379
- <span style="color:#080;font-weight:bold">end</span>
2380
-
2381
- a = <span style="color:#036;font-weight:bold">Klass</span>.new
2382
- a[<span style="color:#A60">:a_key</span>] <span style="color:#777"># =&gt; 1</span>
2383
- </pre></div>
2384
- </div>
2385
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_spec">(<a href="javascript:show('square_brackets_for_spec')">show</a>)</span>
2386
- <div class="source" id="square_brackets_for_spec"><div class="CodeRay">
2387
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/square_brackets_for</span><span style="color:#710">'</span></span>
2388
-
2389
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">square_brackets_for</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2390
-
2391
- before <span style="color:#080;font-weight:bold">do</span>
2392
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Klass</span>
2393
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">initialize</span>
2394
- <span style="color:#33B">@var</span> = {
2395
- <span style="color:#A60">:a_key</span> =&gt; <span style="color:#00D">1</span>,
2396
- <span style="color:#A60">:another_one</span> =&gt; <span style="color:#00D">2</span>,
2397
- }
2398
- <span style="color:#080;font-weight:bold">end</span>
2399
-
2400
- <span style="color:#33B">@eigenvar</span> = {
2401
- <span style="color:#A60">:a_key</span> =&gt; <span style="color:#00D">99</span>
2402
- }
2403
- <span style="color:#080;font-weight:bold">end</span>
2404
- <span style="color:#080;font-weight:bold">end</span>
2405
-
2406
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should define a [] getter (not a setter) for an instance var, if the second parameter is false</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2407
-
2408
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Klass</span>
2409
- square_brackets_for <span style="color:#A60">:var</span>, <span style="color:#069">nil</span>
2410
- <span style="color:#080;font-weight:bold">end</span>
2411
-
2412
- a = <span style="color:#036;font-weight:bold">Klass</span>.new
2413
- a[<span style="color:#A60">:a_key</span>].should == <span style="color:#00D">1</span>
2414
-
2415
- proc <span style="color:#080;font-weight:bold">do</span>
2416
- a[<span style="color:#A60">:this_is</span>] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">not possible</span><span style="color:#710">'</span></span>
2417
- <span style="color:#080;font-weight:bold">end</span>.should raise_exception <span style="color:#036;font-weight:bold">NoMethodError</span>
2418
- <span style="color:#080;font-weight:bold">end</span>
2419
-
2420
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should define [] and []= for accessing an instance variable</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2421
-
2422
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Klass</span>
2423
- square_brackets_for <span style="color:#A60">:var</span>
2424
- <span style="color:#080;font-weight:bold">end</span>
2425
-
2426
- a = <span style="color:#036;font-weight:bold">Klass</span>.new
2427
- a[<span style="color:#A60">:a_key</span>].should == <span style="color:#00D">1</span>
2428
-
2429
- a[<span style="color:#A60">:this_is</span>] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">useful</span><span style="color:#710">'</span></span>
2430
- a[<span style="color:#A60">:this_is</span>].should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">useful</span><span style="color:#710">'</span></span>
2431
- <span style="color:#080;font-weight:bold">end</span>
2432
-
2433
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should also work for class-instance variables</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2434
-
2435
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Klass</span>
2436
- <span style="color:#080;font-weight:bold">class</span> &lt;&lt; <span style="color:#B06;font-weight:bold">Klass</span>
2437
- square_brackets_for <span style="color:#A60">:eigenvar</span>
2438
- <span style="color:#080;font-weight:bold">end</span>
2439
- <span style="color:#080;font-weight:bold">end</span>
2440
-
2441
- <span style="color:#036;font-weight:bold">Klass</span>[<span style="color:#A60">:a_key</span>].should == <span style="color:#00D">99</span>
2442
- <span style="color:#080;font-weight:bold">end</span>
2443
-
2444
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2445
- </div>
2446
- </div> </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>
2447
- <div class="source" id="square_brackets_for_source"><div class="CodeRay">
2448
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2449
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">SquareBracketsFor</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2450
-
2451
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Module</span>
2452
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">square_brackets_for</span>(ivar, assignment = <span style="color:#069">true</span>)
2453
- <span style="color:#777"># undef [] if respond_to? :[]</span>
2454
- <span style="color:#777"># undef []= if respond_to? :[]=</span>
2455
-
2456
- define_method <span style="color:#A60">:[]</span> <span style="color:#080;font-weight:bold">do</span> |key|
2457
- (instance_variable_get <span style="color:#A60"><span style="color:#A60">:</span><span style="color:#630">&quot;</span><span style="color:#A60">@</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>ivar<span style="font-weight:bold;color:#666">}</span></span><span style="color:#630">&quot;</span></span>)[key]
2458
- <span style="color:#080;font-weight:bold">end</span>
2459
-
2460
- <span style="color:#080;font-weight:bold">if</span> assignment
2461
- define_method <span style="color:#A60">:[]=</span> <span style="color:#080;font-weight:bold">do</span> |key, value|
2462
- (instance_variable_get <span style="color:#A60"><span style="color:#A60">:</span><span style="color:#630">&quot;</span><span style="color:#A60">@</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>ivar<span style="font-weight:bold;color:#666">}</span></span><span style="color:#630">&quot;</span></span>)[key] = value
2463
- <span style="color:#080;font-weight:bold">end</span>
2464
- <span style="color:#080;font-weight:bold">end</span>
2465
- <span style="color:#080;font-weight:bold">end</span>
2466
- <span style="color:#080;font-weight:bold">end</span>
2467
-
2468
- <span style="color:#777"># J-_-L</span></pre></div>
2469
- </div>
2470
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/square_brackets_for">github wiki</a></td></tr></table>
2471
- </div>
2472
- <h2 title="require 'zucker/debug'" id="debug">Cubes[debug]</h2>
2473
- <p class="text">(not included by <code>require 'zucker/default'</code>)</p>
2474
- <div class="cubes">
2475
- <h3 title="require 'zucker/binding'" id="binding">binding</h3>
2476
- <table class="cube_table"
2477
- id="binding_cube"
2478
- title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds <code>Binding#vars</code>. It gives you information about the current environment variables.</td></tr><tr><th>Methods/Usage</th> <td><h5>Binding#variables, Binding#vars</h5><div class="usage source" style="display:block"><div class="CodeRay">
2479
- <div class="code"><pre><span style="color:#777"># shows your current environment's variables, irb example:</span>
2480
- &gt;&gt; a=<span style="color:#00D">3</span>
2481
- =&gt; <span style="color:#00D">3</span>
2482
- &gt;&gt; binding.vars
2483
- =&gt; <span style="color:#777">#&lt;Binding:0x94c4b50&gt;</span>
2484
- local variables
2485
- - <span style="color:#606">a</span>: <span style="color:#00D">3</span>
2486
- - <span style="color:#606">_</span>: <span style="color:#777">#&lt;Binding:0x94c4b50&gt;</span>
2487
- (instance variables)
2488
- - none
2489
- <span style="color:#069">self</span>
2490
- - main
2491
- block_given?
2492
- - <span style="color:#069">false</span>
2493
- </pre></div>
2494
- </div>
2495
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_binding_spec">(<a href="javascript:show('binding_spec')">show</a>)</span>
2496
- <div class="source" id="binding_spec"><div class="CodeRay">
2497
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/binding</span><span style="color:#710">'</span></span>
2498
-
2499
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Binding#variables</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2500
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should output information about the current environment</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
2501
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2502
- </div>
2503
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_binding_source">(<a href="javascript:show('binding_source')">show</a>)</span>
2504
- <div class="source" id="binding_source"><div class="CodeRay">
2505
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2506
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Binding</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2507
-
2508
- <span style="color:#080;font-weight:bold">unless</span> <span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#069">RUBY_ENGINE</span>) &amp;&amp; <span style="color:#069">RUBY_ENGINE</span> == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">rbx</span><span style="color:#710">'</span></span>
2509
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Binding</span>
2510
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">variables</span>
2511
- put_vars = lambda { |array|
2512
- <span style="color:#080;font-weight:bold">if</span> array.empty?
2513
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> - none</span><span style="color:#710">'</span></span>
2514
- <span style="color:#080;font-weight:bold">else</span>
2515
- array.map{|e|
2516
- val = (<span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>e<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>).inspect
2517
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20"> - </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>e<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span> val <span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>
2518
- }.join <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#b0b">\n</span><span style="color:#710">&quot;</span></span>
2519
- <span style="color:#080;font-weight:bold">end</span>
2520
- }
2521
-
2522
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span><span style="color:#069">self</span>.to_s<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">
2523
- local variables
2524
- </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span> put_vars[ <span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">local_variables</span><span style="color:#710">'</span></span> ] <span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">
2525
- (instance variables)
2526
- </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span> put_vars[ <span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">instance_variables</span><span style="color:#710">'</span></span> ] <span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">
2527
- self
2528
- - </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span><span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">self</span><span style="color:#710">'</span></span><span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">
2529
- block_given?
2530
- - </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span><span style="color:#069">self</span>.eval <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">block_given?</span><span style="color:#710">'</span></span><span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>
2531
-
2532
- <span style="color:#080;font-weight:bold">end</span>
2533
-
2534
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">vars</span> <span style="color:#06B;font-weight:bold">variables</span>
2535
- <span style="color:#080;font-weight:bold">end</span>
2536
- <span style="color:#080;font-weight:bold">end</span>
2537
-
2538
- <span style="color:#777"># J-_-L</span></pre></div>
2539
- </div>
2540
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (no rbx support)</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/binding">github wiki</a></td></tr></table> <h3 title="require 'zucker/cc'" id="cc">cc</h3>
2541
- <table class="cube_table"
2542
- id="cc_cube"
2543
- title="require 'zucker/cc'"> <tr><th>Summary</th> <td><code>c</code> outputs the current method callstack.</td></tr><tr><th>Methods/Usage</th> <td><h5>c, cc</h5><div class="usage source" style="display:block"><div class="CodeRay">
2544
- <div class="code"><pre>c <span style="color:#777"># shows the method callstack</span>
2545
- c <span style="color:#069">true</span> <span style="color:#777"># do not hide irb/ripl callstack</span>
2546
- </pre></div>
2547
- </div>
2548
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_cc_spec">(<a href="javascript:show('cc_spec')">show</a>)</span>
2549
- <div class="source" id="cc_spec"><div class="CodeRay">
2550
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/cc</span><span style="color:#710">'</span></span>
2551
-
2552
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">c</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2553
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should output the method callstack</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
2554
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2555
- </div>
2556
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_cc_source">(<a href="javascript:show('cc_source')">show</a>)</span>
2557
- <div class="source" id="cc_source"><div class="CodeRay">
2558
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2559
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Cc</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2560
-
2561
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
2562
- private
2563
-
2564
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">c</span>(show_irb = <span style="color:#069">false</span>)
2565
- method_stack = caller.reverse.map{ |m|
2566
- m.rindex( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:</span><span style="color:#D20">\d</span><span style="color:#808">+(:in `(.*)')?$</span><span style="color:#404">/</span></span> )
2567
- <span style="color:#d70">$2</span>
2568
- }.compact
2569
-
2570
- <span style="color:#080;font-weight:bold">if</span> !show_irb
2571
- <span style="color:#080;font-weight:bold">if</span> a = method_stack.index( <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">irb_binding</span><span style="color:#710">'</span></span> )
2572
- method_stack = [ method_stack[<span style="color:#00D">0</span>], <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">(irb)</span><span style="color:#710">'</span></span>, *method_stack[a+<span style="color:#00D">1</span>..<span style="color:#00D">-1</span>] ]
2573
- <span style="color:#080;font-weight:bold">elsif</span> a = method_stack[<span style="color:#00D">1</span>..<span style="color:#00D">-1</span>].index( <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">&lt;main&gt;</span><span style="color:#710">'</span></span> )
2574
- method_stack = [ method_stack[<span style="color:#00D">0</span>], <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">(ripl)</span><span style="color:#710">'</span></span>, *method_stack[a+<span style="color:#00D">2</span>..<span style="color:#00D">-1</span>] ]
2575
- <span style="color:#080;font-weight:bold">end</span>
2576
- <span style="color:#080;font-weight:bold">end</span>
2577
-
2578
- <span style="color:#777"># puts method_stack.map.with_index{ |m, i|</span>
2579
- method_stack.each_with_index{ |m, i|
2580
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20"> </span><span style="color:#710">&quot;</span></span>*i + m
2581
- }
2582
- <span style="color:#069">nil</span>
2583
- <span style="color:#080;font-weight:bold">end</span>
2584
-
2585
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">cc</span> <span style="color:#06B;font-weight:bold">c</span>
2586
- <span style="color:#080;font-weight:bold">end</span>
2587
-
2588
- <span style="color:#777"># J-_-L</span></pre></div>
2589
- </div>
2590
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/cc">github wiki</a></td></tr></table> <h3 title="require 'zucker/dd'" id="dd">dd</h3>
2591
- <table class="cube_table"
2592
- id="dd_cube"
2593
- title="require 'zucker/dd'"> <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, Object#dd</h5><div class="usage source" style="display:block"><div class="CodeRay">
2594
- <div class="code"><pre>some.d.methods.d.noone.d.knows.d
2595
- <span style="color:#777"># ...outputs 4 lines with the inspected objects</span>
2596
- <span style="color:#777"># =&gt; (result)</span>
2597
-
2598
- <span style="color:#00D">21</span>+<span style="color:#036;font-weight:bold">Math</span>.sin(<span style="color:#00D">42</span>).d
2599
- <span style="color:#777"># outputs -0.916521547915634</span>
2600
- <span style="color:#777"># =&gt; 20.0834784520844</span>
2601
-
2602
- name = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Earth</span><span style="color:#710">'</span></span>
2603
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Hello </span><span style="color:#710">'</span></span> + name.d{|e| <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">The length is: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>e.size<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>}
2604
- <span style="color:#777"># outputs &quot;The length is: 5&quot;</span>
2605
- <span style="color:#777"># =&gt; 'Hello Earth'</span>
2606
- </pre></div>
2607
- </div>
2608
- </div></td></tr><tr><th>Information</th> <td>This is inspired by the <a href="http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions"><code>.tap</code> method</a>.<br/>There is also <code>tap</code> cube with more <code>tap</code> methods.</td></tr><tr><th>Specification</th> <td> <span id="show_dd_spec">(<a href="javascript:show('dd_spec')">show</a>)</span>
2609
- <div class="source" id="dd_spec"><div class="CodeRay">
2610
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/dd</span><span style="color:#710">'</span></span>
2611
-
2612
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#d</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2613
-
2614
- let <span style="color:#A60">:a</span> <span style="color:#080;font-weight:bold">do</span>
2615
- [ <span style="color:#00D">1</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1</span><span style="color:#710">&quot;</span></span>, <span style="color:#00D">2</span>..<span style="color:#00D">5</span>, [], {<span style="color:#A60">:hallo</span> =&gt; <span style="color:#A60">:du</span>}, <span style="color:#069">nil</span>, <span style="color:#069">true</span> ]
2616
- <span style="color:#080;font-weight:bold">end</span>
2617
-
2618
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should not change the object's value</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2619
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2620
- a.each{ |e|
2621
- (e.d).should == e
2622
- }
2623
-
2624
- a.each{ |e|
2625
- (e.d{|value| <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">This is a: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>value<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>}).should == e
2626
- }
2627
- <span style="color:#080;font-weight:bold">end</span>
2628
- <span style="color:#080;font-weight:bold">end</span>
2629
-
2630
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should puts .inspect if no block is given (and not change the object's value)</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2631
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2632
- a[<span style="color:#00D">0</span>].d
2633
- a[<span style="color:#00D">1</span>].d
2634
- a[<span style="color:#00D">6</span>].d
2635
- <span style="color:#080;font-weight:bold">end</span>.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%{</span><span style="color:#D20">1</span><span style="color:#b0b">\n</span><span style="color:#D20">&quot;1&quot;</span><span style="color:#b0b">\n</span><span style="color:#D20">true</span><span style="color:#b0b">\n</span><span style="color:#710">}</span></span>
2636
- <span style="color:#080;font-weight:bold">end</span>
2637
-
2638
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should puts the block if it is given (and not change the object's value)</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2639
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2640
- a[<span style="color:#00D">0</span>].d{|value| <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">This is a: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>value<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>}
2641
- a[<span style="color:#00D">1</span>].d{|value| <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">This is a: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>value<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>}
2642
- a[<span style="color:#00D">6</span>].d{|value| <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">This is a: </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>value<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span>}
2643
- <span style="color:#080;font-weight:bold">end</span>.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">%{</span><span style="color:#D20">This is a: 1</span><span style="color:#b0b">\n</span><span style="color:#D20">This is a: 1</span><span style="color:#b0b">\n</span><span style="color:#D20">This is a: true</span><span style="color:#b0b">\n</span><span style="color:#710">}</span></span>
2644
- <span style="color:#080;font-weight:bold">end</span>
2645
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2646
- </div>
2647
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_dd_source">(<a href="javascript:show('dd_source')">show</a>)</span>
2648
- <div class="source" id="dd_source"><div class="CodeRay">
2649
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2650
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Dd</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2651
-
2652
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
2653
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">d</span>(*args, &amp;block)
2654
- <span style="color:#080;font-weight:bold">if</span> args.empty?
2655
- tap{
2656
- <span style="color:#080;font-weight:bold">if</span> block_given?
2657
- puts <span style="color:#080;font-weight:bold">yield</span> <span style="color:#069">self</span>
2658
- <span style="color:#080;font-weight:bold">else</span>
2659
- puts <span style="color:#069">self</span>.inspect
2660
- <span style="color:#080;font-weight:bold">end</span>
2661
- }
2662
- <span style="color:#080;font-weight:bold">else</span>
2663
- raise <span style="color:#036;font-weight:bold">ArgumentError</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">.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 style="color:#710">&quot;</span></span>
2664
- <span style="color:#777"># eval ...</span>
2665
- <span style="color:#080;font-weight:bold">end</span>
2666
- <span style="color:#080;font-weight:bold">end</span>
2667
-
2668
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">dd</span> <span style="color:#06B;font-weight:bold">d</span>
2669
- <span style="color:#080;font-weight:bold">end</span>
2670
-
2671
- <span style="color:#777"># J-_-L</span></pre></div>
2672
- </div>
2673
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/dd">github wiki</a></td></tr></table> <h3 title="require 'zucker/mm'" id="mm">mm</h3>
2674
- <table class="cube_table"
2675
- id="mm_cube"
2676
- title="require 'zucker/mm'"> <tr><th>Summary</th> <td><code>m</code> displays an ordered public method list.</td></tr><tr><th>Why?</th> <td>See one object's methods without those rarely used inherited methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#m, Object#mm, Object#method_list</h5><div class="usage source" style="display:block"><div class="CodeRay">
2677
- <div class="code"><pre>&gt;&gt; <span style="color:#00D">5</span>.mm <span style="color:#00D">2</span> <span style="color:#777"># outputs the list (2 levels deep), for example:</span>
2678
- <span style="color:#777"># = Fixnum</span>
2679
- <span style="color:#777"># to_s -@ + - * / div % modulo divmod fdiv ** abs magnitude == === &lt;=&gt; &gt; &gt;= &lt; &lt;= ~ &amp; | ^ [] &lt;&lt; &gt;&gt; to_f size zero? odd? even? succ</span>
2680
- <span style="color:#777"># = Integer</span>
2681
- <span style="color:#777"># integer? odd? even? upto downto times succ next pred chr ord to_i to_int floor ceil truncate round gcd lcm gcdlcm numerator denominator to_r rationalize</span>
2682
- </pre></div>
2683
- </div>
2684
- </div></td></tr><tr><th>Information</th> <td>See <a href="http://rbjl.net/31-the-multi-mega-method-list">this article</a> for more information.</td></tr><tr><th>Specification</th> <td> <span id="show_mm_spec">(<a href="javascript:show('mm_spec')">show</a>)</span>
2685
- <div class="source" id="mm_spec"><div class="CodeRay">
2686
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/mm</span><span style="color:#710">'</span></span>
2687
-
2688
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Object#mm</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2689
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display an ordered method list</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
2690
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2691
- </div>
2692
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_mm_source">(<a href="javascript:show('mm_source')">show</a>)</span>
2693
- <div class="source" id="mm_source"><div class="CodeRay">
2694
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2695
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Mm</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2696
-
2697
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
2698
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">m</span>(levels = <span style="color:#00D">1</span>)
2699
- <span style="color:#777"># output helper</span>
2700
- mputs = proc{ |e| puts e.map(&amp;<span style="color:#A60">:to_s</span>)*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> </span><span style="color:#710">'</span></span> }
2701
-
2702
- <span style="color:#777"># how to get results</span>
2703
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">self</span>.is_a? <span style="color:#036;font-weight:bold">Module</span>
2704
- klass, method_function = <span style="color:#069">self</span>, <span style="color:#A60">:public_methods</span>
2705
- <span style="color:#080;font-weight:bold">else</span>
2706
- klass, method_function = <span style="color:#069">self</span>.class, <span style="color:#A60">:public_instance_methods</span>
2707
- <span style="color:#080;font-weight:bold">end</span>
2708
-
2709
- <span style="color:#777"># eigenclass</span>
2710
- eigen = <span style="color:#069">self</span>.singleton_methods
2711
- <span style="color:#080;font-weight:bold">if</span> !eigen.empty?
2712
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">= Eigenclass</span><span style="color:#710">'</span></span> <span style="color:#777"># sorry for not being up to date, I just love the word</span>
2713
- mputs[ <span style="color:#069">self</span>.singleton_methods ]
2714
- <span style="color:#080;font-weight:bold">end</span>
2715
-
2716
- <span style="color:#777"># display</span>
2717
- levels.times{ |level|
2718
- <span style="color:#080;font-weight:bold">if</span> cur = klass.ancestors[level]
2719
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">= </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>cur<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">&quot;</span></span> <span style="color:#777"># put class name</span>
2720
- mputs[ cur.send method_function, <span style="color:#069">false</span> ] <span style="color:#777"># put methods of the class</span>
2721
- <span style="color:#080;font-weight:bold">else</span>
2722
- <span style="color:#080;font-weight:bold">break</span>
2723
- <span style="color:#080;font-weight:bold">end</span>
2724
- }
2725
-
2726
- <span style="color:#069">nil</span>
2727
- <span style="color:#080;font-weight:bold">end</span>
2728
-
2729
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">mm</span> <span style="color:#06B;font-weight:bold">m</span>
2730
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">method_list</span> <span style="color:#06B;font-weight:bold">m</span>
2731
- <span style="color:#080;font-weight:bold">end</span>
2732
-
2733
- <span style="color:#777"># J-_-L</span></pre></div>
2734
- </div>
2735
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/mm">github wiki</a></td></tr></table> <h3 title="require 'zucker/oo'" id="oo">oo</h3>
2736
- <table class="cube_table"
2737
- id="oo_cube"
2738
- title="require 'zucker/oo'"> <tr><th>Summary</th> <td>The <code>o</code> method outputs the current method, line, file and label (if given).</td></tr><tr><th>Methods/Usage</th> <td><h5>o, oo</h5><div class="usage source" style="display:block"><div class="CodeRay">
2739
- <div class="code"><pre>o <span style="color:#777"># e.g: Reached method `irb_binding', line 1 of file (irb)</span>
2740
- <span style="color:#606">o</span>:<span style="color:#036;font-weight:bold">Label</span> <span style="color:#777"># e.g: Label: reached method `abc', line 7 of file (irb)</span>
2741
- </pre></div>
2742
- </div>
2743
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_oo_spec">(<a href="javascript:show('oo_spec')">show</a>)</span>
2744
- <div class="source" id="oo_spec"><div class="CodeRay">
2745
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/oo</span><span style="color:#710">'</span></span>
2746
-
2747
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">o</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2748
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should output the current line, file, method and label (if given)</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>; <span style="color:#080;font-weight:bold">end</span>
2749
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2750
- </div>
2751
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_oo_source">(<a href="javascript:show('oo_source')">show</a>)</span>
2752
- <div class="source" id="oo_source"><div class="CodeRay">
2753
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2754
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Oo</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2755
-
2756
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
2757
- private
2758
-
2759
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">o</span>(desc = <span style="color:#069">nil</span>)
2760
- caller[<span style="color:#00D">0</span>].rindex( <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">:(</span><span style="color:#D20">\d</span><span style="color:#808">+)(:in (`.*'))?$</span><span style="color:#404">/</span></span> )
2761
- m = <span style="color:#d70">$3</span> ? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">method </span><span style="color:#710">#</span><span style="color:#d70">$3</span><span style="color:#D20">, </span><span style="color:#710">&quot;</span></span> : <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#710">&quot;</span></span>
2762
- d = desc ? <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>desc<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">: r</span><span style="color:#710">&quot;</span></span> : <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">R</span><span style="color:#710">'</span></span>
2763
-
2764
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>d<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">eached </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">#{</span>m<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">line </span><span style="color:#710">#</span><span style="color:#d70">$1</span><span style="color:#D20"> of file </span><span style="color:#710">#</span><span style="color:#d70">$`</span><span style="color:#710">&quot;</span></span>
2765
- <span style="color:#777"># [$`, $1.to_i, $3.to_sym, desc]</span>
2766
- <span style="color:#080;font-weight:bold">end</span>
2767
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">oo</span> <span style="color:#06B;font-weight:bold">o</span>
2768
- <span style="color:#080;font-weight:bold">end</span>
2769
-
2770
- <span style="color:#777"># J-_-L</span></pre></div>
2771
- </div>
2772
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/oo">github wiki</a></td></tr></table> <h3 title="require 'zucker/qq'" id="qq">qq</h3>
2773
- <table class="cube_table"
2774
- id="qq_cube"
2775
- title="require 'zucker/qq'"> <tr><th>Summary</th> <td>The <code>q</code> method does the same like <code>p</code>, but outputs multiple arguments on the same line.</td></tr><tr><th>Methods/Usage</th> <td><h5>q, qq</h5><div class="usage source" style="display:block"><div class="CodeRay">
2776
- <div class="code"><pre>q <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">zucker</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#710">'</span></span>, {<span style="color:#00D">6</span>=&gt;<span style="color:#00D">7</span>}, <span style="color:#00D">5</span>, <span style="color:#60E">2.3</span>, <span style="color:#A60">:o</span> <span style="color:#777"># outputs &quot;zucker&quot; &quot;&quot; {6=&gt;7} 5 2.3 :o</span>
2777
- </pre></div>
2778
- </div>
2779
- </div></td></tr><tr><th>Information</th> <td>Inspired by <a href="http://github.com/rdp/sane/blob/master/lib/sane/pps.rb">rdp</a>.</td></tr><tr><th>Specification</th> <td> <span id="show_qq_spec">(<a href="javascript:show('qq_spec')">show</a>)</span>
2780
- <div class="source" id="qq_spec"><div class="CodeRay">
2781
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/qq</span><span style="color:#710">'</span></span>
2782
-
2783
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">q</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2784
-
2785
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should output the same as p for a single arg</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2786
- capture_stdout{p <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">some object</span><span style="color:#404">/</span></span>}.should ==
2787
- capture_stdout{q <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">some object</span><span style="color:#404">/</span></span>}
2788
- <span style="color:#080;font-weight:bold">end</span>
2789
-
2790
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">should output the same as p but for multiple args one one line, values separated by two spaces</span><span style="color:#710">&quot;</span></span> <span style="color:#080;font-weight:bold">do</span>
2791
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2792
- q <span style="color:#00D">1</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1</span><span style="color:#710">&quot;</span></span>, <span style="color:#00D">2</span>..<span style="color:#00D">5</span>, [], {<span style="color:#A60">:hallo</span> =&gt; <span style="color:#A60">:du</span>}, <span style="color:#069">nil</span>, <span style="color:#069">true</span>
2793
- <span style="color:#080;font-weight:bold">end</span>.chop.should ==
2794
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2795
- p <span style="color:#00D">1</span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1</span><span style="color:#710">&quot;</span></span>, <span style="color:#00D">2</span>..<span style="color:#00D">5</span>, [], {<span style="color:#A60">:hallo</span> =&gt; <span style="color:#A60">:du</span>}, <span style="color:#069">nil</span>, <span style="color:#069">true</span>
2796
- <span style="color:#080;font-weight:bold">end</span>.chop.gsub( <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#b0b">\n</span><span style="color:#710">&quot;</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> </span><span style="color:#710">'</span></span> )
2797
- <span style="color:#080;font-weight:bold">end</span>
2798
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2799
- </div>
2800
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_qq_source">(<a href="javascript:show('qq_source')">show</a>)</span>
2801
- <div class="source" id="qq_source"><div class="CodeRay">
2802
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2803
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">Qq</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2804
-
2805
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Kernel</span>
2806
- private
2807
-
2808
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">q</span>(*args)
2809
- puts args.map( &amp;<span style="color:#A60">:inspect</span> )*<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> </span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">unless</span> args.empty?
2810
- <span style="color:#080;font-weight:bold">end</span>
2811
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">qq</span> <span style="color:#06B;font-weight:bold">q</span>
2812
- <span style="color:#080;font-weight:bold">end</span>
2813
-
2814
- <span style="color:#777"># J-_-L</span></pre></div>
2815
- </div>
2816
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/qq">github wiki</a></td></tr></table> <h3 title="require 'zucker/regexp_visualize'" id="regexp_visualize">regexp_visualize</h3>
2817
- <table class="cube_table"
2818
- id="regexp_visualize_cube"
2819
- title="require 'zucker/regexp_visualize'"> <tr><th>Summary</th> <td>Test your regex against a string, handy for the command line.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#visualize, Regexp#vis</h5><div class="usage source" style="display:block"><div class="CodeRay">
2820
- <div class="code"><pre><span style="color:#777"># regex = /\b([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})\b/i</span>
2821
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">I do not contain an email address.</span><span style="color:#710">'</span></span>
2822
- <span style="color:#777"># no match</span>
2823
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">I contain an email address: mail@example.com</span><span style="color:#710">'</span></span>
2824
- <span style="color:#777"># I contain an email address: &gt;mail@example.com&lt; </span>
2825
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, <span style="color:#00D">1</span>
2826
- <span style="color:#777"># 1: &gt;mail&lt; @example.com</span>
2827
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, <span style="color:#00D">2</span>
2828
- <span style="color:#777"># 2: mail@ &gt;example.com&lt; </span>
2829
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, <span style="color:#00D">3</span>
2830
- <span style="color:#777"># 3: no match</span>
2831
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, [<span style="color:#00D">0</span>,<span style="color:#00D">1</span>,<span style="color:#00D">2</span>]
2832
- <span style="color:#777"># 0: &gt;mail@example.com&lt; </span>
2833
- <span style="color:#777"># 1: &gt;mail&lt; @example.com</span>
2834
- <span style="color:#777"># 2: mail@ &gt;example.com&lt; </span>
2835
- </pre></div>
2836
- </div>
2837
- </div></td></tr><tr><th>Specification</th> <td> <span id="show_regexp_visualize_spec">(<a href="javascript:show('regexp_visualize_spec')">show</a>)</span>
2838
- <div class="source" id="regexp_visualize_spec"><div class="CodeRay">
2839
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker/regexp_visualize</span><span style="color:#710">'</span></span>
2840
-
2841
- describe <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Regexp#visualize</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2842
- let <span style="color:#A60">:regex</span> <span style="color:#080;font-weight:bold">do</span> <span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#D20">\b</span><span style="color:#808">([A-Z0-9._%+-]+)@([A-Z0-9.-]+</span><span style="color:#D20">\.</span><span style="color:#808">[A-Z]{2,4})</span><span style="color:#D20">\b</span><span style="color:#404">/</span><span style="color:#C2C">i</span></span> <span style="color:#080;font-weight:bold">end</span> <span style="color:#777"># regex from regular-expressions.info/email.html</span>
2843
-
2844
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display a string where &gt;and&lt; mark the found regex</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2845
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2846
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">I do not contain an email address.</span><span style="color:#710">'</span></span>
2847
- <span style="color:#080;font-weight:bold">end</span>.chomp.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">no match</span><span style="color:#710">'</span></span>
2848
- <span style="color:#080;font-weight:bold">end</span>
2849
-
2850
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display &quot;no match&quot; if the regex could not be matched</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2851
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2852
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">I contain an email address: mail@example.com</span><span style="color:#710">'</span></span>
2853
- <span style="color:#080;font-weight:bold">end</span>.chomp.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">I contain an email address: &gt;mail@example.com&lt; </span><span style="color:#710">'</span></span>
2854
- <span style="color:#080;font-weight:bold">end</span>
2855
-
2856
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display a string where &gt;and&lt; mark the found regex group if group identifier is given as arg2</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2857
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2858
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, <span style="color:#00D">1</span>
2859
- <span style="color:#080;font-weight:bold">end</span>.chomp.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">1: &gt;mail&lt; @example.com</span><span style="color:#710">'</span></span>
2860
- <span style="color:#080;font-weight:bold">end</span>
2861
-
2862
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should display &quot;group identifier: no match&quot; if the arg2 group could not be matched</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2863
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2864
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, <span style="color:#00D">3</span>
2865
- <span style="color:#080;font-weight:bold">end</span>.chomp.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">3: no match</span><span style="color:#710">'</span></span>
2866
- <span style="color:#080;font-weight:bold">end</span>
2867
-
2868
- it <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">should also take an array of group identfiers as arg2</span><span style="color:#710">'</span></span> <span style="color:#080;font-weight:bold">do</span>
2869
- capture_stdout <span style="color:#080;font-weight:bold">do</span>
2870
- regex.vis <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mail@example.com</span><span style="color:#710">'</span></span>, [<span style="color:#00D">0</span>,<span style="color:#00D">1</span>,<span style="color:#00D">2</span>]
2871
- <span style="color:#080;font-weight:bold">end</span>.chomp.should == <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">0: &gt;mail@example.com&lt; </span><span style="color:#b0b">\n</span><span style="color:#710">&quot;</span></span>\
2872
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">1: &gt;mail&lt; @example.com</span><span style="color:#b0b">\n</span><span style="color:#710">&quot;</span></span>\
2873
- <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">&quot;</span><span style="color:#D20">2: mail@ &gt;example.com&lt; </span><span style="color:#710">&quot;</span></span>
2874
- <span style="color:#080;font-weight:bold">end</span>
2875
- <span style="color:#080;font-weight:bold">end</span></pre></div>
2876
- </div>
2877
- </div> </td></tr><tr><th>Source</th> <td> <span id="show_regexp_visualize_source">(<a href="javascript:show('regexp_visualize_source')">show</a>)</span>
2878
- <div class="source" id="regexp_visualize_source"><div class="CodeRay">
2879
- <div class="code"><pre>require <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">zucker</span><span style="color:#710">'</span></span>
2880
- <span style="color:#080;font-weight:bold">module</span> <span style="color:#B06;font-weight:bold">Zucker</span> <span style="color:#036;font-weight:bold">RegexpVisualize</span> = <span style="color:#069">true</span> <span style="color:#080;font-weight:bold">end</span>
2881
-
2882
- <span style="color:#080;font-weight:bold">class</span> <span style="color:#B06;font-weight:bold">Regexp</span>
2883
- <span style="color:#080;font-weight:bold">def</span> <span style="color:#06B;font-weight:bold">visualize</span>(string, groups = <span style="color:#069">nil</span>)
2884
- <span style="color:#080;font-weight:bold">if</span> <span style="color:#069">self</span> =~ string
2885
- <span style="color:#080;font-weight:bold">if</span> !groups
2886
- puts <span style="color:#d70">$`</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> &gt;</span><span style="color:#710">'</span></span> + <span style="color:#d70">$&amp;</span> + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">&lt; </span><span style="color:#710">'</span></span> + <span style="color:#d70">$'</span>
2887
- <span style="color:#080;font-weight:bold">else</span>
2888
- Array( groups ).each{ |group|
2889
- <span style="color:#080;font-weight:bold">begin</span>
2890
- gr_string = string.dup
2891
- gr_string[ <span style="color:#d70">$~</span>.end( group ), <span style="color:#00D">0</span> ] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">&lt; </span><span style="color:#710">'</span></span>
2892
- gr_string[ <span style="color:#d70">$~</span>.begin( group ), <span style="color:#00D">0</span> ] = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20"> &gt;</span><span style="color:#710">'</span></span>
2893
- puts group.to_s + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">: </span><span style="color:#710">'</span></span> + gr_string
2894
- <span style="color:#080;font-weight:bold">rescue</span> <span style="color:#036;font-weight:bold">IndexError</span>, <span style="color:#080;font-weight:bold">defined?</span>(<span style="color:#036;font-weight:bold">Rubinius</span>) &amp;&amp; <span style="color:#036;font-weight:bold">Rubinius</span>::<span style="color:#036;font-weight:bold">ObjectBoundsExceededError</span>
2895
- puts group.to_s + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">: no match</span><span style="color:#710">'</span></span>
2896
- <span style="color:#080;font-weight:bold">end</span>
2897
- }
2898
- <span style="color:#080;font-weight:bold">end</span>
2899
- <span style="color:#080;font-weight:bold">else</span>
2900
- puts <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">no match</span><span style="color:#710">'</span></span>
2901
- <span style="color:#080;font-weight:bold">end</span>
2902
-
2903
- <span style="color:#069">nil</span>
2904
- <span style="color:#080;font-weight:bold">end</span>
2905
-
2906
- <span style="color:#080;font-weight:bold">alias</span> <span style="color:#06B;font-weight:bold">vis</span> <span style="color:#06B;font-weight:bold">visualize</span>
2907
- <span style="color:#080;font-weight:bold">end</span>
2908
-
2909
- <span style="color:#777"># J-_-L</span></pre></div>
2910
- </div>
2911
- </div> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8</td></tr><tr><th>Discussion</th> <td><a href="http://wiki.github.com/janlelis/zucker/regexp_visualize">github wiki</a></td></tr></table>
2912
- </div>
2913
-
2914
- <h2>Changelog</h2>
2915
- <div class="cubes">
2916
- <pre class="scode">2013-04-30 | <a href="http://rubyzucker.info/13.1/">Zucker 13.1</a>
2917
- * fix alias_for bug
2918
- * let RubyVersion#inspect and RubyEngine#inspect map to #to_s
2919
- * remove Array#sum
2920
- * repackage (gh#4)
2921
-
2922
-
2923
- 2012-01-16 | <a href="http://rubyzucker.info/12.1/">Zucker 12.1</a>
2924
- * fix a requiring bug
2925
- * rename zucker/version cube to zucker/ruby_version and Zucker::PACKAGES to Zucker::PACKS
2926
- * remove more_aliases! (keep it simple...)
2927
- * add case cube: String#to_camel and String#to_snake
2928
-
2929
-
2930
- 2011-05-25 | <a href="http://rubyzucker.info/11/">Zucker 11</a>
2931
- * add Zucker::constants to check if cube has been required (e.g. Zucker::AliasFor if 'zucker/alias_for' is loaded)
2932
- * rbx tweaks
2933
- * don't define Binding#vars on rbx (prevent endless recursion)
2934
- * sandbox-not-working warning
2935
- * add Kernel#ignore_sigquit!
2936
- * fix warnings
2937
- * doc tweaks
2938
-
2939
-
2940
- 2011-04-29 | <a href="http://rubyzucker.info/10/">Zucker 10</a>
2941
- * doc/spec tweaks
2942
- * make test.rubygems.org-testable
2943
- * fix zucker 9 permission issue
2944
-
2945
-
2946
- 2011-01-22 | <a href="http://rubyzucker.info/9/">Zucker 9</a>
2947
- * remove history versions (sorry, it caused too much gem/rdoc troubles)
2948
- * add file cube: File.delete! and File.gsub
2949
- * debug pack improvements
2950
- * binding: typos + return nil
2951
- * cc: support for ripl + return nil
2952
- * mm: also show eigenclass for modules + nicer displaying + return nil
2953
- * added Regexp#visualize
2954
- * remove optional sandbox param
2955
- * rename xxx2proc to xxx_to_proc
2956
- * change rakefile/rspec/gemspec structure
2957
- * more minor fixes and improvements
2958
-
2959
-
2960
- 2010-10-06 | <a href="http://rubyzucker.info/8/">Zucker 8</a>
2961
- * fix a little alias_for bug
2962
- * disable rdoc creation when installing (in favour of the custom docs)
2963
- * change Binding#inspect to Binding.variables (was too verbose and dangerous)
2964
-
2965
-
2966
- 2010-10-03 | <a href="http://rubyzucker.info/7/">Zucker 7</a>
2967
- * fix critical OS.windows? bug
2968
-
2969
-
2970
- 2010-10-03 | <a href="http://rubyzucker.info/6/">Zucker 6</a>
2971
- * no new cubes
2972
- * bugfix for OS.posix?
2973
- * small changes + bugfixes + doc improvements
2974
- * add two user methods to Info
2975
- * change egonil semantics ( using method_missing, see http://rbjl.net/26/catch_nil.rb )
2976
- * bugfix for vv
2977
-
2978
-
2979
- 2010-09-04 | <a href="http://rubyzucker.info/5/">Zucker 5</a>
2980
- * debug edition - add two debug helpers: oo (output line, method, file) and cc (output method callstack)
2981
- * rename cube D to dd add add more debug aliases (for mm and binding)
2982
- * fix __SPECIAL_VARS__ in info and kernel cube and some minor bugfixes
2983
- * Zucker.activate_more_aliases! option
2984
- * add Hash#&amp;
2985
- * add aliases: File.filename (for basename), Dir.join and Dir.split (for File.join, split)
2986
- * add a flexible requiring mechansim in zucker.rb (no api changes)
2987
- * restructure packages
2988
- * add rake tasks for releasing
2989
- * improve RubyVersion constant (cleaner and more flexible)
2990
-
2991
-
2992
- 2010-09-01 | <a href="http://rubyzucker.info/4/">Zucker 4</a>
2993
- * fix Binding#inspect
2994
- * add RubyEngine constant
2995
- * add RubyVersion constant
2996
- * add OS constant
2997
- * add q debug method (like p but on one line)
2998
- * add String#-
2999
-
3000
-
3001
- 2010-08-14 | <a href="http://rubyzucker.info/3/">Zucker 3</a>
3002
- * add tap cube
3003
- * add Object#not
3004
- * add alias_for for an alternative alias syntax
3005
- * add String#constantize (improved AS version)
3006
- * improve Info module
3007
- * make Array#sum Rails compatibile
3008
- * improve docs
3009
- * change directory layout (no changes for requiring)
3010
- * more small changes
3011
-
3012
-
3013
- 2010-08-08 | <a href="http://rubyzucker.info/2/">Zucker 2</a>
3014
- * add info cube
3015
- * add chaining for array2proc
3016
- * fix Hash.zip
3017
- * fix instance_variables_from binding for 1.9
3018
- * more specs
3019
-
3020
-
3021
- 2010-08-06 | <a href="http://rubyzucker.info/1/">Zucker 1</a>
3022
- * initial release
3023
-
3024
-
3025
- 2010-08-05 | <a href="http://rubyzucker.info/rubyzucker.pdf">Zucker 0</a>
3026
- * pre-release for rug-b talk
3027
-
3028
- </pre>
3029
- </div>
3030
- <br/>
3031
- </div>
3032
- <div id="foot">
3033
- <div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
3034
- This is the Ruby Zucker 13.1 documentation (2013-04-30).
3035
- The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
3036
- Source at <a href="http://github.com/janlelis/zucker">github</a>.
3037
- Ruby Logo CC-BY-SA Yukihiro Matsumoto.
3038
- </div>
3039
-
3040
- </body>
3041
- </html>
3042
-