zucker 8 → 9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/MIT-LICENSE +1 -1
- data/README +1 -1
- data/Rakefile +33 -40
- data/desc/alias_for.yaml +17 -0
- data/desc/aliases.yaml +18 -0
- data/desc/array.yaml +13 -0
- data/desc/array_to_proc.yaml +16 -0
- data/desc/binding.yaml +22 -0
- data/desc/blank.yaml +9 -0
- data/desc/cc.yaml +10 -0
- data/desc/class_to_proc.yaml +11 -0
- data/desc/dd.yaml +23 -0
- data/desc/egonil.yaml +15 -0
- data/desc/engine.yaml +15 -0
- data/desc/enumerable.yaml +10 -0
- data/desc/file.yaml +15 -0
- data/desc/hash.yaml +14 -0
- data/desc/hash_to_proc.yaml +10 -0
- data/desc/info.yaml +20 -0
- data/desc/iterate.yaml +16 -0
- data/desc/ivars.yaml +19 -0
- data/desc/kernel.yaml +14 -0
- data/desc/mcopy.yaml +11 -0
- data/desc/mm.yaml +15 -0
- data/desc/not.yaml +11 -0
- data/desc/oo.yaml +10 -0
- data/desc/os.yaml +19 -0
- data/desc/qq.yaml +11 -0
- data/desc/regexp_to_proc.yaml +12 -0
- data/desc/regexp_visualize.yaml +23 -0
- data/desc/sandbox.yaml +12 -0
- data/desc/square_brackets_for.yaml +22 -0
- data/desc/string.yaml +23 -0
- data/desc/tap.yaml +17 -0
- data/desc/unary.yaml +15 -0
- data/desc/union.yaml +10 -0
- data/desc/version.yaml +28 -0
- data/doc/zucker_doc.html +2688 -0
- data/doc/zucker_doc.rb +553 -0
- data/lib/zucker.rb +10 -9
- data/lib/zucker/array2proc.rb +2 -18
- data/lib/zucker/{2/array2proc.rb → array_to_proc.rb} +0 -0
- data/lib/zucker/binding.rb +4 -5
- data/lib/zucker/cc.rb +7 -2
- data/lib/zucker/class2proc.rb +2 -12
- data/lib/zucker/{1/class2proc.rb → class_to_proc.rb} +0 -0
- data/lib/zucker/file.rb +22 -0
- data/lib/zucker/hash2proc.rb +2 -16
- data/lib/zucker/{1/hash2proc.rb → hash_to_proc.rb} +0 -0
- data/lib/zucker/kernel.rb +4 -2
- data/lib/zucker/mm.rb +14 -8
- data/lib/zucker/regexp2proc.rb +2 -12
- data/lib/zucker/{1/regexp2proc.rb → regexp_to_proc.rb} +0 -0
- data/lib/zucker/regexp_visualize.rb +26 -0
- data/lib/zucker/sandbox.rb +1 -11
- data/spec/alias_for_spec.rb +117 -0
- data/spec/aliases_spec.rb +13 -0
- data/spec/array_spec.rb +23 -0
- data/spec/array_to_proc_spec.rb +12 -0
- data/spec/binding_spec.rb +6 -0
- data/spec/blank_spec.rb +20 -0
- data/spec/cc_spec.rb +6 -0
- data/spec/class_to_proc_spec.rb +9 -0
- data/spec/dd_spec.rb +37 -0
- data/spec/egonil_spec.rb +32 -0
- data/spec/engine_spec.rb +11 -0
- data/spec/enumerable_spec.rb +8 -0
- data/spec/file_spec.rb +30 -0
- data/spec/hash_spec.rb +25 -0
- data/spec/hash_to_proc_spec.rb +12 -0
- data/spec/info_spec.rb +8 -0
- data/spec/iterate_spec.rb +59 -0
- data/spec/ivars_spec.rb +20 -0
- data/spec/kernel_spec.rb +37 -0
- data/spec/mcopy_spec.rb +13 -0
- data/spec/mm_spec.rb +6 -0
- data/spec/not_spec.rb +12 -0
- data/spec/oo_spec.rb +6 -0
- data/spec/os_spec.rb +37 -0
- data/spec/qq_spec.rb +19 -0
- data/spec/regexp_to_proc_spec.rb +9 -0
- data/spec/regexp_visualize_spec.rb +38 -0
- data/spec/sandbox_spec.rb +12 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/square_brackets_for_spec.rb +59 -0
- data/spec/string_spec.rb +65 -0
- data/spec/tap_spec.rb +18 -0
- data/spec/unary_spec.rb +26 -0
- data/spec/union_spec.rb +17 -0
- data/spec/version_spec.rb +55 -0
- data/zucker.gemspec +21 -0
- metadata +139 -323
- data/lib/zucker/1/D.rb +0 -21
- data/lib/zucker/1/aliases.rb +0 -30
- data/lib/zucker/1/all.rb +0 -8
- data/lib/zucker/1/array.rb +0 -19
- data/lib/zucker/1/array2proc.rb +0 -12
- data/lib/zucker/1/binding.rb +0 -27
- data/lib/zucker/1/blank.rb +0 -28
- data/lib/zucker/1/debug.rb +0 -6
- data/lib/zucker/1/default.rb +0 -27
- data/lib/zucker/1/egonil.rb +0 -16
- data/lib/zucker/1/enumerable.rb +0 -14
- data/lib/zucker/1/hash.rb +0 -21
- data/lib/zucker/1/iterate.rb +0 -25
- data/lib/zucker/1/ivars.rb +0 -28
- data/lib/zucker/1/kernel.rb +0 -22
- data/lib/zucker/1/mcopy.rb +0 -11
- data/lib/zucker/1/mm.rb +0 -33
- data/lib/zucker/1/sandbox.rb +0 -17
- data/lib/zucker/1/square_brackets_for.rb +0 -22
- data/lib/zucker/1/string.rb +0 -27
- data/lib/zucker/1/unary.rb +0 -24
- data/lib/zucker/1/union.rb +0 -16
- data/lib/zucker/2/D.rb +0 -21
- data/lib/zucker/2/aliases.rb +0 -30
- data/lib/zucker/2/all.rb +0 -8
- data/lib/zucker/2/array.rb +0 -19
- data/lib/zucker/2/binding.rb +0 -27
- data/lib/zucker/2/blank.rb +0 -28
- data/lib/zucker/2/class2proc.rb +0 -12
- data/lib/zucker/2/debug.rb +0 -6
- data/lib/zucker/2/default.rb +0 -27
- data/lib/zucker/2/egonil.rb +0 -16
- data/lib/zucker/2/enumerable.rb +0 -14
- data/lib/zucker/2/hash.rb +0 -21
- data/lib/zucker/2/hash2proc.rb +0 -16
- data/lib/zucker/2/info.rb +0 -166
- data/lib/zucker/2/iterate.rb +0 -25
- data/lib/zucker/2/ivars.rb +0 -28
- data/lib/zucker/2/kernel.rb +0 -20
- data/lib/zucker/2/mcopy.rb +0 -11
- data/lib/zucker/2/mm.rb +0 -33
- data/lib/zucker/2/regexp2proc.rb +0 -12
- data/lib/zucker/2/sandbox.rb +0 -21
- data/lib/zucker/2/square_brackets_for.rb +0 -22
- data/lib/zucker/2/string.rb +0 -27
- data/lib/zucker/2/unary.rb +0 -24
- data/lib/zucker/2/union.rb +0 -16
- data/lib/zucker/3/D.rb +0 -21
- data/lib/zucker/3/alias_for.rb +0 -21
- data/lib/zucker/3/aliases.rb +0 -30
- data/lib/zucker/3/all.rb +0 -8
- data/lib/zucker/3/array.rb +0 -25
- data/lib/zucker/3/array2proc.rb +0 -18
- data/lib/zucker/3/binding.rb +0 -27
- data/lib/zucker/3/blank.rb +0 -28
- data/lib/zucker/3/class2proc.rb +0 -12
- data/lib/zucker/3/debug.rb +0 -8
- data/lib/zucker/3/default.rb +0 -37
- data/lib/zucker/3/egonil.rb +0 -16
- data/lib/zucker/3/enumerable.rb +0 -14
- data/lib/zucker/3/hash.rb +0 -21
- data/lib/zucker/3/hash2proc.rb +0 -16
- data/lib/zucker/3/info.rb +0 -169
- data/lib/zucker/3/iterate.rb +0 -25
- data/lib/zucker/3/ivars.rb +0 -28
- data/lib/zucker/3/kernel.rb +0 -50
- data/lib/zucker/3/mcopy.rb +0 -10
- data/lib/zucker/3/mm.rb +0 -33
- data/lib/zucker/3/not.rb +0 -19
- data/lib/zucker/3/regexp2proc.rb +0 -12
- data/lib/zucker/3/sandbox.rb +0 -21
- data/lib/zucker/3/square_brackets_for.rb +0 -22
- data/lib/zucker/3/string.rb +0 -49
- data/lib/zucker/3/tap.rb +0 -11
- data/lib/zucker/3/unary.rb +0 -24
- data/lib/zucker/3/union.rb +0 -16
- data/lib/zucker/4/D.rb +0 -21
- data/lib/zucker/4/alias_for.rb +0 -21
- data/lib/zucker/4/aliases.rb +0 -35
- data/lib/zucker/4/all.rb +0 -8
- data/lib/zucker/4/array.rb +0 -25
- data/lib/zucker/4/array2proc.rb +0 -18
- data/lib/zucker/4/binding.rb +0 -27
- data/lib/zucker/4/blank.rb +0 -28
- data/lib/zucker/4/class2proc.rb +0 -12
- data/lib/zucker/4/debug.rb +0 -8
- data/lib/zucker/4/default.rb +0 -40
- data/lib/zucker/4/egonil.rb +0 -16
- data/lib/zucker/4/engine.rb +0 -69
- data/lib/zucker/4/enumerable.rb +0 -14
- data/lib/zucker/4/hash.rb +0 -21
- data/lib/zucker/4/hash2proc.rb +0 -16
- data/lib/zucker/4/info.rb +0 -173
- data/lib/zucker/4/iterate.rb +0 -25
- data/lib/zucker/4/ivars.rb +0 -28
- data/lib/zucker/4/kernel.rb +0 -27
- data/lib/zucker/4/mcopy.rb +0 -10
- data/lib/zucker/4/mm.rb +0 -33
- data/lib/zucker/4/not.rb +0 -19
- data/lib/zucker/4/os.rb +0 -50
- data/lib/zucker/4/q.rb +0 -7
- data/lib/zucker/4/regexp2proc.rb +0 -12
- data/lib/zucker/4/sandbox.rb +0 -23
- data/lib/zucker/4/square_brackets_for.rb +0 -22
- data/lib/zucker/4/string.rb +0 -54
- data/lib/zucker/4/tap.rb +0 -11
- data/lib/zucker/4/unary.rb +0 -24
- data/lib/zucker/4/union.rb +0 -16
- data/lib/zucker/4/version.rb +0 -115
- data/lib/zucker/5/alias_for.rb +0 -21
- data/lib/zucker/5/aliases.rb +0 -51
- data/lib/zucker/5/all.rb +0 -4
- data/lib/zucker/5/array.rb +0 -25
- data/lib/zucker/5/array2proc.rb +0 -18
- data/lib/zucker/5/binding.rb +0 -34
- data/lib/zucker/5/blank.rb +0 -28
- data/lib/zucker/5/cc.rb +0 -25
- data/lib/zucker/5/class2proc.rb +0 -12
- data/lib/zucker/5/control.rb +0 -4
- data/lib/zucker/5/dd.rb +0 -24
- data/lib/zucker/5/debug.rb +0 -4
- data/lib/zucker/5/default.rb +0 -4
- data/lib/zucker/5/egonil.rb +0 -16
- data/lib/zucker/5/engine.rb +0 -69
- data/lib/zucker/5/enumerable.rb +0 -14
- data/lib/zucker/5/env.rb +0 -4
- data/lib/zucker/5/extensions.rb +0 -4
- data/lib/zucker/5/hash.rb +0 -27
- data/lib/zucker/5/hash2proc.rb +0 -16
- data/lib/zucker/5/info.rb +0 -177
- data/lib/zucker/5/iterate.rb +0 -26
- data/lib/zucker/5/ivars.rb +0 -28
- data/lib/zucker/5/kernel.rb +0 -34
- data/lib/zucker/5/mcopy.rb +0 -10
- data/lib/zucker/5/mm.rb +0 -34
- data/lib/zucker/5/not.rb +0 -19
- data/lib/zucker/5/object.rb +0 -4
- data/lib/zucker/5/oo.rb +0 -17
- data/lib/zucker/5/os.rb +0 -49
- data/lib/zucker/5/qq.rb +0 -12
- data/lib/zucker/5/regexp2proc.rb +0 -12
- data/lib/zucker/5/sandbox.rb +0 -25
- data/lib/zucker/5/shortcuts.rb +0 -4
- data/lib/zucker/5/square_brackets_for.rb +0 -21
- data/lib/zucker/5/string.rb +0 -54
- data/lib/zucker/5/tap.rb +0 -11
- data/lib/zucker/5/to_proc.rb +0 -4
- data/lib/zucker/5/unary.rb +0 -24
- data/lib/zucker/5/union.rb +0 -16
- data/lib/zucker/5/version.rb +0 -118
- data/lib/zucker/6/alias_for.rb +0 -21
- data/lib/zucker/6/aliases.rb +0 -51
- data/lib/zucker/6/all.rb +0 -4
- data/lib/zucker/6/array.rb +0 -25
- data/lib/zucker/6/array2proc.rb +0 -18
- data/lib/zucker/6/binding.rb +0 -34
- data/lib/zucker/6/blank.rb +0 -28
- data/lib/zucker/6/cc.rb +0 -25
- data/lib/zucker/6/class2proc.rb +0 -12
- data/lib/zucker/6/control.rb +0 -4
- data/lib/zucker/6/dd.rb +0 -23
- data/lib/zucker/6/debug.rb +0 -4
- data/lib/zucker/6/default.rb +0 -4
- data/lib/zucker/6/egonil.rb +0 -27
- data/lib/zucker/6/engine.rb +0 -68
- data/lib/zucker/6/enumerable.rb +0 -14
- data/lib/zucker/6/env.rb +0 -4
- data/lib/zucker/6/extensions.rb +0 -4
- data/lib/zucker/6/hash.rb +0 -27
- data/lib/zucker/6/hash2proc.rb +0 -16
- data/lib/zucker/6/info.rb +0 -192
- data/lib/zucker/6/iterate.rb +0 -27
- data/lib/zucker/6/ivars.rb +0 -28
- data/lib/zucker/6/kernel.rb +0 -34
- data/lib/zucker/6/mcopy.rb +0 -10
- data/lib/zucker/6/mm.rb +0 -34
- data/lib/zucker/6/not.rb +0 -19
- data/lib/zucker/6/object.rb +0 -4
- data/lib/zucker/6/oo.rb +0 -17
- data/lib/zucker/6/os.rb +0 -54
- data/lib/zucker/6/qq.rb +0 -12
- data/lib/zucker/6/regexp2proc.rb +0 -12
- data/lib/zucker/6/sandbox.rb +0 -25
- data/lib/zucker/6/shortcuts.rb +0 -4
- data/lib/zucker/6/square_brackets_for.rb +0 -21
- data/lib/zucker/6/string.rb +0 -54
- data/lib/zucker/6/tap.rb +0 -11
- data/lib/zucker/6/to_proc.rb +0 -4
- data/lib/zucker/6/unary.rb +0 -24
- data/lib/zucker/6/union.rb +0 -16
- data/lib/zucker/6/version.rb +0 -118
- data/lib/zucker/7/alias_for.rb +0 -21
- data/lib/zucker/7/aliases.rb +0 -51
- data/lib/zucker/7/all.rb +0 -4
- data/lib/zucker/7/array.rb +0 -25
- data/lib/zucker/7/array2proc.rb +0 -18
- data/lib/zucker/7/binding.rb +0 -34
- data/lib/zucker/7/blank.rb +0 -28
- data/lib/zucker/7/cc.rb +0 -25
- data/lib/zucker/7/class2proc.rb +0 -12
- data/lib/zucker/7/control.rb +0 -4
- data/lib/zucker/7/dd.rb +0 -23
- data/lib/zucker/7/debug.rb +0 -4
- data/lib/zucker/7/default.rb +0 -4
- data/lib/zucker/7/egonil.rb +0 -27
- data/lib/zucker/7/engine.rb +0 -68
- data/lib/zucker/7/enumerable.rb +0 -14
- data/lib/zucker/7/env.rb +0 -4
- data/lib/zucker/7/extensions.rb +0 -4
- data/lib/zucker/7/hash.rb +0 -27
- data/lib/zucker/7/hash2proc.rb +0 -16
- data/lib/zucker/7/info.rb +0 -192
- data/lib/zucker/7/iterate.rb +0 -27
- data/lib/zucker/7/ivars.rb +0 -28
- data/lib/zucker/7/kernel.rb +0 -34
- data/lib/zucker/7/mcopy.rb +0 -10
- data/lib/zucker/7/mm.rb +0 -34
- data/lib/zucker/7/not.rb +0 -19
- data/lib/zucker/7/object.rb +0 -4
- data/lib/zucker/7/oo.rb +0 -17
- data/lib/zucker/7/os.rb +0 -54
- data/lib/zucker/7/qq.rb +0 -12
- data/lib/zucker/7/regexp2proc.rb +0 -12
- data/lib/zucker/7/sandbox.rb +0 -25
- data/lib/zucker/7/shortcuts.rb +0 -4
- data/lib/zucker/7/square_brackets_for.rb +0 -21
- data/lib/zucker/7/string.rb +0 -54
- data/lib/zucker/7/tap.rb +0 -11
- data/lib/zucker/7/to_proc.rb +0 -4
- data/lib/zucker/7/unary.rb +0 -24
- data/lib/zucker/7/union.rb +0 -16
- data/lib/zucker/7/version.rb +0 -118
- data/lib/zucker/8/alias_for.rb +0 -21
- data/lib/zucker/8/aliases.rb +0 -51
- data/lib/zucker/8/all.rb +0 -4
- data/lib/zucker/8/array.rb +0 -25
- data/lib/zucker/8/array2proc.rb +0 -18
- data/lib/zucker/8/binding.rb +0 -33
- data/lib/zucker/8/blank.rb +0 -28
- data/lib/zucker/8/cc.rb +0 -25
- data/lib/zucker/8/class2proc.rb +0 -12
- data/lib/zucker/8/control.rb +0 -4
- data/lib/zucker/8/dd.rb +0 -23
- data/lib/zucker/8/debug.rb +0 -4
- data/lib/zucker/8/default.rb +0 -4
- data/lib/zucker/8/egonil.rb +0 -27
- data/lib/zucker/8/engine.rb +0 -68
- data/lib/zucker/8/enumerable.rb +0 -14
- data/lib/zucker/8/env.rb +0 -4
- data/lib/zucker/8/extensions.rb +0 -4
- data/lib/zucker/8/hash.rb +0 -27
- data/lib/zucker/8/hash2proc.rb +0 -16
- data/lib/zucker/8/info.rb +0 -192
- data/lib/zucker/8/iterate.rb +0 -27
- data/lib/zucker/8/ivars.rb +0 -28
- data/lib/zucker/8/kernel.rb +0 -34
- data/lib/zucker/8/mcopy.rb +0 -10
- data/lib/zucker/8/mm.rb +0 -34
- data/lib/zucker/8/not.rb +0 -19
- data/lib/zucker/8/object.rb +0 -4
- data/lib/zucker/8/oo.rb +0 -17
- data/lib/zucker/8/os.rb +0 -54
- data/lib/zucker/8/qq.rb +0 -12
- data/lib/zucker/8/regexp2proc.rb +0 -12
- data/lib/zucker/8/sandbox.rb +0 -25
- data/lib/zucker/8/shortcuts.rb +0 -4
- data/lib/zucker/8/square_brackets_for.rb +0 -21
- data/lib/zucker/8/string.rb +0 -54
- data/lib/zucker/8/tap.rb +0 -11
- data/lib/zucker/8/to_proc.rb +0 -4
- data/lib/zucker/8/unary.rb +0 -24
- data/lib/zucker/8/union.rb +0 -16
- data/lib/zucker/8/version.rb +0 -118
data/desc/kernel.yaml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
kernel:
|
2
|
+
summary: Some useful general shortcut methods.
|
3
|
+
why: Readability.
|
4
|
+
methods:
|
5
|
+
activate_warnings!: "activate_warnings! # sets $VERBOSE to 1"
|
6
|
+
deactivate_warnings!: "deactivate_warnings! # sets $VERBOSE to 0"
|
7
|
+
executed_directly?, standalone?: "executed_directly? # checks, if the current file is run directly -> true"
|
8
|
+
library?: "library? # checks, if the current file is run directly -> false"
|
9
|
+
irb?: "irb? # returns false if not called from IRB"
|
10
|
+
ignore_sigint!: "ignore_sigint! # blocks CTRL+C"
|
11
|
+
versions:
|
12
|
+
- 1.9
|
13
|
+
- 1.8
|
14
|
+
|
data/desc/mcopy.yaml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
mcopy:
|
2
|
+
summary: Adds ⇧Object#mcopy⇧ to create a deep copy using ⇧Marshal⇧.
|
3
|
+
why: Every Ruby book describes, you can do ⇧Marshal.load Marshal.dump object⇧ to create a deep copy... But who needs this verbose syntax in everyday coding?
|
4
|
+
methods:
|
5
|
+
Object#mcopy: |
|
6
|
+
a = %w[hello world]
|
7
|
+
b = a.mcopy
|
8
|
+
versions:
|
9
|
+
- 1.9
|
10
|
+
- 1.8
|
11
|
+
|
data/desc/mm.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
mm:
|
2
|
+
summary: ⇧m⇧ displays an ordered public method list.
|
3
|
+
why: See one object's methods without those rarely used inherited methods.
|
4
|
+
methods:
|
5
|
+
Object#m, Object#mm, Object#method_list: |
|
6
|
+
>> 5.mm 2 # outputs the list (2 levels deep), for example:
|
7
|
+
# = Fixnum
|
8
|
+
# to_s -@ + - * / div % modulo divmod fdiv ** abs magnitude == === <=> > >= < <= ~ & | ^ [] << >> to_f size zero? odd? even? succ
|
9
|
+
# = Integer
|
10
|
+
# 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
|
11
|
+
info:
|
12
|
+
- See →this article→http://rbjl.net/31-the-multi-mega-method-list→ for more information.
|
13
|
+
versions:
|
14
|
+
- 1.9
|
15
|
+
- 1.8
|
data/desc/not.yaml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
not:
|
2
|
+
summary: ⇧not⇧ returns an object on which all methods are redirected to it's receiver object, but returns inverted boolean.
|
3
|
+
why: Better readability.
|
4
|
+
methods:
|
5
|
+
Object#not: |
|
6
|
+
[1,2,3].not.empty? # true
|
7
|
+
info:
|
8
|
+
- See →this article by Jay Field→http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html→ for more information.
|
9
|
+
versions:
|
10
|
+
- 1.9
|
11
|
+
|
data/desc/oo.yaml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
oo:
|
2
|
+
summary: The ⇧o⇧ method outputs the current method, line, file and label (if given).
|
3
|
+
methods:
|
4
|
+
o, oo: |
|
5
|
+
o # e.g: Reached method `irb_binding', line 1 of file (irb)
|
6
|
+
o:Label # e.g: Label: reached method `abc', line 7 of file (irb)
|
7
|
+
versions:
|
8
|
+
- 1.9
|
9
|
+
- 1.8
|
10
|
+
|
data/desc/os.yaml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
os:
|
2
|
+
summary: Detect the operating system.
|
3
|
+
why: Checking for ⇧RUBY_PLATFORM⇧ does not report the OS when using JRuby and for better readability.
|
4
|
+
methods:
|
5
|
+
OS: "# displays the operating system"
|
6
|
+
OS.linux?: "OS.linux? # true if you are on a linux machine"
|
7
|
+
OS.mac?: "OS.mac? # true for macs"
|
8
|
+
OS.bsd?: "OS.bsd? # true for bsds"
|
9
|
+
OS.windows?: "include OS; windows? # true for windows. Example of including OS to get the os methods into the global namespace"
|
10
|
+
OS.solaris?: "OS.solaris? # true for solaris/sunos"
|
11
|
+
OS.posix?: "OS.posix? # true if system is posix compatible"
|
12
|
+
OS.is?: "OS.is? /x/ # matches the regex against the os string"
|
13
|
+
info:
|
14
|
+
- This cube depends on system information and may not be 100% accurate.
|
15
|
+
- "Syntax and semantic inspired by Roger Pack's →os gem→http://github.com/rdp/os→."
|
16
|
+
versions:
|
17
|
+
- 1.9
|
18
|
+
- 1.8
|
19
|
+
|
data/desc/qq.yaml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
qq:
|
2
|
+
summary: The ⇧q⇧ method does the same like ⇧p⇧, but puts out multiple arguments on the same line.
|
3
|
+
methods:
|
4
|
+
q, qq: |
|
5
|
+
q "zucker", '', {6=>7}, 5, 2.3, :o # outputs "zucker" "" {6=>7} 5 2.3 :o
|
6
|
+
info:
|
7
|
+
- Inspired by →rdp→http://github.com/rdp/sane/blob/master/lib/sane/pps.rb→.
|
8
|
+
versions:
|
9
|
+
- 1.9
|
10
|
+
- 1.8
|
11
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
regexp_to_proc:
|
2
|
+
summary: Use ⇧&/regex/⇧ to match it against strings.
|
3
|
+
methods:
|
4
|
+
Regexp#to_proc: |
|
5
|
+
%w|just another string array|.map &/[jy]/ # => ["j", nil, nil, "y"]
|
6
|
+
%w|just another string array|.select &/[jy]/ # => ["just", "array"]
|
7
|
+
info:
|
8
|
+
- "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
|
9
|
+
versions:
|
10
|
+
- 1.9
|
11
|
+
- 1.8
|
12
|
+
|
@@ -0,0 +1,23 @@
|
|
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
|
+
|
data/desc/sandbox.yaml
ADDED
@@ -0,0 +1,12 @@
|
|
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
|
@@ -0,0 +1,22 @@
|
|
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
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
string:
|
2
|
+
summary: ⇧String⇧ extensions.
|
3
|
+
why: Strings cannot be comfortable enough ;).
|
4
|
+
methods:
|
5
|
+
String#-: "'1234abc5678' - 'b' - /\d/ # => ac"
|
6
|
+
String#^: "'Yes vs No'^7 # => '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
|
23
|
+
|
data/desc/tap.yaml
ADDED
@@ -0,0 +1,17 @@
|
|
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
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
unary:
|
2
|
+
summary: Easy conversion between strings and symbols.
|
3
|
+
why: "Sometimes, you do not care if you get a ⇧String⇧ or ⇧Symbol⇧ as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators ⇧String#-@⇧ and ⇧Symbol#+@⇧."
|
4
|
+
methods:
|
5
|
+
String#+@: "+'was_string' # => 'was_string'"
|
6
|
+
String#-@: "-'was_string' # => :was_string"
|
7
|
+
Symbol#+@: "+:was_symbol # => 'was_symbol'"
|
8
|
+
Symbol#-@: "-:was_symbol # => :was_symbol"
|
9
|
+
package: default
|
10
|
+
info:
|
11
|
+
- "Inspired by (I've seen the unary + for Symbol somewhere on the net... but cannot remember where...)"
|
12
|
+
versions:
|
13
|
+
- 1.9
|
14
|
+
- 1.8 (⇧+:literal⇧ not possible)
|
15
|
+
|
data/desc/union.yaml
ADDED
data/desc/version.yaml
ADDED
@@ -0,0 +1,28 @@
|
|
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/doc/zucker_doc.html
ADDED
@@ -0,0 +1,2688 @@
|
|
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 9</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 9</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. Everything is documented on this page.</p>
|
287
|
+
<p class="text">See the <a href="http://github.com/janlelis/zucker">github page</a> and wiki for issues, discussion and information about contributing.</p>
|
288
|
+
|
289
|
+
<h2>Install</h2>
|
290
|
+
<p class="text">
|
291
|
+
<code class="scode">gem install zucker --no-rdoc --no-ri # might need sudo</code>
|
292
|
+
</p>
|
293
|
+
<h3 style="padding-left:1.3em">Quickstart</h3>
|
294
|
+
<p class="text">
|
295
|
+
<code class="scode">require 'zucker/all'</code>
|
296
|
+
</p>
|
297
|
+
<h2>Usage / organisation</h2>
|
298
|
+
<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:
|
299
|
+
<code class="scode">require 'zucker/cube_or_pack_name'</code>
|
300
|
+
</p>
|
301
|
+
<h3 style="padding-left:1.3em">Packages</h3>
|
302
|
+
<ul class="text">
|
303
|
+
<li><a href="#control" style="text-decoration:none;font-weight:bold;">control</a>: Cubes that change program behaviour.</li>
|
304
|
+
<li><a href="#env" style="text-decoration:none;font-weight:bold;">env</a>: Cubes for collecting information.</li>
|
305
|
+
<li><a href="#extensions" style="text-decoration:none;font-weight:bold;">extensions</a>: More extensions for Ruby base classes.</li>
|
306
|
+
<li><a href="#object" style="text-decoration:none;font-weight:bold;">object</a>: Extensions for <code>Object</code>.</li>
|
307
|
+
<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>&</code>.</li>
|
308
|
+
<li><a href="#shortcuts" style="text-decoration:none;font-weight:bold;">shortcuts</a>: Cubes that save you keystrokes or disambiguate things.</li>
|
309
|
+
<li><a href="#debug" style="text-decoration:none;font-weight:bold;">debug</a>: Print debugging tools.</li>
|
310
|
+
</ul>
|
311
|
+
<p class="text">
|
312
|
+
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>
|
313
|
+
|
314
|
+
<h3 style="padding-left:1.3em">Which methods and constants are added directly to the global namespace by the <em>default</em> pack?</h3>
|
315
|
+
<p class="text">
|
316
|
+
|
317
|
+
<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?, standalone?, library?, ignore_sigint!, sandbox, tap_on, make_new, (blank?, present?, mcopy)</code>
|
318
|
+
</p>
|
319
|
+
|
320
|
+
|
321
|
+
<h2 title="require 'zucker/control'" id="control">Cubes[control]</h2>
|
322
|
+
|
323
|
+
<div class="cubes">
|
324
|
+
<h3 title="require 'zucker/egonil'" id="egonil">egonil</h3>
|
325
|
+
<table class="cube_table"
|
326
|
+
id="egonil_cube"
|
327
|
+
title="require 'zucker/egonil'"> <tr><th>Summary</th> <td>Creates a block, where <code>nil</code> does not raise <code>NoMethodError</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>egonil, nn</h5><pre class="usage source" style="display:block">egonil <span class="r">do</span>
|
328
|
+
<span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist
|
329
|
+
<span class="r">end</span> <span class="c"># => nil</span>
|
330
|
+
|
331
|
+
nn{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist } <span class="c"># => nil</span>
|
332
|
+
</pre></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>
|
333
|
+
<pre class="source" id="egonil_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/egonil</span><span class="dl">'</span></span>
|
334
|
+
|
335
|
+
describe <span class="s"><span class="dl">'</span><span class="k">egonil</span><span class="dl">'</span></span> <span class="r">do</span>
|
336
|
+
it <span class="s"><span class="dl">'</span><span class="k">should not raise nil exceptions in the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
337
|
+
proc <span class="r">do</span>
|
338
|
+
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
339
|
+
<span class="r">end</span>.should_not raise_exception
|
340
|
+
<span class="r">end</span>
|
341
|
+
|
342
|
+
it <span class="s"><span class="dl">'</span><span class="k">should restore default behaviour after the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
343
|
+
proc <span class="r">do</span>
|
344
|
+
egonil{ <span class="pc">nil</span>.some_methods.that[<span class="sy">:do</span>].not.exist }
|
345
|
+
<span class="r">end</span>.should_not raise_exception
|
346
|
+
|
347
|
+
proc <span class="r">do</span>
|
348
|
+
<span class="pc">nil</span>.a_method
|
349
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
350
|
+
<span class="r">end</span>
|
351
|
+
|
352
|
+
it <span class="s"><span class="dl">'</span><span class="k">raise NoMethodError for non-nil objects</span><span class="dl">'</span></span> <span class="r">do</span>
|
353
|
+
proc <span class="r">do</span>
|
354
|
+
egonil{ <span class="i">5</span>.a_method }
|
355
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
356
|
+
<span class="r">end</span>
|
357
|
+
|
358
|
+
it <span class="s"><span class="dl">'</span><span class="k">should raise other Exceptions</span><span class="dl">'</span></span> <span class="r">do</span>
|
359
|
+
proc <span class="r">do</span>
|
360
|
+
egonil{ <span class="i">5</span> / <span class="i">0</span> }
|
361
|
+
<span class="r">end</span>.should raise_exception <span class="co">ZeroDivisionError</span>
|
362
|
+
<span class="r">end</span>
|
363
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_egonil_source">(<a href="javascript:show('egonil_source')">show</a>)</span>
|
364
|
+
<pre class="source" id="egonil_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
365
|
+
|
366
|
+
<span class="c"># code by Yohan, slightly edited and comments by me</span>
|
367
|
+
<span class="r">def</span> <span class="fu">egonil</span>(&block)
|
368
|
+
<span class="c"># grip methods</span>
|
369
|
+
ori_method_missing = <span class="co">NilClass</span>.instance_method(<span class="sy">:method_missing</span>)
|
370
|
+
catch_method_missing = <span class="co">NilClass</span>.instance_method(<span class="sy">:catch_method_missing</span>)
|
371
|
+
<span class="c"># activate ego mode</span>
|
372
|
+
<span class="co">NilClass</span>.send <span class="sy">:define_method</span>, <span class="sy">:method_missing</span>, catch_method_missing
|
373
|
+
<span class="c"># run code</span>
|
374
|
+
<span class="r">yield</span>
|
375
|
+
<span class="r">ensure</span>
|
376
|
+
<span class="c"># no matter what happens: restore default nil behaviour</span>
|
377
|
+
<span class="co">NilClass</span>.send <span class="sy">:define_method</span>, <span class="sy">:method_missing</span>, ori_method_missing
|
378
|
+
<span class="r">end</span>
|
379
|
+
|
380
|
+
<span class="c"># this is the ego nil</span>
|
381
|
+
<span class="r">class</span> <span class="cl">NilClass</span>
|
382
|
+
<span class="r">def</span> <span class="fu">catch_method_missing</span>(m, *args, &block)
|
383
|
+
<span class="pc">nil</span>
|
384
|
+
<span class="r">end</span>
|
385
|
+
<span class="r">end</span>
|
386
|
+
|
387
|
+
<span class="r">alias</span> <span class="fu">nn</span> <span class="fu">egonil</span>
|
388
|
+
|
389
|
+
<span class="c"># J-_-L</span></pre> </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>
|
390
|
+
<table class="cube_table"
|
391
|
+
id="iterate_cube"
|
392
|
+
title="require 'zucker/iterate'"> <tr><th>Summary</th> <td>Iterate over one or more collections.</td></tr><tr><th>Why?</th> <td>It's like <code>.each</code> with two differences: It feels more like a control structure and you can easily iterate over multiple objects.</td></tr><tr><th>Methods/Usage</th> <td><h5>iterate</h5><pre class="usage source" style="display:block">iterate [<span class="i">1</span>,<span class="i">2</span>], [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>] <span class="r">do</span> |e,f|
|
393
|
+
puts <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="k">,</span><span class="il"><span class="idl">#{</span>f<span class="idl">}</span></span><span class="dl">"</span></span>
|
394
|
+
<span class="r">end</span>
|
395
|
+
<span class="c"># outputs</span>
|
396
|
+
<span class="c"># 1,3</span>
|
397
|
+
<span class="c"># 2,4</span>
|
398
|
+
<span class="c"># ,5</span>
|
399
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_iterate_spec">(<a href="javascript:show('iterate_spec')">show</a>)</span>
|
400
|
+
<pre class="source" id="iterate_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/iterate</span><span class="dl">'</span></span>
|
401
|
+
|
402
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#iterate</span><span class="dl">'</span></span> <span class="r">do</span>
|
403
|
+
let <span class="sy">:a</span> <span class="r">do</span> [<span class="i">1</span>, <span class="i">2</span>, <span class="i">3</span>] <span class="r">end</span>
|
404
|
+
let <span class="sy">:b</span> <span class="r">do</span> <span class="s"><span class="dl">%w|</span><span class="k">a b c d</span><span class="dl">|</span></span> <span class="r">end</span>
|
405
|
+
let <span class="sy">:res</span> <span class="r">do</span> <span class="co">Hash</span>.new {[]} <span class="r">end</span>
|
406
|
+
|
407
|
+
it <span class="s"><span class="dl">'</span><span class="k">should behave like Enumerable#each for a single argument</span><span class="dl">'</span></span> <span class="r">do</span>
|
408
|
+
iterate a <span class="r">do</span> |ele|
|
409
|
+
res[<span class="sy">:iter</span>] << ele
|
410
|
+
<span class="r">end</span>
|
411
|
+
|
412
|
+
a.each <span class="r">do</span> |ele|
|
413
|
+
res[<span class="sy">:each</span>] << ele
|
414
|
+
<span class="r">end</span>
|
415
|
+
|
416
|
+
res[<span class="sy">:iter</span>].should == res[<span class="sy">:each</span>]
|
417
|
+
<span class="r">end</span>
|
418
|
+
|
419
|
+
it <span class="s"><span class="dl">'</span><span class="k">should pass the right params to the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
420
|
+
res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
|
421
|
+
res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
|
422
|
+
res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
|
423
|
+
|
424
|
+
|
425
|
+
iterate a, b <span class="r">do</span> |e,f|
|
426
|
+
res[<span class="sy">:iter_a_b</span>] << [e, f]
|
427
|
+
<span class="r">end</span>
|
428
|
+
|
429
|
+
res[<span class="sy">:iter_a_b</span>].should == [
|
430
|
+
[<span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>],
|
431
|
+
[<span class="i">2</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>],
|
432
|
+
[<span class="i">3</span>, <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>],
|
433
|
+
[<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>],
|
434
|
+
]
|
435
|
+
|
436
|
+
iterate b, a <span class="r">do</span> |e,f|
|
437
|
+
res[<span class="sy">:iter_b_a</span>] << [e, f]
|
438
|
+
<span class="r">end</span>
|
439
|
+
|
440
|
+
res[<span class="sy">:iter_b_a</span>].should == [
|
441
|
+
[<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, <span class="i">1</span>],
|
442
|
+
[<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>, <span class="i">2</span>],
|
443
|
+
[<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>, <span class="i">3</span>],
|
444
|
+
[<span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>, <span class="pc">nil</span>],
|
445
|
+
]
|
446
|
+
|
447
|
+
<span class="r">end</span>
|
448
|
+
|
449
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return enumerators if no block is applied</span><span class="dl">'</span></span> <span class="r">do</span>
|
450
|
+
res = <span class="co">Hash</span>.new {[]} <span class="c"># TODO: why?</span>
|
451
|
+
res[<span class="sy">:iter_a_b</span>] = [] <span class="c"># ....</span>
|
452
|
+
res[<span class="sy">:iter_b_a</span>] = [] <span class="c"># ....</span>
|
453
|
+
|
454
|
+
enumerator = iterate a,b
|
455
|
+
enumerator.should be_kind_of(<span class="co">RUBY_VERSION</span> < <span class="s"><span class="dl">'</span><span class="k">1.9</span><span class="dl">'</span></span> ? <span class="co">Enumerable</span>::<span class="co">Enumerator</span> : <span class="co">Enumerator</span>)
|
456
|
+
enumerator.to_a.should == [[<span class="i">1</span>,<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>], [<span class="i">2</span>,<span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>], [<span class="i">3</span>,<span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span>], [<span class="pc">nil</span>, <span class="s"><span class="dl">'</span><span class="k">d</span><span class="dl">'</span></span>]]
|
457
|
+
<span class="r">end</span>
|
458
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_iterate_source">(<a href="javascript:show('iterate_source')">show</a>)</span>
|
459
|
+
<pre class="source" id="iterate_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
460
|
+
|
461
|
+
<span class="r">def</span> <span class="fu">iterate</span>(*params)
|
462
|
+
<span class="c"># params.shift.zip(*params).each{ |*elements| yield *elements }</span>
|
463
|
+
raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">"</span><span class="k">wrong number of arguments (0)</span><span class="dl">"</span></span> <span class="r">if</span> params.empty?
|
464
|
+
|
465
|
+
first = params.shift
|
466
|
+
<span class="r">if</span> params.empty? <span class="c"># single param - like each</span>
|
467
|
+
<span class="r">if</span> block_given?
|
468
|
+
first.map{|e| <span class="r">yield</span> e }
|
469
|
+
<span class="r">else</span>
|
470
|
+
first.map.to_enum
|
471
|
+
<span class="r">end</span>
|
472
|
+
<span class="r">else</span> <span class="c"># multiple params</span>
|
473
|
+
max_size = [first, *params].max_by(&<span class="sy">:count</span>).size
|
474
|
+
padded_first = first.to_a + [<span class="pc">nil</span>]*(max_size - first.count) <span class="c"># append nils</span>
|
475
|
+
obj = padded_first.zip *params
|
476
|
+
<span class="r">if</span> block_given?
|
477
|
+
obj.map{|es| <span class="r">yield</span> *es }
|
478
|
+
<span class="r">else</span>
|
479
|
+
obj.map.to_enum
|
480
|
+
<span class="r">end</span>
|
481
|
+
<span class="r">end</span>
|
482
|
+
<span class="r">end</span>
|
483
|
+
|
484
|
+
<span class="c"># J-_-L</span></pre> </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>
|
485
|
+
<table class="cube_table"
|
486
|
+
id="kernel_cube"
|
487
|
+
title="require 'zucker/kernel'"> <tr><th>Summary</th> <td>Some useful general shortcut methods.</td></tr><tr><th>Why?</th> <td>Readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>activate_warnings!</h5><pre class="usage source" style="display:block">activate_warnings! <span class="c"># sets $VERBOSE to 1</span></pre><h5>deactivate_warnings!</h5><pre class="usage source" style="display:block">deactivate_warnings! <span class="c"># sets $VERBOSE to 0</span></pre><h5>executed_directly?, standalone?</h5><pre class="usage source" style="display:block">executed_directly? <span class="c"># checks, if the current file is run directly -> true</span></pre><h5>library?</h5><pre class="usage source" style="display:block">library? <span class="c"># checks, if the current file is run directly -> false</span></pre><h5>irb?</h5><pre class="usage source" style="display:block">irb? <span class="c"># returns false if not called from IRB</span></pre><h5>ignore_sigint!</h5><pre class="usage source" style="display:block">ignore_sigint! <span class="c"># blocks CTRL+C</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_kernel_spec">(<a href="javascript:show('kernel_spec')">show</a>)</span>
|
488
|
+
<pre class="source" id="kernel_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/kernel</span><span class="dl">'</span></span>
|
489
|
+
|
490
|
+
describe <span class="s"><span class="dl">'</span><span class="k">activate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
491
|
+
it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to true</span><span class="dl">'</span></span> <span class="r">do</span>
|
492
|
+
activate_warnings!
|
493
|
+
<span class="gv">$VERBOSE</span>.should == <span class="pc">true</span>
|
494
|
+
<span class="r">end</span>
|
495
|
+
<span class="r">end</span>
|
496
|
+
|
497
|
+
describe <span class="s"><span class="dl">'</span><span class="k">deactivate_warnings!</span><span class="dl">'</span></span> <span class="r">do</span>
|
498
|
+
it <span class="s"><span class="dl">'</span><span class="k">should set $VERBOSE to false</span><span class="dl">'</span></span> <span class="r">do</span>
|
499
|
+
deactivate_warnings!
|
500
|
+
<span class="gv">$VERBOSE</span>.should == <span class="pc">false</span>
|
501
|
+
<span class="r">end</span>
|
502
|
+
<span class="r">end</span>
|
503
|
+
|
504
|
+
describe <span class="s"><span class="dl">'</span><span class="k">library?</span><span class="dl">'</span></span> <span class="r">do</span>
|
505
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return false if the file is invoked directly</span><span class="dl">'</span></span> <span class="r">do</span>
|
506
|
+
library?.should == ( <span class="pc">__FILE__</span> != <span class="gv">$PROGRAM_NAME</span> )
|
507
|
+
<span class="r">end</span>
|
508
|
+
<span class="r">end</span>
|
509
|
+
|
510
|
+
describe <span class="s"><span class="dl">'</span><span class="k">executed_directly?</span><span class="dl">'</span></span> <span class="r">do</span>
|
511
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if the file is invoked directly</span><span class="dl">'</span></span> <span class="r">do</span>
|
512
|
+
executed_directly?.should == ( <span class="pc">__FILE__</span> == <span class="gv">$PROGRAM_NAME</span> )
|
513
|
+
<span class="r">end</span>
|
514
|
+
<span class="r">end</span>
|
515
|
+
|
516
|
+
describe <span class="s"><span class="dl">'</span><span class="k">irb?</span><span class="dl">'</span></span> <span class="r">do</span>
|
517
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if called in IRB</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
518
|
+
<span class="r">end</span>
|
519
|
+
|
520
|
+
describe <span class="s"><span class="dl">'</span><span class="k">ignore_sigint!</span><span class="dl">'</span></span> <span class="r">do</span>
|
521
|
+
it <span class="s"><span class="dl">'</span><span class="k">should catch ctrl+c signals</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
522
|
+
<span class="r">end</span>
|
523
|
+
|
524
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_kernel_source">(<a href="javascript:show('kernel_source')">show</a>)</span>
|
525
|
+
<pre class="source" id="kernel_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
526
|
+
|
527
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
528
|
+
private
|
529
|
+
|
530
|
+
<span class="r">def</span> <span class="fu">activate_warnings!</span>
|
531
|
+
<span class="gv">$VERBOSE</span> = <span class="pc">true</span>
|
532
|
+
<span class="r">end</span>
|
533
|
+
|
534
|
+
<span class="r">def</span> <span class="fu">deactivate_warnings!</span>
|
535
|
+
<span class="gv">$VERBOSE</span> = <span class="pc">false</span>
|
536
|
+
<span class="r">end</span>
|
537
|
+
|
538
|
+
<span class="r">def</span> <span class="fu">library?</span>
|
539
|
+
caller[<span class="i">0</span>].rindex(<span class="rx"><span class="dl">/</span><span class="k">:</span><span class="ch">\d</span><span class="k">+(:in `.*')?$</span><span class="dl">/</span></span>)
|
540
|
+
<span class="gv">$PROGRAM_NAME</span> != <span class="gv">$`</span> <span class="c"># __FILE__</span>
|
541
|
+
<span class="r">end</span>
|
542
|
+
|
543
|
+
<span class="r">def</span> <span class="fu">executed_directly?</span>
|
544
|
+
caller[<span class="i">0</span>].rindex(<span class="rx"><span class="dl">/</span><span class="k">:</span><span class="ch">\d</span><span class="k">+(:in `.*')?$</span><span class="dl">/</span></span>)
|
545
|
+
<span class="gv">$PROGRAM_NAME</span> == <span class="gv">$`</span> <span class="c"># __FILE__</span>
|
546
|
+
<span class="r">end</span>
|
547
|
+
<span class="r">alias</span> <span class="fu">standalone?</span> <span class="fu">executed_directly?</span>
|
548
|
+
<span class="r">alias</span> <span class="fu">directly_executed?</span> <span class="fu">executed_directly?</span>
|
549
|
+
|
550
|
+
<span class="r">def</span> <span class="fu">irb?</span>
|
551
|
+
(<span class="r">defined?</span>(<span class="co">IRB</span>) && <span class="gv">$0</span> =~ <span class="rx"><span class="dl">/</span><span class="k">irb</span><span class="dl">/</span></span>) || (<span class="r">defined?</span>(<span class="co">Ripl</span>) && <span class="gv">$0</span> =~ <span class="rx"><span class="dl">/</span><span class="k">ripl</span><span class="dl">/</span></span>)
|
552
|
+
<span class="r">end</span>
|
553
|
+
|
554
|
+
<span class="r">def</span> <span class="fu">ignore_sigint!</span> <span class="c"># ctrl+c</span>
|
555
|
+
<span class="co">Signal</span>.trap *<span class="s"><span class="dl">%w|</span><span class="k">SIGINT IGNORE</span><span class="dl">|</span></span>
|
556
|
+
<span class="pc">true</span>
|
557
|
+
<span class="r">end</span>
|
558
|
+
<span class="r">end</span>
|
559
|
+
|
560
|
+
<span class="c"># J-_-L</span></pre> </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>
|
561
|
+
<table class="cube_table"
|
562
|
+
id="sandbox_cube"
|
563
|
+
title="require 'zucker/sandbox'"> <tr><th>Summary</th> <td>Creates a sandbox area.</td></tr><tr><th>Why?</th> <td>Ruby comes with sandboxes, but they are hidden (-> integrated) in the <code>$SAFE</code> concept.</td></tr><tr><th>Methods/Usage</th> <td><h5>sandbox</h5><pre class="usage source" style="display:block">sandbox <span class="r">do</span>
|
564
|
+
<span class="c"># dangerous commands throw SecurityErrors ($SAFE=4)</span>
|
565
|
+
<span class="r">end</span>
|
566
|
+
<span class="c"># everything's normal again</span>
|
567
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_sandbox_spec">(<a href="javascript:show('sandbox_spec')">show</a>)</span>
|
568
|
+
<pre class="source" id="sandbox_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/sandbox</span><span class="dl">'</span></span>
|
569
|
+
|
570
|
+
describe <span class="s"><span class="dl">'</span><span class="k">sandbox</span><span class="dl">'</span></span> <span class="r">do</span>
|
571
|
+
it <span class="s"><span class="dl">'</span><span class="k">should throw a SecurityError if bad commands are issued</span><span class="dl">'</span></span> <span class="r">do</span>
|
572
|
+
proc <span class="r">do</span>
|
573
|
+
sandbox <span class="r">do</span>
|
574
|
+
<span class="sh"><span class="dl">`</span><span class="k">ls</span><span class="dl">`</span></span>
|
575
|
+
<span class="r">end</span>
|
576
|
+
<span class="r">end</span>.should raise_exception <span class="co">SecurityError</span>
|
577
|
+
<span class="r">end</span>
|
578
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_sandbox_source">(<a href="javascript:show('sandbox_source')">show</a>)</span>
|
579
|
+
<pre class="source" id="sandbox_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
580
|
+
|
581
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
582
|
+
private
|
583
|
+
|
584
|
+
<span class="r">def</span> <span class="fu">sandbox</span>
|
585
|
+
<span class="co">Thread</span>.start <span class="r">do</span>
|
586
|
+
<span class="gv">$SAFE</span> = <span class="i">4</span>
|
587
|
+
<span class="r">yield</span>
|
588
|
+
<span class="r">end</span>.value
|
589
|
+
<span class="r">end</span>
|
590
|
+
<span class="r">end</span>
|
591
|
+
|
592
|
+
<span class="c"># J-_-L</span></pre> </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/sandbox">github wiki</a></td></tr></table> <h3 title="require 'zucker/tap'" id="tap">tap</h3>
|
593
|
+
<table class="cube_table"
|
594
|
+
id="tap_cube"
|
595
|
+
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><pre class="usage source" style="display:block">tap_on [<span class="i">1</span>,<span class="i">2</span>] <span class="r">do</span> |obj|
|
596
|
+
obj[<span class="i">4</span>] = <span class="i">5</span>
|
597
|
+
<span class="r">end</span> <span class="c">#=> [1, 2, nil, nil, 5]</span>
|
598
|
+
</pre><h5>make_new</h5><pre class="usage source" style="display:block">make_new <span class="co">Hash</span> <span class="r">do</span> |obj|
|
599
|
+
obj[<span class="i">1</span>] = <span class="i">2</span>
|
600
|
+
<span class="r">end</span> <span class="c">#=> {1 => 2}</span>
|
601
|
+
</pre></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>
|
602
|
+
<pre class="source" id="tap_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/tap</span><span class="dl">'</span></span>
|
603
|
+
|
604
|
+
describe <span class="s"><span class="dl">'</span><span class="k">tap_on</span><span class="dl">'</span></span> <span class="r">do</span>
|
605
|
+
it <span class="s"><span class="dl">'</span><span class="k">should call tap on the argument and apply the block</span><span class="dl">'</span></span> <span class="r">do</span>
|
606
|
+
obj = <span class="s"><span class="dl">"</span><span class="k">an_object</span><span class="dl">"</span></span>
|
607
|
+
block = <span class="sy">:reverse</span>.to_proc
|
608
|
+
|
609
|
+
tap_on( obj, &block ).should == obj.tap( &block )
|
610
|
+
<span class="r">end</span>
|
611
|
+
<span class="r">end</span>
|
612
|
+
|
613
|
+
describe <span class="s"><span class="dl">'</span><span class="k">make_new</span><span class="dl">'</span></span> <span class="r">do</span>
|
614
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create a new instance of the class given as argument, apply the block on it and return result</span><span class="dl">'</span></span> <span class="r">do</span>
|
615
|
+
make_new <span class="co">Hash</span> <span class="r">do</span> |obj|
|
616
|
+
obj[<span class="i">1</span>] = <span class="i">2</span>
|
617
|
+
<span class="r">end</span>.should.send(<span class="sy"><span class="sy">:</span><span class="dl">'</span><span class="k">==</span><span class="dl">'</span></span>, { <span class="i">1</span> => <span class="i">2</span>})
|
618
|
+
<span class="r">end</span>
|
619
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_tap_source">(<a href="javascript:show('tap_source')">show</a>)</span>
|
620
|
+
<pre class="source" id="tap_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
621
|
+
|
622
|
+
<span class="r">def</span> <span class="fu">tap_on</span>(obj, &block)
|
623
|
+
obj.tap &block
|
624
|
+
<span class="r">end</span>
|
625
|
+
|
626
|
+
<span class="r">def</span> <span class="fu">make_new</span>(what, *args, &block)
|
627
|
+
what.new(*args).tap &block
|
628
|
+
<span class="r">end</span>
|
629
|
+
|
630
|
+
<span class="c"># J-_-L</span></pre> </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>
|
631
|
+
</div>
|
632
|
+
<h2 title="require 'zucker/env'" id="env">Cubes[env]</h2>
|
633
|
+
|
634
|
+
<div class="cubes">
|
635
|
+
<h3 title="require 'zucker/engine'" id="engine">engine</h3>
|
636
|
+
<table class="cube_table"
|
637
|
+
id="engine_cube"
|
638
|
+
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><pre class="usage source" style="display:block"><span class="c"># outputs the interpreter name</span></pre><h5>RubyEngine.mri?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.mri? <span class="c"># true if using the official Ruby interpreter</span></pre><h5>RubyEngine.jruby?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.jruby? <span class="c"># true if using JRuby</span></pre><h5>RubyEngine.rubinius?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.rubinius? <span class="c"># true if using rubinius</span></pre><h5>RubyEngine.ree?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.ree? <span class="c"># true if using enterprise edition</span></pre><h5>RubyEngine.ironruby?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.ironruby? <span class="c"># true if using IronRuby</span></pre><h5>RubyEngine.macruby?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.macruby? <span class="c"># true if using MacRuby</span></pre><h5>RubyEngine.cardinal?</h5><pre class="usage source" style="display:block"><span class="co">RubyEngine</span>.cardinal? <span class="c"># true if using parrot</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_engine_spec">(<a href="javascript:show('engine_spec')">show</a>)</span>
|
639
|
+
<pre class="source" id="engine_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/engine</span><span class="dl">'</span></span>
|
640
|
+
|
641
|
+
describe <span class="s"><span class="dl">'</span><span class="k">RubyEngine</span><span class="dl">'</span></span> <span class="r">do</span>
|
642
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display the current ruby interpreter (to_s)</span><span class="dl">'</span></span> <span class="r">do</span>
|
643
|
+
<span class="co">RubyEngine</span>
|
644
|
+
<span class="r">end</span>
|
645
|
+
|
646
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create many method aliases for asking for a specific interpreter</span><span class="dl">'</span></span> <span class="r">do</span>
|
647
|
+
<span class="c"># please see the source or description file</span>
|
648
|
+
<span class="r">end</span>
|
649
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_engine_source">(<a href="javascript:show('engine_source')">show</a>)</span>
|
650
|
+
<pre class="source" id="engine_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
651
|
+
|
652
|
+
<span class="r">module</span> <span class="cl">RubyEngine</span>
|
653
|
+
<span class="c"># try to guess the interpreter</span>
|
654
|
+
<span class="iv">@interpreter</span> = <span class="r">case</span>
|
655
|
+
<span class="r">when</span> <span class="co">RUBY_PLATFORM</span> == <span class="s"><span class="dl">'</span><span class="k">parrot</span><span class="dl">'</span></span>
|
656
|
+
<span class="s"><span class="dl">'</span><span class="k">cardinal</span><span class="dl">'</span></span>
|
657
|
+
<span class="r">when</span> <span class="co">Object</span>.const_defined?(<span class="sy">:RUBY_ENGINE</span>)
|
658
|
+
<span class="r">if</span> <span class="co">RUBY_ENGINE</span> == <span class="s"><span class="dl">'</span><span class="k">ruby</span><span class="dl">'</span></span>
|
659
|
+
<span class="r">if</span> <span class="co">RUBY_DESCRIPTION</span> =~ <span class="rx"><span class="dl">/</span><span class="k">Enterprise</span><span class="dl">/</span></span>
|
660
|
+
<span class="s"><span class="dl">'</span><span class="k">ree</span><span class="dl">'</span></span>
|
661
|
+
<span class="r">else</span>
|
662
|
+
<span class="s"><span class="dl">'</span><span class="k">mri</span><span class="dl">'</span></span>
|
663
|
+
<span class="r">end</span>
|
664
|
+
<span class="r">else</span>
|
665
|
+
<span class="co">RUBY_ENGINE</span>.to_s <span class="c"># jruby, rbx, ironruby, macruby, etc.</span>
|
666
|
+
<span class="r">end</span>
|
667
|
+
<span class="r">else</span> <span class="c"># probably 1.8</span>
|
668
|
+
<span class="s"><span class="dl">'</span><span class="k">mri</span><span class="dl">'</span></span>
|
669
|
+
<span class="r">end</span>
|
670
|
+
|
671
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
672
|
+
<span class="r">def</span> <span class="fu">is?</span>(what)
|
673
|
+
what === <span class="iv">@interpreter</span>
|
674
|
+
<span class="r">end</span>
|
675
|
+
<span class="r">alias</span> <span class="fu">is</span> <span class="fu">is?</span>
|
676
|
+
|
677
|
+
<span class="r">def</span> <span class="fu">to_s</span>
|
678
|
+
<span class="iv">@interpreter</span>.to_s
|
679
|
+
<span class="r">end</span>
|
680
|
+
|
681
|
+
<span class="c"># ask methods</span>
|
682
|
+
|
683
|
+
<span class="r">def</span> <span class="fu">mri?</span>
|
684
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">mri</span><span class="dl">'</span></span>
|
685
|
+
<span class="r">end</span>
|
686
|
+
<span class="r">alias</span> <span class="fu">official_ruby?</span> <span class="fu">mri?</span>
|
687
|
+
<span class="r">alias</span> <span class="fu">ruby?</span> <span class="fu">mri?</span>
|
688
|
+
|
689
|
+
<span class="r">def</span> <span class="fu">jruby?</span>
|
690
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">jruby</span><span class="dl">'</span></span>
|
691
|
+
<span class="r">end</span>
|
692
|
+
<span class="r">alias</span> <span class="fu">java?</span> <span class="fu">jruby?</span>
|
693
|
+
|
694
|
+
<span class="r">def</span> <span class="fu">rubinius?</span>
|
695
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">rbx</span><span class="dl">'</span></span>
|
696
|
+
<span class="r">end</span>
|
697
|
+
<span class="r">alias</span> <span class="fu">rbx?</span> <span class="fu">rubinius?</span>
|
698
|
+
|
699
|
+
<span class="r">def</span> <span class="fu">ree?</span>
|
700
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">ree</span><span class="dl">'</span></span>
|
701
|
+
<span class="r">end</span>
|
702
|
+
<span class="r">alias</span> <span class="fu">enterprise?</span> <span class="fu">ree?</span>
|
703
|
+
|
704
|
+
<span class="r">def</span> <span class="fu">ironruby?</span>
|
705
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">ironruby</span><span class="dl">'</span></span>
|
706
|
+
<span class="r">end</span>
|
707
|
+
<span class="r">alias</span> <span class="fu">iron_ruby?</span> <span class="fu">ironruby?</span>
|
708
|
+
|
709
|
+
<span class="r">def</span> <span class="fu">cardinal?</span>
|
710
|
+
<span class="co">RubyEngine</span>.is? <span class="s"><span class="dl">'</span><span class="k">cardinal</span><span class="dl">'</span></span>
|
711
|
+
<span class="r">end</span>
|
712
|
+
<span class="r">alias</span> <span class="fu">parrot?</span> <span class="fu">cardinal?</span>
|
713
|
+
<span class="r">alias</span> <span class="fu">perl?</span> <span class="fu">cardinal?</span>
|
714
|
+
<span class="r">end</span>
|
715
|
+
<span class="r">end</span>
|
716
|
+
|
717
|
+
<span class="c"># J-_-L</span></pre> </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>
|
718
|
+
<table class="cube_table"
|
719
|
+
id="info_cube"
|
720
|
+
title="require 'zucker/info'"> <tr><th>Summary</th> <td>Access environment information with the <code>Info</code> module.</td></tr><tr><th>Why?</th> <td>You don't need to remember in which global variable, constant or special method the information you are searching for is hidden.</td></tr><tr><th>Methods/Usage</th> <td><h5>Info</h5><pre class="usage source" style="display:block"><span class="c"># for example</span>
|
721
|
+
<span class="co">Info</span>.working_directory <span class="c"># Dir.pwd</span>
|
722
|
+
<span class="co">Info</span>.load_path <span class="c"># $:</span>
|
723
|
+
<span class="co">Info</span>.platform <span class="c"># RUBY_PLATFORM</span>
|
724
|
+
<span class="co">Info</span>.current_file <span class="c"># similar to __FILE__</span>
|
725
|
+
<span class="c"># you could also add them to the global namespace with: include Info</span>
|
726
|
+
<span class="c"># see the source file for the list of accessors or</span>
|
727
|
+
<span class="co">Info</span>.list
|
728
|
+
</pre></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>
|
729
|
+
<pre class="source" id="info_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/info</span><span class="dl">'</span></span>
|
730
|
+
|
731
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Info</span><span class="dl">'</span></span> <span class="r">do</span>
|
732
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define accessors for global variables, constants and some special methods/keywords</span><span class="dl">'</span></span> <span class="r">do</span>
|
733
|
+
<span class="c"># see sourcefile for the list</span>
|
734
|
+
<span class="r">end</span>
|
735
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_info_source">(<a href="javascript:show('info_source')">show</a>)</span>
|
736
|
+
<pre class="source" id="info_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
737
|
+
require <span class="s"><span class="dl">'</span><span class="k">rbconfig</span><span class="dl">'</span></span>
|
738
|
+
require <span class="s"><span class="dl">'</span><span class="k">etc</span><span class="dl">'</span></span>
|
739
|
+
|
740
|
+
<span class="r">module</span> <span class="cl">Info</span>
|
741
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
742
|
+
<span class="c"># hash like access</span>
|
743
|
+
<span class="r">def</span> <span class="fu">[]</span>(what)
|
744
|
+
send what
|
745
|
+
<span class="r">end</span>
|
746
|
+
|
747
|
+
<span class="c"># list available info methods</span>
|
748
|
+
<span class="r">def</span> <span class="fu">list</span>
|
749
|
+
singleton_methods - [<span class="sy">:[]</span>, <span class="sy">:list</span>, <span class="s"><span class="dl">'</span><span class="k">[]</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k">list</span><span class="dl">'</span></span>]
|
750
|
+
<span class="r">end</span>
|
751
|
+
<span class="r">end</span>
|
752
|
+
|
753
|
+
module_function
|
754
|
+
|
755
|
+
<span class="c"># input</span>
|
756
|
+
<span class="r">def</span> <span class="fu">last_input_file</span>
|
757
|
+
<span class="gv">$FILENAME</span>
|
758
|
+
<span class="r">end</span>
|
759
|
+
|
760
|
+
<span class="r">def</span> <span class="fu">last_input_line_number</span>
|
761
|
+
<span class="gv">$.</span>
|
762
|
+
<span class="r">end</span>
|
763
|
+
|
764
|
+
<span class="r">def</span> <span class="fu">last_input</span>
|
765
|
+
<span class="gv">$_</span>
|
766
|
+
<span class="r">end</span>
|
767
|
+
|
768
|
+
<span class="c"># program</span>
|
769
|
+
<span class="r">def</span> <span class="fu">program_name</span>
|
770
|
+
<span class="gv">$0</span>
|
771
|
+
<span class="r">end</span>
|
772
|
+
|
773
|
+
<span class="r">def</span> <span class="fu">program_arguments</span>
|
774
|
+
<span class="gv">$:</span>
|
775
|
+
<span class="r">end</span>
|
776
|
+
|
777
|
+
<span class="r">def</span> <span class="fu">loaded_programs</span>
|
778
|
+
<span class="gv">$"</span>
|
779
|
+
<span class="r">end</span>
|
780
|
+
|
781
|
+
<span class="r">def</span> <span class="fu">program_data</span>
|
782
|
+
::<span class="co">DATA</span>
|
783
|
+
<span class="r">end</span>
|
784
|
+
|
785
|
+
<span class="r">def</span> <span class="fu">child_program_status</span>
|
786
|
+
<span class="gv">$CHILD_STATUS</span>
|
787
|
+
<span class="r">end</span>
|
788
|
+
|
789
|
+
<span class="c"># system info</span>
|
790
|
+
<span class="r">def</span> <span class="fu">environment</span>
|
791
|
+
::<span class="co">ENV</span>
|
792
|
+
<span class="r">end</span>
|
793
|
+
<span class="r">alias</span> <span class="fu">env</span> <span class="fu">environment</span>
|
794
|
+
|
795
|
+
<span class="r">def</span> <span class="fu">working_directory</span>
|
796
|
+
<span class="co">Dir</span>.pwd
|
797
|
+
<span class="r">end</span>
|
798
|
+
|
799
|
+
<span class="r">def</span> <span class="fu">platform</span>
|
800
|
+
::<span class="co">RUBY_PLATFORM</span>
|
801
|
+
<span class="r">end</span>
|
802
|
+
|
803
|
+
<span class="r">def</span> <span class="fu">os</span>
|
804
|
+
<span class="co">RbConfig</span>::<span class="co">CONFIG</span>[<span class="s"><span class="dl">'</span><span class="k">host_os</span><span class="dl">'</span></span>]
|
805
|
+
<span class="r">end</span>
|
806
|
+
|
807
|
+
<span class="r">def</span> <span class="fu">process_id</span>
|
808
|
+
<span class="gv">$$</span>
|
809
|
+
<span class="r">end</span>
|
810
|
+
|
811
|
+
<span class="r">def</span> <span class="fu">load_path</span>
|
812
|
+
<span class="gv">$:</span>
|
813
|
+
<span class="r">end</span>
|
814
|
+
|
815
|
+
<span class="c"># user</span>
|
816
|
+
<span class="r">def</span> <span class="fu">user_login</span>
|
817
|
+
<span class="co">Etc</span>.getlogin
|
818
|
+
<span class="r">end</span>
|
819
|
+
|
820
|
+
<span class="r">def</span> <span class="fu">user_name</span>
|
821
|
+
<span class="co">Etc</span>.getpwnam(user_login).gecos.split(<span class="s"><span class="dl">'</span><span class="k">,</span><span class="dl">'</span></span>)[<span class="i">0</span>]
|
822
|
+
<span class="r">end</span>
|
823
|
+
|
824
|
+
<span class="c"># current</span>
|
825
|
+
|
826
|
+
<span class="r">def</span> <span class="fu">current_file</span> <span class="c"># __FILE__</span>
|
827
|
+
<span class="r">return</span> <span class="gv">$`</span> <span class="r">if</span> caller[<span class="i">0</span>].rindex(<span class="rx"><span class="dl">/</span><span class="k">:</span><span class="ch">\d</span><span class="k">+(:in `.*')?$</span><span class="dl">/</span></span>)
|
828
|
+
<span class="r">end</span>
|
829
|
+
|
830
|
+
<span class="r">def</span> <span class="fu">current_file_directory</span>
|
831
|
+
<span class="r">if</span> current_file[<span class="i">0</span>,<span class="i">1</span>] == <span class="s"><span class="dl">'</span><span class="k">(</span><span class="dl">'</span></span> && current_file[<span class="i">-1</span>,<span class="i">1</span>] == <span class="s"><span class="dl">'</span><span class="k">)</span><span class="dl">'</span></span>
|
832
|
+
current_file
|
833
|
+
<span class="r">else</span>
|
834
|
+
<span class="co">File</span>.dirname(current_file)
|
835
|
+
<span class="r">end</span>
|
836
|
+
<span class="r">end</span>
|
837
|
+
|
838
|
+
<span class="r">def</span> <span class="fu">current_line</span> <span class="c"># __LINE__</span>
|
839
|
+
<span class="r">return</span> <span class="gv">$1</span>.to_i <span class="r">if</span> caller[<span class="i">0</span>].rindex( <span class="rx"><span class="dl">/</span><span class="k">:(</span><span class="ch">\d</span><span class="k">+)(:in `.*')?$</span><span class="dl">/</span></span> )
|
840
|
+
<span class="r">end</span>
|
841
|
+
|
842
|
+
<span class="r">def</span> <span class="fu">current_method</span> <span class="c"># __method__ (except aliases)</span>
|
843
|
+
<span class="r">return</span> <span class="gv">$1</span>.to_sym <span class="r">if</span> caller(<span class="i">1</span>)[<span class="i">0</span>].rindex( <span class="rx"><span class="dl">/</span><span class="ch">\`</span><span class="k">([^</span><span class="ch">\'</span><span class="k">]+)</span><span class="ch">\'</span><span class="dl">/</span></span> )
|
844
|
+
<span class="r">end</span>
|
845
|
+
|
846
|
+
<span class="r">def</span> <span class="fu">current_callstack</span>
|
847
|
+
caller
|
848
|
+
<span class="r">end</span>
|
849
|
+
|
850
|
+
<span class="c"># dealing with strings</span>
|
851
|
+
<span class="r">def</span> <span class="fu">gets_separator</span>
|
852
|
+
<span class="gv">$/</span>
|
853
|
+
<span class="r">end</span>
|
854
|
+
|
855
|
+
<span class="r">def</span> <span class="fu">join_separator</span>
|
856
|
+
<span class="gv">$,</span>
|
857
|
+
<span class="r">end</span>
|
858
|
+
|
859
|
+
<span class="r">def</span> <span class="fu">print_separator</span>
|
860
|
+
<span class="gv">$,</span>
|
861
|
+
<span class="r">end</span>
|
862
|
+
|
863
|
+
<span class="r">def</span> <span class="fu">split_separator</span>
|
864
|
+
<span class="gv">$;</span>
|
865
|
+
<span class="r">end</span>
|
866
|
+
|
867
|
+
<span class="c"># misc</span>
|
868
|
+
<span class="r">def</span> <span class="fu">security_level</span>
|
869
|
+
<span class="gv">$SAFE</span>
|
870
|
+
<span class="r">end</span>
|
871
|
+
|
872
|
+
<span class="r">def</span> <span class="fu">warnings_activated?</span>
|
873
|
+
<span class="gv">$VERBOSE</span>
|
874
|
+
<span class="r">end</span>
|
875
|
+
|
876
|
+
<span class="r">def</span> <span class="fu">debug_activated?</span>
|
877
|
+
<span class="gv">$DEBUG</span>
|
878
|
+
<span class="r">end</span>
|
879
|
+
|
880
|
+
<span class="r">def</span> <span class="fu">last_exception</span>
|
881
|
+
<span class="gv">$!</span>
|
882
|
+
<span class="r">end</span>
|
883
|
+
|
884
|
+
<span class="c"># defined objects</span>
|
885
|
+
<span class="r">def</span> <span class="fu">global_variables</span>
|
886
|
+
<span class="co">Object</span>.send <span class="sy">:global_variables</span>
|
887
|
+
<span class="r">end</span>
|
888
|
+
|
889
|
+
<span class="r">def</span> <span class="fu">global_constants</span>
|
890
|
+
<span class="co">Object</span>.constants
|
891
|
+
<span class="r">end</span>
|
892
|
+
|
893
|
+
<span class="c"># encoding (1.9)</span>
|
894
|
+
<span class="c">#def source_encoding</span>
|
895
|
+
<span class="c"># __ENCODING__</span>
|
896
|
+
<span class="c">#end</span>
|
897
|
+
|
898
|
+
<span class="r">def</span> <span class="fu">external_encoding</span>
|
899
|
+
<span class="co">Encoding</span>.default_external
|
900
|
+
<span class="r">end</span>
|
901
|
+
|
902
|
+
<span class="r">def</span> <span class="fu">internal_encoding</span>
|
903
|
+
<span class="co">Encoding</span>.default_internal
|
904
|
+
<span class="r">end</span>
|
905
|
+
|
906
|
+
<span class="c"># ruby version info</span>
|
907
|
+
<span class="r">def</span> <span class="fu">ruby_version</span> <span class="c"># also see the RubyVersion cube</span>
|
908
|
+
::<span class="co">RUBY_VERSION</span>
|
909
|
+
<span class="r">end</span>
|
910
|
+
|
911
|
+
<span class="r">def</span> <span class="fu">ruby_patchlevel</span>
|
912
|
+
::<span class="co">RUBY_PATCHLEVEL</span>
|
913
|
+
<span class="r">end</span>
|
914
|
+
|
915
|
+
<span class="r">def</span> <span class="fu">ruby_description</span>
|
916
|
+
::<span class="co">RUBY_DESCRIPTION</span>
|
917
|
+
<span class="r">end</span>
|
918
|
+
|
919
|
+
<span class="r">def</span> <span class="fu">ruby_release_date</span>
|
920
|
+
::<span class="co">RUBY_RELEASE_DATE</span>
|
921
|
+
<span class="r">end</span>
|
922
|
+
|
923
|
+
<span class="r">def</span> <span class="fu">ruby_engine</span> <span class="c"># warning! not support by every implementation. It's saver to use the RubyEngine cube</span>
|
924
|
+
::<span class="co">RUBY_ENGINE</span>
|
925
|
+
<span class="r">end</span>
|
926
|
+
<span class="r">end</span></pre> </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>
|
927
|
+
<table class="cube_table"
|
928
|
+
id="os_cube"
|
929
|
+
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><pre class="usage source" style="display:block"><span class="c"># displays the operating system</span></pre><h5>OS.linux?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.linux? <span class="c"># true if you are on a linux machine</span></pre><h5>OS.mac?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.mac? <span class="c"># true for macs</span></pre><h5>OS.bsd?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.bsd? <span class="c"># true for bsds</span></pre><h5>OS.windows?</h5><pre class="usage source" style="display:block">include <span class="co">OS</span>; windows? <span class="c"># true for windows. Example of including OS to get the os methods into the global namespace</span></pre><h5>OS.solaris?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.solaris? <span class="c"># true for solaris/sunos</span></pre><h5>OS.posix?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.posix? <span class="c"># true if system is posix compatible</span></pre><h5>OS.is?</h5><pre class="usage source" style="display:block"><span class="co">OS</span>.is? <span class="rx"><span class="dl">/</span><span class="k">x</span><span class="dl">/</span></span> <span class="c"># matches the regex against the os string</span></pre></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>
|
930
|
+
<pre class="source" id="os_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/os</span><span class="dl">'</span></span>
|
931
|
+
|
932
|
+
describe <span class="s"><span class="dl">'</span><span class="k">OS</span><span class="dl">'</span></span> <span class="r">do</span>
|
933
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display the used operation system (to_s)</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
934
|
+
<span class="r">end</span>
|
935
|
+
|
936
|
+
describe <span class="s"><span class="dl">'</span><span class="k">linux?</span><span class="dl">'</span></span> <span class="r">do</span>
|
937
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is a linux</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
938
|
+
<span class="r">end</span>
|
939
|
+
|
940
|
+
describe <span class="s"><span class="dl">'</span><span class="k">mac?</span><span class="dl">'</span></span> <span class="r">do</span>
|
941
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is a mac or darwin</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
942
|
+
<span class="r">end</span>
|
943
|
+
|
944
|
+
describe <span class="s"><span class="dl">'</span><span class="k">bsd?</span><span class="dl">'</span></span> <span class="r">do</span>
|
945
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is a bsd</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
946
|
+
<span class="r">end</span>
|
947
|
+
|
948
|
+
describe <span class="s"><span class="dl">'</span><span class="k">windows?</span><span class="dl">'</span></span> <span class="r">do</span>
|
949
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is a windows</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
950
|
+
<span class="r">end</span>
|
951
|
+
|
952
|
+
describe <span class="s"><span class="dl">'</span><span class="k">solaris?</span><span class="dl">'</span></span> <span class="r">do</span>
|
953
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is a solaris or sunos</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
954
|
+
<span class="r">end</span>
|
955
|
+
|
956
|
+
describe <span class="s"><span class="dl">'</span><span class="k">posix?</span><span class="dl">'</span></span> <span class="r">do</span>
|
957
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return true if OS is posix compatible</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
958
|
+
<span class="r">end</span>
|
959
|
+
|
960
|
+
describe <span class="s"><span class="dl">'</span><span class="k">OS.is?</span><span class="dl">'</span></span> <span class="r">do</span>
|
961
|
+
it <span class="s"><span class="dl">"</span><span class="k">should test if RbConfig::CONFIG['host_os'] matches the regex (boolean value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
962
|
+
<span class="co">OS</span>.is?(<span class="rx"><span class="dl">/</span><span class="k">linux</span><span class="dl">/</span></span>).should ==
|
963
|
+
!!( <span class="co">RbConfig</span>::<span class="co">CONFIG</span>[<span class="s"><span class="dl">'</span><span class="k">host_os</span><span class="dl">'</span></span>] =~ <span class="rx"><span class="dl">/</span><span class="k">linux</span><span class="dl">/</span></span> )
|
964
|
+
<span class="r">end</span>
|
965
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_os_source">(<a href="javascript:show('os_source')">show</a>)</span>
|
966
|
+
<pre class="source" id="os_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
967
|
+
require <span class="s"><span class="dl">'</span><span class="k">rbconfig</span><span class="dl">'</span></span>
|
968
|
+
|
969
|
+
<span class="r">module</span> <span class="cl">OS</span>
|
970
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
971
|
+
<span class="r">def</span> <span class="fu">is?</span>(what)
|
972
|
+
what === <span class="co">RbConfig</span>::<span class="co">CONFIG</span>[<span class="s"><span class="dl">'</span><span class="k">host_os</span><span class="dl">'</span></span>]
|
973
|
+
<span class="r">end</span>
|
974
|
+
<span class="r">alias</span> <span class="fu">is</span> <span class="fu">is?</span>
|
975
|
+
|
976
|
+
<span class="r">def</span> <span class="fu">to_s</span>
|
977
|
+
<span class="co">RbConfig</span>::<span class="co">CONFIG</span>[<span class="s"><span class="dl">'</span><span class="k">host_os</span><span class="dl">'</span></span>]
|
978
|
+
<span class="r">end</span>
|
979
|
+
<span class="r">end</span>
|
980
|
+
|
981
|
+
module_function
|
982
|
+
|
983
|
+
<span class="r">def</span> <span class="fu">linux?</span>
|
984
|
+
<span class="co">OS</span>.is?( <span class="rx"><span class="dl">/</span><span class="k">linux|cygwin</span><span class="dl">/</span></span> )
|
985
|
+
<span class="r">end</span>
|
986
|
+
|
987
|
+
<span class="r">def</span> <span class="fu">mac?</span>
|
988
|
+
<span class="co">OS</span>.is?( <span class="rx"><span class="dl">/</span><span class="k">mac|darwin</span><span class="dl">/</span></span> )
|
989
|
+
<span class="r">end</span>
|
990
|
+
|
991
|
+
<span class="r">def</span> <span class="fu">bsd?</span>
|
992
|
+
<span class="co">OS</span>.is?( <span class="rx"><span class="dl">/</span><span class="k">bsd</span><span class="dl">/</span></span> )
|
993
|
+
<span class="r">end</span>
|
994
|
+
|
995
|
+
<span class="r">def</span> <span class="fu">windows?</span>
|
996
|
+
<span class="co">OS</span>.is?( <span class="rx"><span class="dl">/</span><span class="k">mswin|mingw</span><span class="dl">/</span></span> )
|
997
|
+
<span class="r">end</span>
|
998
|
+
|
999
|
+
<span class="r">def</span> <span class="fu">solaris?</span>
|
1000
|
+
<span class="co">OS</span>.is?( <span class="rx"><span class="dl">/</span><span class="k">solaris|sunos</span><span class="dl">/</span></span> )
|
1001
|
+
<span class="r">end</span>
|
1002
|
+
|
1003
|
+
<span class="r">def</span> <span class="fu">posix?</span>
|
1004
|
+
linux? <span class="r">or</span> mac? <span class="r">or</span> bsd? <span class="r">or</span> solaris? <span class="r">or</span> <span class="r">begin</span>
|
1005
|
+
fork <span class="r">do</span> <span class="r">end</span>
|
1006
|
+
<span class="pc">true</span>
|
1007
|
+
<span class="r">rescue</span> <span class="co">NotImplementedError</span>, <span class="co">NoMethodError</span>
|
1008
|
+
<span class="pc">false</span>
|
1009
|
+
<span class="r">end</span>
|
1010
|
+
<span class="r">end</span>
|
1011
|
+
|
1012
|
+
<span class="c">#def symbian?</span>
|
1013
|
+
<span class="c">#TODO who knows what symbian returns?</span>
|
1014
|
+
<span class="c">#end</span>
|
1015
|
+
|
1016
|
+
<span class="c"># ...</span>
|
1017
|
+
<span class="r">end</span>
|
1018
|
+
|
1019
|
+
<span class="c"># J-_-L</span></pre> </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> <h3 title="require 'zucker/version'" id="version">version</h3>
|
1020
|
+
<table class="cube_table"
|
1021
|
+
id="version_cube"
|
1022
|
+
title="require 'zucker/version'"> <tr><th>Summary</th> <td>Adds a <code>RubyVersion</code> class for easy compatibility checks.</td></tr><tr><th>Methods/Usage</th> <td><h5>RubyVersion</h5><pre class="usage source" style="display:block"><span class="c"># return RUBY_VERSION</span>
|
1023
|
+
<span class="co">RubyVersion</span>
|
1024
|
+
<span class="c"># check for the main version with a Float</span>
|
1025
|
+
<span class="co">RubyVersion</span>.is? <span class="fl">1.8</span>
|
1026
|
+
<span class="c"># use strings for exacter checking</span>
|
1027
|
+
<span class="co">RubyVersion</span>.is.above <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>
|
1028
|
+
<span class="co">RubyVersion</span>.is.at_least <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span> <span class="c"># or exactly, below, at_most</span>
|
1029
|
+
<span class="c"># you can use the common comparison operators</span>
|
1030
|
+
<span class="co">RubyVersion</span> >= <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>
|
1031
|
+
<span class="co">RubyVersion</span>.between? <span class="s"><span class="dl">'</span><span class="k">1.8.6</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>
|
1032
|
+
<span class="c"># relase date checks</span>
|
1033
|
+
<span class="co">RubyVersion</span>.is.older_than <span class="co">Date</span>.today
|
1034
|
+
<span class="co">RubyVersion</span>.is.newer_than <span class="s"><span class="dl">'</span><span class="k">2009-08-19</span><span class="dl">'</span></span>
|
1035
|
+
<span class="c"># accessors</span>
|
1036
|
+
<span class="co">RubyVersion</span>.major <span class="c"># e.g. => 1</span>
|
1037
|
+
<span class="co">RubyVersion</span>.minor <span class="c"># e.g. => 8</span>
|
1038
|
+
<span class="co">RubyVersion</span>.tiny <span class="c"># e.g. => 7</span>
|
1039
|
+
<span class="co">RubyVersion</span>.patchlevel <span class="c"># e.g. => 249</span>
|
1040
|
+
<span class="co">RubyVersion</span>.description <span class="c"># e.g. => "ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]"</span>
|
1041
|
+
</pre></td></tr><tr><th>Information</th> <td>Thanks to Hanmac for a nice improvement.</td></tr><tr><th>Specification</th> <td> <span id="show_version_spec">(<a href="javascript:show('version_spec')">show</a>)</span>
|
1042
|
+
<pre class="source" id="version_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/version</span><span class="dl">'</span></span>
|
1043
|
+
require <span class="s"><span class="dl">'</span><span class="k">date</span><span class="dl">'</span></span>
|
1044
|
+
|
1045
|
+
describe <span class="s"><span class="dl">'</span><span class="k">RubyVersion</span><span class="dl">'</span></span> <span class="r">do</span>
|
1046
|
+
before <span class="sy">:all</span> <span class="r">do</span>
|
1047
|
+
<span class="iv">@remember_version</span> = <span class="co">RUBY_VERSION</span>
|
1048
|
+
capture_stderr{ <span class="co">RUBY_VERSION</span> = <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span> }
|
1049
|
+
<span class="r">end</span>
|
1050
|
+
|
1051
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display RUBY_VERSION if called directly (to_s)</span><span class="dl">'</span></span> <span class="r">do</span>
|
1052
|
+
<span class="co">RubyVersion</span>.to_s.should == <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>
|
1053
|
+
<span class="r">end</span>
|
1054
|
+
|
1055
|
+
context <span class="s"><span class="dl">'</span><span class="k">using is method with parameter</span><span class="dl">'</span></span> <span class="r">do</span>
|
1056
|
+
it <span class="s"><span class="dl">'</span><span class="k">should check for main version (1.8 or 1.9) when Float paramater is given</span><span class="dl">'</span></span> <span class="r">do</span>
|
1057
|
+
<span class="co">RubyVersion</span>.is?( <span class="fl">1.8</span> ).should == <span class="pc">true</span>
|
1058
|
+
<span class="co">RubyVersion</span>.is?( <span class="fl">1.9</span> ).should == <span class="pc">false</span>
|
1059
|
+
<span class="r">end</span>
|
1060
|
+
|
1061
|
+
it <span class="s"><span class="dl">'</span><span class="k">should check with string comparison if parameter is not Float</span><span class="dl">'</span></span> <span class="r">do</span>
|
1062
|
+
<span class="co">RubyVersion</span>.is?( <span class="s"><span class="dl">'</span><span class="k">1.8</span><span class="dl">'</span></span> ).should == <span class="pc">false</span>
|
1063
|
+
<span class="r">end</span>
|
1064
|
+
<span class="r">end</span>
|
1065
|
+
|
1066
|
+
context <span class="s"><span class="dl">'</span><span class="k">using is method without parameter but method chaining</span><span class="dl">'</span></span> <span class="r">do</span>
|
1067
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return a string for usage with comparison operators</span><span class="dl">'</span></span> <span class="r">do</span>
|
1068
|
+
(<span class="co">RubyVersion</span>.is > <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>).should == <span class="pc">false</span>
|
1069
|
+
(<span class="co">RubyVersion</span> <= <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>).should == <span class="pc">true</span>
|
1070
|
+
(<span class="co">RubyVersion</span>.is.between? <span class="s"><span class="dl">'</span><span class="k">1.8.6</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span>).should == <span class="pc">true</span>
|
1071
|
+
<span class="r">end</span>
|
1072
|
+
|
1073
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create some handy compare aliases</span><span class="dl">'</span></span> <span class="r">do</span>
|
1074
|
+
<span class="co">RubyVersion</span>.is.above( <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span> ).should == <span class="pc">false</span>
|
1075
|
+
<span class="co">RubyVersion</span>.is.at_least( <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span> ).should == <span class="pc">true</span>
|
1076
|
+
<span class="co">RubyVersion</span>.is.exactly( <span class="s"><span class="dl">'</span><span class="k">1.8.7</span><span class="dl">'</span></span> ).should == <span class="pc">true</span>
|
1077
|
+
<span class="r">end</span>
|
1078
|
+
|
1079
|
+
it <span class="s"><span class="dl">'</span><span class="k">also allows to check for the release dates</span><span class="dl">'</span></span> <span class="r">do</span>
|
1080
|
+
<span class="co">RubyVersion</span>.is.older_than( <span class="co">Date</span>.today ).should == <span class="pc">true</span>
|
1081
|
+
<span class="co">RubyVersion</span>.is.newer_than( <span class="s"><span class="dl">'</span><span class="k">2000-01-01</span><span class="dl">'</span></span> ).should == <span class="pc">true</span>
|
1082
|
+
<span class="r">end</span>
|
1083
|
+
<span class="r">end</span>
|
1084
|
+
|
1085
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define some accessors</span><span class="dl">'</span></span> <span class="r">do</span>
|
1086
|
+
<span class="co">RubyVersion</span>.major.should == <span class="i">1</span>
|
1087
|
+
<span class="co">RubyVersion</span>.minor.should == <span class="i">8</span>
|
1088
|
+
<span class="co">RubyVersion</span>.tiny.should == <span class="i">7</span>
|
1089
|
+
<span class="c"># RubyVersion.patchlevel # => RUBY_PATCHLEVEL</span>
|
1090
|
+
<span class="c"># RubyVersion.description # => RUBY_DESCRIPTION</span>
|
1091
|
+
<span class="r">end</span>
|
1092
|
+
|
1093
|
+
after <span class="sy">:all</span> <span class="r">do</span>
|
1094
|
+
capture_stderr{ <span class="co">RUBY_VERSION</span> = <span class="iv">@remember_version</span> }
|
1095
|
+
<span class="r">end</span>
|
1096
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_version_source">(<a href="javascript:show('version_source')">show</a>)</span>
|
1097
|
+
<pre class="source" id="version_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1098
|
+
require <span class="s"><span class="dl">'</span><span class="k">date</span><span class="dl">'</span></span>
|
1099
|
+
require <span class="s"><span class="dl">'</span><span class="k">time</span><span class="dl">'</span></span>
|
1100
|
+
|
1101
|
+
<span class="r">module</span> <span class="cl">RubyVersion</span>
|
1102
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
1103
|
+
<span class="r">def</span> <span class="fu">to_s</span>
|
1104
|
+
<span class="co">RUBY_VERSION</span>
|
1105
|
+
<span class="r">end</span>
|
1106
|
+
|
1107
|
+
<span class="c"># comparable</span>
|
1108
|
+
<span class="r">def</span> <span class="fu"><=></span>(other)
|
1109
|
+
value = <span class="r">case</span> other
|
1110
|
+
<span class="r">when</span> <span class="co">Integer</span>
|
1111
|
+
<span class="co">RUBY_VERSION</span>.to_i
|
1112
|
+
<span class="r">when</span> <span class="co">Float</span>
|
1113
|
+
<span class="co">RUBY_VERSION</span>.to_f
|
1114
|
+
<span class="r">when</span> <span class="co">String</span>
|
1115
|
+
<span class="co">RUBY_VERSION</span>
|
1116
|
+
<span class="r">when</span> <span class="co">Date</span>, <span class="co">Time</span>
|
1117
|
+
other.class.parse(<span class="co">RUBY_RELEASE_DATE</span>)
|
1118
|
+
<span class="r">else</span>
|
1119
|
+
other = other.to_s
|
1120
|
+
<span class="co">RUBY_VERSION</span>
|
1121
|
+
<span class="r">end</span>
|
1122
|
+
value <=> other
|
1123
|
+
<span class="r">end</span>
|
1124
|
+
include <span class="co">Comparable</span>
|
1125
|
+
|
1126
|
+
<span class="c"># chaining for dsl-like language</span>
|
1127
|
+
<span class="r">def</span> <span class="fu">is?</span>(other = <span class="pc">nil</span>)
|
1128
|
+
<span class="r">if</span> other
|
1129
|
+
<span class="co">RubyVersion</span> == other
|
1130
|
+
<span class="r">else</span>
|
1131
|
+
<span class="co">RubyVersion</span>
|
1132
|
+
<span class="r">end</span>
|
1133
|
+
<span class="r">end</span>
|
1134
|
+
<span class="r">alias</span> <span class="fu">is</span> <span class="fu">is?</span>
|
1135
|
+
|
1136
|
+
<span class="c"># aliases</span>
|
1137
|
+
<span class="r">alias</span> <span class="fu">below</span> <span class="fu"><</span>
|
1138
|
+
<span class="r">alias</span> <span class="fu">below?</span> <span class="fu"><</span>
|
1139
|
+
<span class="r">alias</span> <span class="fu">at_most</span> <span class="fu"><=</span>
|
1140
|
+
<span class="r">alias</span> <span class="fu">at_most?</span> <span class="fu"><=</span>
|
1141
|
+
<span class="r">alias</span> <span class="fu">above</span> <span class="fu">></span>
|
1142
|
+
<span class="r">alias</span> <span class="fu">above?</span> <span class="fu">></span>
|
1143
|
+
<span class="r">alias</span> <span class="fu">at_least</span> <span class="fu">>=</span>
|
1144
|
+
<span class="r">alias</span> <span class="fu">at_least?</span> <span class="fu">>=</span>
|
1145
|
+
<span class="r">alias</span> <span class="fu">exactly</span> <span class="fu">==</span>
|
1146
|
+
<span class="r">alias</span> <span class="fu">exactly?</span> <span class="fu">==</span>
|
1147
|
+
<span class="r">def</span> <span class="fu">not</span>(other)
|
1148
|
+
<span class="pc">self</span> != other
|
1149
|
+
<span class="r">end</span>
|
1150
|
+
<span class="r">alias</span> <span class="fu">not?</span> <span class="fu">not</span>
|
1151
|
+
<span class="r">alias</span> <span class="fu">between</span> <span class="fu">between?</span>
|
1152
|
+
|
1153
|
+
<span class="c"># compare dates</span>
|
1154
|
+
<span class="r">def</span> <span class="fu">newer_than</span>(other)
|
1155
|
+
<span class="r">if</span> other.is_a? <span class="co">Date</span> <span class="r">or</span> other.is_a? <span class="co">Time</span>
|
1156
|
+
<span class="co">RubyVersion</span> > other
|
1157
|
+
<span class="r">else</span>
|
1158
|
+
<span class="co">RUBY_RELEASE_DATE</span> > other.to_s
|
1159
|
+
<span class="r">end</span>
|
1160
|
+
<span class="r">end</span>
|
1161
|
+
<span class="r">alias</span> <span class="fu">newer_than?</span> <span class="fu">newer_than</span>
|
1162
|
+
|
1163
|
+
<span class="r">def</span> <span class="fu">older_than</span>(other)
|
1164
|
+
<span class="r">if</span> other.is_a? <span class="co">Date</span> <span class="r">or</span> other.is_a? <span class="co">Time</span>
|
1165
|
+
<span class="co">RubyVersion</span> < other
|
1166
|
+
<span class="r">else</span>
|
1167
|
+
<span class="co">RUBY_RELEASE_DATE</span> < other.to_s
|
1168
|
+
<span class="r">end</span>
|
1169
|
+
<span class="r">end</span>
|
1170
|
+
<span class="r">alias</span> <span class="fu">older_than?</span> <span class="fu">older_than</span>
|
1171
|
+
|
1172
|
+
<span class="r">def</span> <span class="fu">released_today</span>
|
1173
|
+
<span class="co">RubyVersion</span>.date == <span class="co">Date</span>.today
|
1174
|
+
<span class="r">end</span>
|
1175
|
+
<span class="r">alias</span> <span class="fu">released_today?</span> <span class="fu">released_today</span>
|
1176
|
+
|
1177
|
+
<span class="c"># accessors</span>
|
1178
|
+
|
1179
|
+
<span class="r">def</span> <span class="fu">major</span>
|
1180
|
+
<span class="co">RUBY_VERSION</span>.to_i
|
1181
|
+
<span class="r">end</span>
|
1182
|
+
<span class="r">alias</span> <span class="fu">main</span> <span class="fu">major</span>
|
1183
|
+
|
1184
|
+
<span class="r">def</span> <span class="fu">minor</span>
|
1185
|
+
<span class="co">RUBY_VERSION</span>.split(<span class="s"><span class="dl">'</span><span class="k">.</span><span class="dl">'</span></span>)[<span class="i">1</span>].to_i
|
1186
|
+
<span class="r">end</span>
|
1187
|
+
<span class="r">alias</span> <span class="fu">mini</span> <span class="fu">minor</span>
|
1188
|
+
|
1189
|
+
<span class="r">def</span> <span class="fu">tiny</span>
|
1190
|
+
<span class="co">RUBY_VERSION</span>.split(<span class="s"><span class="dl">'</span><span class="k">.</span><span class="dl">'</span></span>)[<span class="i">2</span>].to_i
|
1191
|
+
<span class="r">end</span>
|
1192
|
+
|
1193
|
+
<span class="r">alias</span> <span class="fu">teeny</span> <span class="fu">tiny</span>
|
1194
|
+
|
1195
|
+
<span class="r">def</span> <span class="fu">patchlevel</span>
|
1196
|
+
<span class="co">RUBY_PATCHLEVEL</span>
|
1197
|
+
<span class="r">end</span>
|
1198
|
+
|
1199
|
+
<span class="r">def</span> <span class="fu">platform</span>
|
1200
|
+
<span class="co">RUBY_PLATFORM</span>
|
1201
|
+
<span class="r">end</span>
|
1202
|
+
|
1203
|
+
<span class="r">def</span> <span class="fu">release_date</span>
|
1204
|
+
<span class="co">Date</span>.parse <span class="co">RUBY_RELEASE_DATE</span>
|
1205
|
+
<span class="r">end</span>
|
1206
|
+
<span class="r">alias</span> <span class="fu">date</span> <span class="fu">release_date</span>
|
1207
|
+
|
1208
|
+
<span class="r">def</span> <span class="fu">description</span>
|
1209
|
+
<span class="co">RUBY_DESCRIPTION</span>
|
1210
|
+
<span class="r">end</span>
|
1211
|
+
<span class="r">end</span>
|
1212
|
+
<span class="r">end</span>
|
1213
|
+
|
1214
|
+
<span class="c"># J-_-L</span></pre> </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/version">github wiki</a></td></tr></table>
|
1215
|
+
</div>
|
1216
|
+
<h2 title="require 'zucker/extensions'" id="extensions">Cubes[extensions]</h2>
|
1217
|
+
|
1218
|
+
<div class="cubes">
|
1219
|
+
<h3 title="require 'zucker/array'" id="array">array</h3>
|
1220
|
+
<table class="cube_table"
|
1221
|
+
id="array_cube"
|
1222
|
+
title="require 'zucker/array'"> <tr><th>Summary</th> <td>Methods one could miss for <code>Array</code>.</td></tr><tr><th>Why?</th> <td><p><code>Array</code> has <code>&</code> and <code>|</code>, but why does it not have <code>^</code>?</p><p><code>sum</code> is just needed pretty often..</p></td></tr><tr><th>Methods/Usage</th> <td><h5>Array#^</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>] ^ [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => [1,2,5,6]</span></pre><h5>Array#sum</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum <span class="c"># => 15</span></pre><h5>Array#chrs</h5><pre class="usage source" style="display:block">[<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs <span class="c"># => 'Hallo'</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_array_spec">(<a href="javascript:show('array_spec')">show</a>)</span>
|
1223
|
+
<pre class="source" id="array_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array</span><span class="dl">'</span></span>
|
1224
|
+
|
1225
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1226
|
+
it <span class="s"><span class="dl">'</span><span class="k">should do an exclusive or</span><span class="dl">'</span></span> <span class="r">do</span>
|
1227
|
+
a = [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>]
|
1228
|
+
b = [<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>]
|
1229
|
+
(a^b).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">5</span>,<span class="i">6</span>]
|
1230
|
+
<span class="r">end</span>
|
1231
|
+
<span class="r">end</span>
|
1232
|
+
|
1233
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#sum</span><span class="dl">'</span></span> <span class="r">do</span>
|
1234
|
+
it <span class="s"><span class="dl">'</span><span class="k">should sum the array</span><span class="dl">'</span></span> <span class="r">do</span>
|
1235
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>,<span class="i">5</span>].sum.should == <span class="i">15</span>
|
1236
|
+
<span class="s"><span class="dl">%w|</span><span class="k">More Ruby</span><span class="dl">|</span></span>.sum.should == <span class="s"><span class="dl">'</span><span class="k">MoreRuby</span><span class="dl">'</span></span>
|
1237
|
+
<span class="r">end</span>
|
1238
|
+
<span class="r">end</span>
|
1239
|
+
|
1240
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#chrs</span><span class="dl">'</span></span> <span class="r">do</span>
|
1241
|
+
it <span class="s"><span class="dl">'</span><span class="k">should convert the array to a string, using each element as ord value for the char</span><span class="dl">'</span></span> <span class="r">do</span>
|
1242
|
+
[<span class="i">72</span>, <span class="i">97</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">111</span>].chrs.should == <span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>
|
1243
|
+
<span class="r">end</span>
|
1244
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_array_source">(<a href="javascript:show('array_source')">show</a>)</span>
|
1245
|
+
<pre class="source" id="array_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1246
|
+
|
1247
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
1248
|
+
<span class="r">def</span> <span class="fu">^</span>(other) <span class="c"># TODO: more efficient</span>
|
1249
|
+
(<span class="pc">self</span> - other) +
|
1250
|
+
(other - <span class="pc">self</span>)
|
1251
|
+
<span class="r">end</span>
|
1252
|
+
|
1253
|
+
<span class="c"># can take an argument & block to be Rails compatible</span>
|
1254
|
+
<span class="r">def</span> <span class="fu">sum</span>(identity = <span class="i">0</span>, &block)
|
1255
|
+
<span class="c"># inject(:+)</span>
|
1256
|
+
<span class="r">if</span> block_given?
|
1257
|
+
map(&block).sum( identity )
|
1258
|
+
<span class="r">else</span>
|
1259
|
+
inject(<span class="sy">:+</span>) || identity
|
1260
|
+
<span class="r">end</span>
|
1261
|
+
<span class="r">end</span>
|
1262
|
+
|
1263
|
+
<span class="r">def</span> <span class="fu">chrs</span>
|
1264
|
+
<span class="pc">self</span>.pack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
1265
|
+
<span class="r">end</span>
|
1266
|
+
<span class="r">end</span>
|
1267
|
+
|
1268
|
+
<span class="c"># J-_-L</span></pre> </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/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><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] } <span class="c"># => {1=>'1',2=>'2',3=>'3'}</span></pre></td></tr><tr><th>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>
|
1272
|
+
<pre class="source" id="enumerable_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/enumerable</span><span class="dl">'</span></span>
|
1273
|
+
|
1274
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Enumerable#mash</span><span class="dl">'</span></span> <span class="r">do</span>
|
1275
|
+
it <span class="s"><span class="dl">'</span><span class="k">should "map" a hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
1276
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].mash{|e| [e, e.to_s] }.should == {<span class="i">1</span>=><span class="s"><span class="dl">'</span><span class="k">1</span><span class="dl">'</span></span>,<span class="i">2</span>=><span class="s"><span class="dl">'</span><span class="k">2</span><span class="dl">'</span></span>,<span class="i">3</span>=><span class="s"><span class="dl">'</span><span class="k">3</span><span class="dl">'</span></span>,}
|
1277
|
+
<span class="r">end</span>
|
1278
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_enumerable_source">(<a href="javascript:show('enumerable_source')">show</a>)</span>
|
1279
|
+
<pre class="source" id="enumerable_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1280
|
+
|
1281
|
+
<span class="r">module</span> <span class="cl">Enumerable</span>
|
1282
|
+
<span class="r">def</span> <span class="fu">mash</span>
|
1283
|
+
ret = {}
|
1284
|
+
each{ |kv|
|
1285
|
+
ret.store *( <span class="r">yield</span>(kv)[<span class="i">0</span>,<span class="i">2</span>] )
|
1286
|
+
}
|
1287
|
+
ret
|
1288
|
+
<span class="r">end</span>
|
1289
|
+
<span class="r">end</span>
|
1290
|
+
|
1291
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1292
|
+
<table class="cube_table"
|
1293
|
+
id="file_cube"
|
1294
|
+
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><pre class="usage source" style="display:block"><span class="co">File</span>.gsub <span class="s"><span class="dl">'</span><span class="k">some_filename</span><span class="dl">'</span></span>, {
|
1295
|
+
<span class="rx"><span class="dl">/</span><span class="k">hi</span><span class="dl">/</span></span> => <span class="s"><span class="dl">'</span><span class="k">cu</span><span class="dl">'</span></span>,
|
1296
|
+
<span class="rx"><span class="dl">/</span><span class="ch">\d</span><span class="dl">/</span></span> => proc <span class="r">do</span> (<span class="gv">$&</span>.to_i+<span class="i">1</span>).to_s <span class="r">end</span>
|
1297
|
+
} <span class="c"># will read the file and substitute the hash keys with their values</span>
|
1298
|
+
</pre><h5>File#delete!</h5><pre class="usage source" style="display:block"><span class="co">File</span>.delete! <span class="s"><span class="dl">'</span><span class="k">some_filename</span><span class="dl">'</span></span> <span class="c"># will delete the file, don't care if it exist.</span></pre></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>
|
1299
|
+
<pre class="source" id="file_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/file</span><span class="dl">'</span></span>
|
1300
|
+
require <span class="s"><span class="dl">'</span><span class="k">fileutils</span><span class="dl">'</span></span>
|
1301
|
+
|
1302
|
+
describe <span class="s"><span class="dl">'</span><span class="k">File#gsub</span><span class="dl">'</span></span> <span class="r">do</span>
|
1303
|
+
it <span class="s"><span class="dl">'</span><span class="k">should read filename in arg1, substitute every key in the arg2 with its value and save the file</span><span class="dl">'</span></span>
|
1304
|
+
<span class="r">end</span>
|
1305
|
+
|
1306
|
+
describe <span class="s"><span class="dl">'</span><span class="k">File#delete!</span><span class="dl">'</span></span> <span class="r">do</span>
|
1307
|
+
let <span class="sy">:random_filename</span> <span class="r">do</span>
|
1308
|
+
<span class="s"><span class="dl">'</span><span class="k">test_</span><span class="dl">'</span></span> + (<span class="i">0</span>..<span class="i">20</span>).map{|e| [*<span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>..<span class="s"><span class="dl">'</span><span class="k">z</span><span class="dl">'</span></span>].send <span class="co">RUBY_VERSION</span> > <span class="s"><span class="dl">'</span><span class="k">1.9</span><span class="dl">'</span></span> ? <span class="sy">:sample</span> : <span class="sy">:choice</span> }*<span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1309
|
+
<span class="r">end</span>
|
1310
|
+
|
1311
|
+
it <span class="s"><span class="dl">'</span><span class="k">should delete the filename given as argument if it exists + return non-nil</span><span class="dl">'</span></span> <span class="r">do</span>
|
1312
|
+
<span class="co">FileUtils</span>.touch random_filename
|
1313
|
+
res = <span class="pc">false</span>
|
1314
|
+
proc <span class="r">do</span>
|
1315
|
+
res = <span class="co">File</span>.delete! random_filename
|
1316
|
+
<span class="r">end</span>.should_not raise_exception
|
1317
|
+
res.should be_true
|
1318
|
+
<span class="r">end</span>
|
1319
|
+
|
1320
|
+
it <span class="s"><span class="dl">'</span><span class="k">should do nothing if the filename given as argument does not exist + return nil</span><span class="dl">'</span></span> <span class="r">do</span>
|
1321
|
+
res = <span class="pc">false</span>
|
1322
|
+
proc <span class="r">do</span>
|
1323
|
+
res = <span class="co">File</span>.delete! random_filename
|
1324
|
+
<span class="r">end</span>.should_not raise_exception
|
1325
|
+
res.should be_false
|
1326
|
+
<span class="r">end</span>
|
1327
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_file_source">(<a href="javascript:show('file_source')">show</a>)</span>
|
1328
|
+
<pre class="source" id="file_source"><span class="r">class</span> << <span class="cl">File</span>
|
1329
|
+
<span class="r">def</span> <span class="fu">gsub</span>(filename, regex_hash)
|
1330
|
+
data = <span class="co">File</span>.read filename
|
1331
|
+
<span class="co">File</span>.open(filename,<span class="s"><span class="dl">'</span><span class="k">w</span><span class="dl">'</span></span>){ |file|
|
1332
|
+
regex_hash.each{ |regex, new_string|
|
1333
|
+
regex = regex.to_s <span class="r">unless</span> regex.is_a? <span class="co">Regexp</span>
|
1334
|
+
|
1335
|
+
<span class="r">if</span> new_string.is_a? <span class="co">Proc</span>
|
1336
|
+
data.gsub! regex, &new_string
|
1337
|
+
<span class="r">else</span>
|
1338
|
+
data.gsub! regex, new_string
|
1339
|
+
<span class="r">end</span>
|
1340
|
+
}
|
1341
|
+
file.print data
|
1342
|
+
}
|
1343
|
+
<span class="r">end</span>
|
1344
|
+
|
1345
|
+
<span class="r">def</span> <span class="fu">delete!</span> filename
|
1346
|
+
<span class="r">return</span> <span class="pc">nil</span> <span class="r">if</span> !<span class="co">File</span>.exist?(filename)
|
1347
|
+
<span class="co">File</span>.delete filename
|
1348
|
+
<span class="r">end</span>
|
1349
|
+
<span class="r">end</span></pre> </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>
|
1350
|
+
<table class="cube_table"
|
1351
|
+
id="hash_cube"
|
1352
|
+
title="require 'zucker/hash'"> <tr><th>Summary</th> <td>Some sugar for dealing with hashs.</td></tr><tr><th>Methods/Usage</th> <td><h5>Hash.zip</h5><pre class="usage source" style="display:block"><span class="co">Hash</span>.zip [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] <span class="c"># => {1=>4, 2=>5, 3=>6}</span></pre><h5>Hash#<<</h5><pre class="usage source" style="display:block">{<span class="i">1</span>=><span class="i">2</span>} << [<span class="i">3</span>, <span class="i">4</span>] <span class="c"># => { 1=>2, 3=>4 }</span>
|
1353
|
+
{<span class="i">1</span>=><span class="i">2</span>} << { <span class="i">5</span>=><span class="i">6</span> } <span class="c"># => { 1=>2, 5=>6 }</span>
|
1354
|
+
</pre><h5>Hash#&</h5><pre class="usage source" style="display:block">{ <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> } & { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">7</span> } <span class="c"># => { 1=>4 }</span></pre></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>
|
1355
|
+
<pre class="source" id="hash_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash</span><span class="dl">'</span></span>
|
1356
|
+
|
1357
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash.zip</span><span class="dl">'</span></span> <span class="r">do</span>
|
1358
|
+
it <span class="s"><span class="dl">'</span><span class="k">should zip together both given enumerables and take them as key=>values for a new hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
1359
|
+
<span class="co">Hash</span>.zip( [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>], [<span class="i">4</span>,<span class="i">5</span>,<span class="i">6</span>] ).should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
1360
|
+
<span class="r">end</span>
|
1361
|
+
<span class="r">end</span>
|
1362
|
+
|
1363
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash#<<</span><span class="dl">'</span></span> <span class="r">do</span>
|
1364
|
+
it <span class="s"><span class="dl">'</span><span class="k">should append new elements to the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
1365
|
+
a = { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
1366
|
+
a << { <span class="i">4</span>=><span class="i">7</span> }
|
1367
|
+
a << [<span class="i">5</span>, <span class="i">8</span>]
|
1368
|
+
a.should == { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span>, <span class="i">4</span>=><span class="i">7</span>, <span class="i">5</span>=><span class="i">8</span> }
|
1369
|
+
<span class="r">end</span>
|
1370
|
+
<span class="r">end</span>
|
1371
|
+
|
1372
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash#&</span><span class="dl">'</span></span> <span class="r">do</span>
|
1373
|
+
it <span class="s"><span class="dl">'</span><span class="k">should select a sub hash containt only equal key-value pairs</span><span class="dl">'</span></span> <span class="r">do</span>
|
1374
|
+
a = { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">5</span>, <span class="i">3</span>=><span class="i">6</span> }
|
1375
|
+
b= { <span class="i">1</span>=><span class="i">4</span>, <span class="i">2</span>=><span class="i">7</span> }
|
1376
|
+
(a & b).should == { <span class="i">1</span>=><span class="i">4</span> }
|
1377
|
+
<span class="r">end</span>
|
1378
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_hash_source">(<a href="javascript:show('hash_source')">show</a>)</span>
|
1379
|
+
<pre class="source" id="hash_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1380
|
+
|
1381
|
+
<span class="r">class</span> <span class="cl">Hash</span>
|
1382
|
+
<span class="r">def</span> <span class="pc">self</span>.<span class="fu">zip</span>(keys,values)
|
1383
|
+
<span class="co">Hash</span>[ *keys.zip( values ).flatten ]
|
1384
|
+
<span class="r">end</span>
|
1385
|
+
|
1386
|
+
<span class="r">def</span> <span class="fu"><<</span>(other)
|
1387
|
+
<span class="r">case</span>
|
1388
|
+
<span class="r">when</span> other.is_a?(<span class="co">Hash</span>)
|
1389
|
+
merge! other
|
1390
|
+
<span class="r">when</span> other.is_a?(<span class="co">Enumerable</span>) || other.respond_to?(<span class="sy">:to_splat</span>)
|
1391
|
+
merge! <span class="co">Hash</span>[*other]
|
1392
|
+
<span class="r">else</span>
|
1393
|
+
raise <span class="co">TypeError</span>, <span class="s"><span class="dl">'</span><span class="k">can only append other Hashs and Enumerables (or Classes that implement to_splat)</span><span class="dl">'</span></span>
|
1394
|
+
<span class="r">end</span>
|
1395
|
+
<span class="r">end</span>
|
1396
|
+
|
1397
|
+
<span class="r">def</span> <span class="fu">&</span>(other)
|
1398
|
+
<span class="co">Hash</span>[ *select{ |k,v|
|
1399
|
+
other[k] == v
|
1400
|
+
}.flatten ]
|
1401
|
+
<span class="r">end</span>
|
1402
|
+
<span class="r">end</span>
|
1403
|
+
|
1404
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1405
|
+
<table class="cube_table"
|
1406
|
+
id="string_cube"
|
1407
|
+
title="require 'zucker/string'"> <tr><th>Summary</th> <td><code>String</code> extensions.</td></tr><tr><th>Why?</th> <td>Strings cannot be comfortable enough ;).</td></tr><tr><th>Methods/Usage</th> <td><h5>String#-</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">1234abc5678</span><span class="dl">'</span></span> - <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span> - <span class="rx"><span class="dl">/</span><span class="ch">\d</span><span class="dl">/</span></span> <span class="c"># => ac</span></pre><h5>String#^</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Yes vs No</span><span class="dl">'</span></span>^<span class="i">7</span> <span class="c"># => 'No'</span></pre><h5>String#lchomp</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k"> Yes</span><span class="dl">'</span></span>.lchomp <span class="c"># => 'Yes'</span></pre><h5>String#lchomp!</h5><pre class="usage source" style="display:block"><span class="c"># mutable lchomp version</span></pre><h5>String#ords</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Hallo</span><span class="dl">'</span></span>.ords <span class="c"># => [72, 97, 108, 108, 111]</span></pre><h5>String#constantize</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">Object</span><span class="dl">'</span></span>.constantize <span class="c"># => Object</span>
|
1408
|
+
<span class="s"><span class="dl">'</span><span class="k">Spec::VERSION</span><span class="dl">'</span></span>.constantize <span class="c"># => Spec::VERSION if rspec is loaded</span>
|
1409
|
+
<span class="c"># you can also pass a parameter or block to define what happens, when constant does not exist</span>
|
1410
|
+
<span class="s"><span class="dl">'</span><span class="k">IdontExist</span><span class="dl">'</span></span>.constantize(<span class="co">Array</span>) <span class="c"># => Array</span>
|
1411
|
+
<span class="s"><span class="dl">'</span><span class="k">String5</span><span class="dl">'</span></span>.constantize <span class="r">do</span> |string|
|
1412
|
+
string.chop.constantize
|
1413
|
+
<span class="r">end</span> <span class="c"># => String</span>
|
1414
|
+
</pre></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>
|
1415
|
+
<pre class="source" id="string_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/string</span><span class="dl">'</span></span>
|
1416
|
+
|
1417
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#-</span><span class="dl">'</span></span> <span class="r">do</span>
|
1418
|
+
it <span class="s"><span class="dl">'</span><span class="k">should remove the applied Regexp or String from self via gsub</span><span class="dl">'</span></span> <span class="r">do</span>
|
1419
|
+
(<span class="s"><span class="dl">'</span><span class="k">1234abc5678</span><span class="dl">'</span></span> - <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span> - <span class="rx"><span class="dl">/</span><span class="ch">\d</span><span class="dl">/</span></span>).should == <span class="s"><span class="dl">'</span><span class="k">ac</span><span class="dl">'</span></span>
|
1420
|
+
<span class="r">end</span>
|
1421
|
+
<span class="r">end</span>
|
1422
|
+
|
1423
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#^</span><span class="dl">'</span></span> <span class="r">do</span>
|
1424
|
+
it <span class="s"><span class="dl">'</span><span class="k">should give C-like substring access to strings</span><span class="dl">'</span></span> <span class="r">do</span>
|
1425
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1426
|
+
|
1427
|
+
(string^<span class="i">0</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1428
|
+
(string^<span class="i">1</span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1429
|
+
(string^<span class="i">13</span>).should == <span class="s"><span class="dl">'</span><span class="k">Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1430
|
+
(string^<span class="i">-1</span>).should == <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil</span><span class="dl">'</span></span>
|
1431
|
+
(string^<span class="i">38</span>).should == <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1432
|
+
(string^<span class="i">99</span>).should == <span class="pc">nil</span>
|
1433
|
+
<span class="r">end</span>
|
1434
|
+
<span class="r">end</span>
|
1435
|
+
|
1436
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#lchomp</span><span class="dl">'</span></span> <span class="r">do</span>
|
1437
|
+
it <span class="s"><span class="dl">'</span><span class="k">should chomp on the left side</span><span class="dl">'</span></span> <span class="r">do</span>
|
1438
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1439
|
+
string.lchomp(<span class="s"><span class="dl">'</span><span class="k">T</span><span class="dl">'</span></span>).should == <span class="s"><span class="dl">'</span><span class="k">heoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1440
|
+
<span class="r">end</span>
|
1441
|
+
<span class="r">end</span>
|
1442
|
+
|
1443
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#ords</span><span class="dl">'</span></span> <span class="r">do</span>
|
1444
|
+
it <span class="s"><span class="dl">'</span><span class="k">should unpack characters</span><span class="dl">'</span></span> <span class="r">do</span>
|
1445
|
+
string = <span class="s"><span class="dl">'</span><span class="k">Theoretische Informatik ist voll geil!</span><span class="dl">'</span></span>
|
1446
|
+
string.ords.should == [<span class="i">84</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">101</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">99</span>, <span class="i">104</span>, <span class="i">101</span>, <span class="i">32</span>, <span class="i">73</span>, <span class="i">110</span>, <span class="i">102</span>, <span class="i">111</span>, <span class="i">114</span>, <span class="i">109</span>, <span class="i">97</span>, <span class="i">116</span>, <span class="i">105</span>, <span class="i">107</span>, <span class="i">32</span>, <span class="i">105</span>, <span class="i">115</span>, <span class="i">116</span>, <span class="i">32</span>, <span class="i">118</span>, <span class="i">111</span>, <span class="i">108</span>, <span class="i">108</span>, <span class="i">32</span>, <span class="i">103</span>, <span class="i">101</span>, <span class="i">105</span>, <span class="i">108</span>, <span class="i">33</span>]
|
1447
|
+
<span class="r">end</span>
|
1448
|
+
|
1449
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#constantize</span><span class="dl">'</span></span> <span class="r">do</span>
|
1450
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return the constant with that name</span><span class="dl">'</span></span> <span class="r">do</span>
|
1451
|
+
<span class="s"><span class="dl">'</span><span class="k">Object</span><span class="dl">'</span></span>.constantize.should == <span class="co">Object</span>
|
1452
|
+
<span class="r">end</span>
|
1453
|
+
|
1454
|
+
it <span class="s"><span class="dl">'</span><span class="k">should also work for nested constants</span><span class="dl">'</span></span> <span class="r">do</span>
|
1455
|
+
<span class="s"><span class="dl">'</span><span class="k">Zucker::VERSION</span><span class="dl">'</span></span>.constantize.should == <span class="co">Zucker</span>::<span class="co">VERSION</span>
|
1456
|
+
<span class="r">end</span>
|
1457
|
+
|
1458
|
+
it <span class="s"><span class="dl">'</span><span class="k">should throw name error if constant does not exist (and no parameter is given)</span><span class="dl">'</span></span> <span class="r">do</span>
|
1459
|
+
proc <span class="r">do</span>
|
1460
|
+
<span class="s"><span class="dl">'</span><span class="k">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span class="dl">'</span></span>.constantize
|
1461
|
+
<span class="r">end</span>.should raise_exception <span class="co">NameError</span>
|
1462
|
+
<span class="r">end</span>
|
1463
|
+
|
1464
|
+
it <span class="s"><span class="dl">'</span><span class="k">should call the block (and not raise an error) if constant does not exist and block given</span><span class="dl">'</span></span> <span class="r">do</span>
|
1465
|
+
proc <span class="r">do</span>
|
1466
|
+
<span class="s"><span class="dl">'</span><span class="k">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span class="dl">'</span></span>.constantize <span class="r">do</span> |string|
|
1467
|
+
<span class="co">Default</span> = [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>]
|
1468
|
+
<span class="r">end</span>.should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>]
|
1469
|
+
<span class="r">end</span>.should_not raise_exception <span class="co">NameError</span>
|
1470
|
+
<span class="r">end</span>
|
1471
|
+
|
1472
|
+
it <span class="s"><span class="dl">'</span><span class="k">should return the second parameter (and not raise an error) if constant does not exist and parameter given</span><span class="dl">'</span></span> <span class="r">do</span>
|
1473
|
+
proc <span class="r">do</span>
|
1474
|
+
<span class="s"><span class="dl">'</span><span class="k">ObfsefsefsefafesafaefRubyZuckerafdfselijfesject</span><span class="dl">'</span></span>.constantize(<span class="co">Array</span>).should == <span class="co">Array</span>
|
1475
|
+
<span class="r">end</span>.should_not raise_exception <span class="co">NameError</span>
|
1476
|
+
<span class="r">end</span>
|
1477
|
+
<span class="r">end</span>
|
1478
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_string_source">(<a href="javascript:show('string_source')">show</a>)</span>
|
1479
|
+
<pre class="source" id="string_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1480
|
+
|
1481
|
+
<span class="r">class</span> <span class="cl">String</span>
|
1482
|
+
<span class="r">def</span> <span class="fu">-</span>(rem)
|
1483
|
+
gsub( <span class="co">Regexp</span> === rem ? rem : rem.to_s, <span class="s"><span class="dl">'</span><span class="dl">'</span></span> )
|
1484
|
+
<span class="r">end</span>
|
1485
|
+
|
1486
|
+
<span class="r">def</span> <span class="fu">^</span>(pos)
|
1487
|
+
pos = pos.to_i
|
1488
|
+
<span class="r">if</span> pos >= <span class="i">0</span>
|
1489
|
+
<span class="pc">self</span>[pos..<span class="i">-1</span>]
|
1490
|
+
<span class="r">else</span>
|
1491
|
+
<span class="pc">self</span>[<span class="i">0</span>...pos]
|
1492
|
+
<span class="r">end</span>
|
1493
|
+
<span class="r">end</span>
|
1494
|
+
|
1495
|
+
<span class="r">def</span> <span class="fu">lchomp</span>(arg = <span class="gv">$/</span>)
|
1496
|
+
reverse.chomp(arg).reverse
|
1497
|
+
<span class="r">end</span>
|
1498
|
+
|
1499
|
+
<span class="r">def</span> <span class="fu">lchomp!</span>(arg = <span class="gv">$/</span>)
|
1500
|
+
reverse.chomp!(arg).reverse
|
1501
|
+
<span class="r">end</span>
|
1502
|
+
|
1503
|
+
<span class="r">def</span> <span class="fu">ords</span>
|
1504
|
+
unpack <span class="s"><span class="dl">'</span><span class="k">C*</span><span class="dl">'</span></span>
|
1505
|
+
<span class="c"># bytes.to_a</span>
|
1506
|
+
<span class="r">end</span>
|
1507
|
+
|
1508
|
+
<span class="r">def</span> <span class="fu">constantize</span>(default_value = <span class="pc">nil</span>) <span class="c"># always uses global scope as in AS... is this good?</span>
|
1509
|
+
get_constant = lambda{
|
1510
|
+
<span class="pc">self</span>.split(<span class="rx"><span class="dl">/</span><span class="k">::</span><span class="dl">/</span></span>).inject( <span class="co">Object</span> ){ |base_constant, current_constant|
|
1511
|
+
base_constant.const_get current_constant
|
1512
|
+
}
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
<span class="r">if</span> !default_value && !block_given?
|
1516
|
+
get_constant.call
|
1517
|
+
<span class="r">else</span>
|
1518
|
+
<span class="r">begin</span>
|
1519
|
+
get_constant.call
|
1520
|
+
<span class="r">rescue</span> <span class="co">NameError</span>
|
1521
|
+
<span class="r">if</span> block_given?
|
1522
|
+
<span class="r">yield</span> <span class="pc">self</span>
|
1523
|
+
<span class="r">else</span>
|
1524
|
+
default_value
|
1525
|
+
<span class="r">end</span>
|
1526
|
+
<span class="r">end</span>
|
1527
|
+
<span class="r">end</span>
|
1528
|
+
<span class="r">end</span>
|
1529
|
+
<span class="r">end</span>
|
1530
|
+
|
1531
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1532
|
+
<table class="cube_table"
|
1533
|
+
id="unary_cube"
|
1534
|
+
title="require 'zucker/unary'"> <tr><th>Summary</th> <td>Easy conversion between strings and symbols.</td></tr><tr><th>Why?</th> <td>Sometimes, you do not care if you get a <code>String</code> or <code>Symbol</code> as input - but when analysing it, you often need to choose one format. A concise possibility for this conversion is using the unary operators <code>String#-@</code> and <code>Symbol#+@</code>.</td></tr><tr><th>Methods/Usage</th> <td><h5>String#+@</h5><pre class="usage source" style="display:block">+<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => 'was_string'</span></pre><h5>String#-@</h5><pre class="usage source" style="display:block">-<span class="s"><span class="dl">'</span><span class="k">was_string</span><span class="dl">'</span></span> <span class="c"># => :was_string</span></pre><h5>Symbol#+@</h5><pre class="usage source" style="display:block">+<span class="sy">:was_symbol</span> <span class="c"># => 'was_symbol'</span></pre><h5>Symbol#-@</h5><pre class="usage source" style="display:block">-<span class="sy">:was_symbol</span> <span class="c"># => :was_symbol</span></pre></td></tr><tr><th>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>
|
1535
|
+
<pre class="source" id="unary_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/unary</span><span class="dl">'</span></span>
|
1536
|
+
|
1537
|
+
describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1538
|
+
it <span class="s"><span class="dl">'</span><span class="k">should convert to_s</span><span class="dl">'</span></span> <span class="r">do</span>
|
1539
|
+
+(<span class="sy">:matz</span>) == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> <span class="c"># () 1.8 bug</span>
|
1540
|
+
<span class="r">end</span>
|
1541
|
+
<span class="r">end</span>
|
1542
|
+
|
1543
|
+
describe <span class="co">Symbol</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1544
|
+
it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
|
1545
|
+
-<span class="sy">:matz</span> == <span class="sy">:matz</span>
|
1546
|
+
<span class="r">end</span>
|
1547
|
+
<span class="r">end</span>
|
1548
|
+
|
1549
|
+
describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#+@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1550
|
+
it <span class="s"><span class="dl">'</span><span class="k">should do nothing</span><span class="dl">'</span></span> <span class="r">do</span>
|
1551
|
+
+<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span>
|
1552
|
+
<span class="r">end</span>
|
1553
|
+
<span class="r">end</span>
|
1554
|
+
|
1555
|
+
describe <span class="co">String</span>, <span class="s"><span class="dl">'</span><span class="k">#-@</span><span class="dl">'</span></span> <span class="r">do</span>
|
1556
|
+
it <span class="s"><span class="dl">'</span><span class="k">should convert to_sym</span><span class="dl">'</span></span> <span class="r">do</span>
|
1557
|
+
-<span class="s"><span class="dl">'</span><span class="k">matz</span><span class="dl">'</span></span> == <span class="sy">:matz</span>
|
1558
|
+
<span class="r">end</span>
|
1559
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_unary_source">(<a href="javascript:show('unary_source')">show</a>)</span>
|
1560
|
+
<pre class="source" id="unary_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1561
|
+
|
1562
|
+
<span class="r">class</span> <span class="cl">String</span>
|
1563
|
+
<span class="r">def</span> <span class="fu">+@</span>
|
1564
|
+
<span class="pc">self</span>
|
1565
|
+
<span class="r">end</span>
|
1566
|
+
|
1567
|
+
<span class="r">def</span> <span class="fu">-@</span>
|
1568
|
+
to_sym
|
1569
|
+
<span class="r">end</span>
|
1570
|
+
<span class="r">end</span>
|
1571
|
+
|
1572
|
+
<span class="r">class</span> <span class="cl">Symbol</span>
|
1573
|
+
<span class="r">def</span> <span class="fu">+@</span>
|
1574
|
+
to_s
|
1575
|
+
<span class="r">end</span>
|
1576
|
+
|
1577
|
+
<span class="r">def</span> <span class="fu">-@</span>
|
1578
|
+
<span class="pc">self</span>
|
1579
|
+
<span class="r">end</span>
|
1580
|
+
<span class="r">end</span>
|
1581
|
+
|
1582
|
+
<span class="c"># J-_-L</span></pre> </td></tr><tr><th>Compatibility</th> <td>1.9, 1.8 (<code>+:literal</code> not possible)</td></tr><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>
|
1583
|
+
<table class="cube_table"
|
1584
|
+
id="union_cube"
|
1585
|
+
title="require 'zucker/union'"> <tr><th>Summary</th> <td>Easy creation of <code>Regexp.union</code>s.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#|, String#|</h5><pre class="usage source" style="display:block"><span class="rx"><span class="dl">/</span><span class="k">Ruby</span><span class="ch">\d</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="k">test</span><span class="dl">/</span><span class="mod">i</span></span> | <span class="s"><span class="dl">"</span><span class="k">cheat</span><span class="dl">"</span></span>
|
1586
|
+
<span class="c"># creates a Regexp similar to:</span>
|
1587
|
+
<span class="c"># /(Ruby\d|[tT][eE][sS][tT]|cheat)/</span>
|
1588
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_union_spec">(<a href="javascript:show('union_spec')">show</a>)</span>
|
1589
|
+
<pre class="source" id="union_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/union</span><span class="dl">'</span></span>
|
1590
|
+
|
1591
|
+
shared_examples_for <span class="s"><span class="dl">"</span><span class="k">Regexp.union operator</span><span class="dl">"</span></span> <span class="r">do</span>
|
1592
|
+
it <span class="s"><span class="dl">"</span><span class="k">should create an Regexp.union of both operands</span><span class="dl">"</span></span> <span class="r">do</span>
|
1593
|
+
(<span class="rx"><span class="dl">/</span><span class="k">Ruby</span><span class="ch">\d</span><span class="dl">/</span></span> | <span class="rx"><span class="dl">/</span><span class="k">test</span><span class="dl">/</span><span class="mod">i</span></span> | <span class="s"><span class="dl">"</span><span class="k">cheat</span><span class="dl">"</span></span>).should ==
|
1594
|
+
<span class="co">Regexp</span>.union( <span class="co">Regexp</span>.union( <span class="rx"><span class="dl">/</span><span class="k">Ruby</span><span class="ch">\d</span><span class="dl">/</span></span>, <span class="rx"><span class="dl">/</span><span class="k">test</span><span class="dl">/</span><span class="mod">i</span></span> ), <span class="s"><span class="dl">"</span><span class="k">cheat</span><span class="dl">"</span></span> )
|
1595
|
+
<span class="r">end</span>
|
1596
|
+
<span class="r">end</span>
|
1597
|
+
|
1598
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1599
|
+
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1600
|
+
<span class="r">end</span>
|
1601
|
+
|
1602
|
+
describe <span class="s"><span class="dl">'</span><span class="k">String#|</span><span class="dl">'</span></span> <span class="r">do</span>
|
1603
|
+
it_should_behave_like <span class="s"><span class="dl">'</span><span class="k">Regexp.union operator</span><span class="dl">'</span></span>
|
1604
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_union_source">(<a href="javascript:show('union_source')">show</a>)</span>
|
1605
|
+
<pre class="source" id="union_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1606
|
+
|
1607
|
+
<span class="r">class</span> <span class="cl">Regexp</span>
|
1608
|
+
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1609
|
+
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1610
|
+
<span class="r">end</span>
|
1611
|
+
<span class="r">end</span>
|
1612
|
+
|
1613
|
+
<span class="r">class</span> <span class="cl">String</span>
|
1614
|
+
<span class="r">def</span> <span class="fu">|</span>(arg)
|
1615
|
+
<span class="co">Regexp</span>.union <span class="pc">self</span>, arg.is_a?(<span class="co">Regexp</span>) ? arg : arg.to_s
|
1616
|
+
<span class="r">end</span>
|
1617
|
+
<span class="r">end</span>
|
1618
|
+
|
1619
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1620
|
+
</div>
|
1621
|
+
<h2 title="require 'zucker/object'" id="object">Cubes[object]</h2>
|
1622
|
+
|
1623
|
+
<div class="cubes">
|
1624
|
+
<h3 title="require 'zucker/blank'" id="blank">blank</h3>
|
1625
|
+
<table class="cube_table"
|
1626
|
+
id="blank_cube"
|
1627
|
+
title="require 'zucker/blank'"> <tr><th>Summary</th> <td>Does pretty the same as in ActiveSupport (Every object can be asked if it is blank).</td></tr><tr><th>Why?</th> <td>It's too familiar ;)</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#blank?</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">'</span><span class="k">an object</span><span class="dl">'</span></span>.blank? <span class="c"># => false</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_blank_spec">(<a href="javascript:show('blank_spec')">show</a>)</span>
|
1628
|
+
<pre class="source" id="blank_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/blank</span><span class="dl">'</span></span>
|
1629
|
+
|
1630
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#blank?</span><span class="dl">'</span></span> <span class="r">do</span>
|
1631
|
+
it <span class="s"><span class="dl">'</span><span class="k">should be blank for blank values</span><span class="dl">'</span></span> <span class="r">do</span>
|
1632
|
+
blank_values = [ <span class="pc">nil</span>, <span class="pc">false</span>, <span class="s"><span class="dl">'</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span>, <span class="s"><span class="dl">"</span><span class="k"> </span><span class="ch">\n</span><span class="ch">\t</span><span class="k"> </span><span class="ch">\r</span><span class="k"> </span><span class="dl">"</span></span>, [], {}, <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> ]
|
1633
|
+
|
1634
|
+
blank_values.each{ |blank|
|
1635
|
+
blank.blank?.should == <span class="pc">true</span>
|
1636
|
+
}
|
1637
|
+
<span class="r">end</span>
|
1638
|
+
|
1639
|
+
it <span class="s"><span class="dl">'</span><span class="k">should not be blank for non blank values</span><span class="dl">'</span></span> <span class="r">do</span>
|
1640
|
+
present_values = [ <span class="co">Object</span>.new, <span class="pc">true</span>, <span class="i">0</span>, <span class="i">1</span>, <span class="s"><span class="dl">'</span><span class="k">a</span><span class="dl">'</span></span>, [<span class="pc">nil</span>], { <span class="pc">nil</span> => <span class="pc">nil</span> } ]
|
1641
|
+
|
1642
|
+
present_values.each{ |present|
|
1643
|
+
present.blank?.should == <span class="pc">false</span>
|
1644
|
+
}
|
1645
|
+
<span class="r">end</span>
|
1646
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_blank_source">(<a href="javascript:show('blank_source')">show</a>)</span>
|
1647
|
+
<pre class="source" id="blank_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1648
|
+
|
1649
|
+
<span class="r">class</span> <span class="cl">Object</span>
|
1650
|
+
<span class="r">def</span> <span class="fu">blank?</span>
|
1651
|
+
<span class="r">if</span> respond_to? <span class="sy">:empty?</span> <span class="r">then</span> empty? <span class="r">else</span> !<span class="pc">self</span> <span class="r">end</span>
|
1652
|
+
<span class="r">end</span>
|
1653
|
+
|
1654
|
+
<span class="r">def</span> <span class="fu">present?</span>
|
1655
|
+
!blank?
|
1656
|
+
<span class="r">end</span>
|
1657
|
+
<span class="r">end</span>
|
1658
|
+
|
1659
|
+
|
1660
|
+
{ <span class="c"># what to do # for which classes</span>
|
1661
|
+
lambda{ <span class="pc">true</span> } => [<span class="co">FalseClass</span>, <span class="co">NilClass</span>],
|
1662
|
+
lambda{ <span class="pc">false</span> } => [<span class="co">TrueClass</span>, <span class="co">Numeric</span>],
|
1663
|
+
lambda{ empty? } => [<span class="co">Array</span>, <span class="co">Hash</span>],
|
1664
|
+
lambda{ <span class="pc">self</span> !~ <span class="rx"><span class="dl">/</span><span class="ch">\S</span><span class="dl">/</span></span> } => [<span class="co">String</span>],
|
1665
|
+
lambda{ <span class="pc">self</span> == <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> } => [<span class="co">Regexp</span>],
|
1666
|
+
|
1667
|
+
}.each{ |action, klass_array|
|
1668
|
+
klass_array.each{ |klass|
|
1669
|
+
klass.send <span class="sy">:define_method</span>, <span class="sy">:blank?</span>, &action
|
1670
|
+
}
|
1671
|
+
}
|
1672
|
+
|
1673
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1674
|
+
<table class="cube_table"
|
1675
|
+
id="mcopy_cube"
|
1676
|
+
title="require 'zucker/mcopy'"> <tr><th>Summary</th> <td>Adds <code>Object#mcopy</code> to create a deep copy using <code>Marshal</code>.</td></tr><tr><th>Why?</th> <td>Every Ruby book describes, you can do <code>Marshal.load Marshal.dump object</code> to create a deep copy... But who needs this verbose syntax in everyday coding?</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#mcopy</h5><pre class="usage source" style="display:block">a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
1677
|
+
b = a.mcopy
|
1678
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_mcopy_spec">(<a href="javascript:show('mcopy_spec')">show</a>)</span>
|
1679
|
+
<pre class="source" id="mcopy_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mcopy</span><span class="dl">'</span></span>
|
1680
|
+
|
1681
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#mcopy</span><span class="dl">'</span></span> <span class="r">do</span>
|
1682
|
+
it <span class="s"><span class="dl">'</span><span class="k">create a (deep) copy via marshalling</span><span class="dl">'</span></span> <span class="r">do</span>
|
1683
|
+
a = <span class="s"><span class="dl">%w[</span><span class="k">hello world</span><span class="dl">]</span></span>
|
1684
|
+
b = a.mcopy
|
1685
|
+
b.should == a
|
1686
|
+
|
1687
|
+
b[<span class="i">0</span>][<span class="i">1</span>,<span class="i">1</span>] = <span class="s"><span class="dl">'</span><span class="dl">'</span></span>
|
1688
|
+
b.should_not == a
|
1689
|
+
<span class="r">end</span>
|
1690
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mcopy_source">(<a href="javascript:show('mcopy_source')">show</a>)</span>
|
1691
|
+
<pre class="source" id="mcopy_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1692
|
+
|
1693
|
+
<span class="r">class</span> <span class="cl">Object</span>
|
1694
|
+
<span class="r">def</span> <span class="fu">mcopy</span>
|
1695
|
+
<span class="co">Marshal</span>.load <span class="co">Marshal</span>.dump <span class="pc">self</span>
|
1696
|
+
<span class="r">end</span>
|
1697
|
+
<span class="r">end</span>
|
1698
|
+
|
1699
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1700
|
+
<table class="cube_table"
|
1701
|
+
id="not_cube"
|
1702
|
+
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 inverted boolean.</td></tr><tr><th>Why?</th> <td>Better readability.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#not</h5><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].not.empty? <span class="c"># true</span>
|
1703
|
+
</pre></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>
|
1704
|
+
<pre class="source" id="not_spec"><span class="r">if</span> <span class="r">defined?</span> <span class="co">BasicObject</span> <span class="c"># 1.9 only</span>
|
1705
|
+
|
1706
|
+
require <span class="s"><span class="dl">'</span><span class="k">zucker/not</span><span class="dl">'</span></span>
|
1707
|
+
|
1708
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#not</span><span class="dl">'</span></span> <span class="r">do</span>
|
1709
|
+
it <span class="s"><span class="dl">"</span><span class="k">should return an object on which all methods are redirected to it's receiver object, but return an inverted boolean</span><span class="dl">"</span></span> <span class="r">do</span>
|
1710
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].not.empty?.should == <span class="pc">true</span>
|
1711
|
+
<span class="pc">nil</span>.not.nil?.should == <span class="pc">false</span>
|
1712
|
+
<span class="r">end</span>
|
1713
|
+
<span class="r">end</span>
|
1714
|
+
|
1715
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_not_source">(<a href="javascript:show('not_source')">show</a>)</span>
|
1716
|
+
<pre class="source" id="not_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1717
|
+
|
1718
|
+
<span class="r">class</span> <span class="cl">Object</span>
|
1719
|
+
<span class="r">def</span> <span class="fu">not</span>
|
1720
|
+
<span class="co">NotClass</span>.new <span class="pc">self</span>
|
1721
|
+
<span class="r">end</span>
|
1722
|
+
|
1723
|
+
<span class="r">class</span> <span class="cl">NotClass</span> < <span class="co">BasicObject</span>
|
1724
|
+
<span class="r">def</span> <span class="fu">initialize</span>(receiver)
|
1725
|
+
<span class="iv">@receiver</span> = receiver
|
1726
|
+
<span class="r">end</span>
|
1727
|
+
|
1728
|
+
<span class="r">def</span> <span class="fu">method_missing</span>(m, *args, &block)
|
1729
|
+
<span class="r">not</span> <span class="iv">@receiver</span>.public_send( m, *args, &block )
|
1730
|
+
<span class="r">end</span>
|
1731
|
+
<span class="r">end</span>
|
1732
|
+
<span class="r">end</span>
|
1733
|
+
|
1734
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1735
|
+
</div>
|
1736
|
+
<h2 title="require 'zucker/to_proc'" id="to_proc">Cubes[to_proc]</h2>
|
1737
|
+
|
1738
|
+
<div class="cubes">
|
1739
|
+
<h3 title="require 'zucker/array_to_proc'" id="array_to_proc">array_to_proc</h3>
|
1740
|
+
<table class="cube_table"
|
1741
|
+
id="array_to_proc_cube"
|
1742
|
+
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><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[<span class="sy">:*</span>, <span class="i">5</span>] <span class="c"># => [5, 10, 15, 20]</span>
|
1743
|
+
<span class="c"># you can also chain them, if the first parameter is an Array</span>
|
1744
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map &[[<span class="sy">:to_s</span>, <span class="i">2</span>],[<span class="sy">:+</span>, <span class="s"><span class="dl">'</span><span class="k">b</span><span class="dl">'</span></span>]] <span class="c"># => ["1b", "10b", "11b", "100b"]</span>
|
1745
|
+
</pre></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>
|
1746
|
+
<pre class="source" id="array_to_proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/array_to_proc</span><span class="dl">'</span></span>
|
1747
|
+
|
1748
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Array#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1749
|
+
it <span class="s"><span class="dl">'</span><span class="k">should call the method of the first symbol, using the remaining elements as paramaters</span><span class="dl">'</span></span> <span class="r">do</span>
|
1750
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[<span class="sy">:to_s</span>, <span class="i">2</span>] ).should == [<span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">10</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">11</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">100</span><span class="dl">"</span></span>]
|
1751
|
+
<span class="r">end</span>
|
1752
|
+
|
1753
|
+
it <span class="s"><span class="dl">"</span><span class="k">should convert each element to a proc and chain it, if the first parameter is an array</span><span class="dl">"</span></span> <span class="r">do</span>
|
1754
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map( &[[<span class="sy">:*</span>,<span class="i">2</span>],[<span class="sy">:+</span>,<span class="i">4</span>]] ).should == [<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map{|i| i*<span class="i">2</span> + <span class="i">4</span> }
|
1755
|
+
<span class="r">end</span>
|
1756
|
+
<span class="r">end</span></pre> </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>
|
1757
|
+
<pre class="source" id="array_to_proc_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1758
|
+
|
1759
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
1760
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
1761
|
+
<span class="co">Proc</span>.new{ |obj|
|
1762
|
+
<span class="r">if</span> <span class="pc">self</span>.first.is_a? <span class="co">Array</span>
|
1763
|
+
<span class="pc">self</span>.inject(obj){ |result, nested_array|
|
1764
|
+
nested_array.to_proc.call result
|
1765
|
+
}
|
1766
|
+
<span class="r">else</span>
|
1767
|
+
obj.send *<span class="pc">self</span>
|
1768
|
+
<span class="r">end</span>
|
1769
|
+
}
|
1770
|
+
<span class="r">end</span>
|
1771
|
+
<span class="r">end</span>
|
1772
|
+
|
1773
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1774
|
+
<table class="cube_table"
|
1775
|
+
id="class_to_proc_cube"
|
1776
|
+
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><pre class="usage source" style="display:block">[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&<span class="co">Set</span>) <span class="c"># => [ Set[1,2], Set[5,6,7,3] ]</span></pre></td></tr><tr><th>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>
|
1777
|
+
<pre class="source" id="class_to_proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/class_to_proc</span><span class="dl">'</span></span>
|
1778
|
+
require <span class="s"><span class="dl">'</span><span class="k">set</span><span class="dl">'</span></span>
|
1779
|
+
|
1780
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Class#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1781
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create new instances of the class</span><span class="dl">'</span></span> <span class="r">do</span>
|
1782
|
+
[ [<span class="i">1</span>,<span class="i">2</span>],[<span class="i">3</span>,<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ].map(&<span class="co">Set</span>).should == [ <span class="co">Set</span>[<span class="i">1</span>,<span class="i">2</span>], <span class="co">Set</span>[<span class="i">5</span>,<span class="i">6</span>,<span class="i">7</span>,<span class="i">3</span>] ]
|
1783
|
+
<span class="r">end</span>
|
1784
|
+
<span class="r">end</span></pre> </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>
|
1785
|
+
<pre class="source" id="class_to_proc_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1786
|
+
|
1787
|
+
<span class="r">class</span> <span class="cl">Class</span>
|
1788
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
1789
|
+
<span class="co">Proc</span>.new <span class="r">do</span> |*args|
|
1790
|
+
<span class="pc">self</span>.new *args
|
1791
|
+
<span class="r">end</span>
|
1792
|
+
<span class="r">end</span>
|
1793
|
+
<span class="r">end</span>
|
1794
|
+
|
1795
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1796
|
+
<table class="cube_table"
|
1797
|
+
id="hash_to_proc_cube"
|
1798
|
+
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><pre class="usage source" style="display:block">[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&{
|
1799
|
+
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>},
|
1800
|
+
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
1801
|
+
}) <span class="c"># => [1, 1002, 3, '4']</span></pre></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>
|
1802
|
+
<pre class="source" id="hash_to_proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/hash_to_proc</span><span class="dl">'</span></span>
|
1803
|
+
|
1804
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Hash#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1805
|
+
it <span class="s"><span class="dl">'</span><span class="k">should run the proc given in the value for a key in the hash</span><span class="dl">'</span></span> <span class="r">do</span>
|
1806
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>,<span class="i">4</span>].map(&{
|
1807
|
+
<span class="i">4</span> => <span class="sy">:to_s</span>,
|
1808
|
+
<span class="c"># 3 => [:to_s, 2] # "11" => if array2proc is used</span>
|
1809
|
+
<span class="i">2</span> => lambda {|e| e + <span class="i">1000</span>}
|
1810
|
+
}).should == [<span class="i">1</span>, <span class="i">1002</span>, <span class="i">3</span>, <span class="s"><span class="dl">"</span><span class="k">4</span><span class="dl">"</span></span>]
|
1811
|
+
<span class="r">end</span>
|
1812
|
+
<span class="r">end</span></pre> </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>
|
1813
|
+
<pre class="source" id="hash_to_proc_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1814
|
+
|
1815
|
+
<span class="r">class</span> <span class="cl">Hash</span>
|
1816
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
1817
|
+
<span class="co">Proc</span>.new{ |obj|
|
1818
|
+
<span class="r">if</span> <span class="pc">self</span>.member? obj
|
1819
|
+
<span class="pc">self</span>[obj].to_proc.call obj
|
1820
|
+
<span class="r">else</span>
|
1821
|
+
obj
|
1822
|
+
<span class="r">end</span>
|
1823
|
+
}
|
1824
|
+
<span class="r">end</span>
|
1825
|
+
<span class="r">end</span>
|
1826
|
+
|
1827
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1828
|
+
<table class="cube_table"
|
1829
|
+
id="regexp_to_proc_cube"
|
1830
|
+
title="require 'zucker/regexp_to_proc'"> <tr><th>Summary</th> <td>Use <code>&/regex/</code> to match it against strings.</td></tr><tr><th>Methods/Usage</th> <td><h5>Regexp#to_proc</h5><pre class="usage source" style="display:block"><span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["j", nil, nil, "y"]</span>
|
1831
|
+
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span> <span class="c"># => ["just", "array"]</span>
|
1832
|
+
</pre></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>
|
1833
|
+
<pre class="source" id="regexp_to_proc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/regexp_to_proc</span><span class="dl">'</span></span>
|
1834
|
+
|
1835
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#to_proc</span><span class="dl">'</span></span> <span class="r">do</span>
|
1836
|
+
it <span class="s"><span class="dl">'</span><span class="k">should match the regex</span><span class="dl">'</span></span> <span class="r">do</span>
|
1837
|
+
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.map( &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">"</span><span class="k">j</span><span class="dl">"</span></span>, <span class="pc">nil</span>, <span class="pc">nil</span>, <span class="s"><span class="dl">"</span><span class="k">y</span><span class="dl">"</span></span>]
|
1838
|
+
<span class="s"><span class="dl">%w|</span><span class="k">just another string array</span><span class="dl">|</span></span>.select( &<span class="rx"><span class="dl">/</span><span class="k">[jy]</span><span class="dl">/</span></span>).should == [<span class="s"><span class="dl">"</span><span class="k">just</span><span class="dl">"</span></span>, <span class="s"><span class="dl">"</span><span class="k">array</span><span class="dl">"</span></span>]
|
1839
|
+
<span class="r">end</span>
|
1840
|
+
<span class="r">end</span></pre> </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>
|
1841
|
+
<pre class="source" id="regexp_to_proc_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1842
|
+
|
1843
|
+
<span class="r">class</span> <span class="cl">Regexp</span>
|
1844
|
+
<span class="r">def</span> <span class="fu">to_proc</span>
|
1845
|
+
proc <span class="r">do</span> |e|
|
1846
|
+
e.to_s[<span class="pc">self</span>]
|
1847
|
+
<span class="r">end</span>
|
1848
|
+
<span class="r">end</span>
|
1849
|
+
<span class="r">end</span>
|
1850
|
+
|
1851
|
+
<span class="c"># J-_-L</span></pre> </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>
|
1852
|
+
</div>
|
1853
|
+
<h2 title="require 'zucker/shortcuts'" id="shortcuts">Cubes[shortcuts]</h2>
|
1854
|
+
|
1855
|
+
<div class="cubes">
|
1856
|
+
<h3 title="require 'zucker/alias_for'" id="alias_for">alias_for</h3>
|
1857
|
+
<table class="cube_table"
|
1858
|
+
id="alias_for_cube"
|
1859
|
+
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><pre class="usage source" style="display:block">alias_for <span class="sy">:methods</span>, <span class="sy">:ms</span> <span class="c"># creates an alias for the method :methods with the name ms</span>
|
1860
|
+
<span class="r">module</span> <span class="cl">Enumerable</span>
|
1861
|
+
aliases_for <span class="sy">:zip</span>, <span class="sy">:with</span>, <span class="sy">:%</span>
|
1862
|
+
<span class="r">end</span> <span class="c"># creates multiple aliases</span>
|
1863
|
+
</pre><h5>Module#method_alias_for, Module#method_aliases_for</h5><pre class="usage source" style="display:block"><span class="c"># same as alias_for, but using Module#alias_method instead of alias</span>
|
1864
|
+
</pre></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>
|
1865
|
+
<pre class="source" id="alias_for_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/alias_for</span><span class="dl">'</span></span>
|
1866
|
+
|
1867
|
+
describe <span class="s"><span class="dl">'</span><span class="k">alias_for</span><span class="dl">'</span></span> <span class="r">do</span>
|
1868
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create an alias for global methods</span><span class="dl">'</span></span> <span class="r">do</span>
|
1869
|
+
<span class="c"># rspec bug? </span>
|
1870
|
+
<span class="c"># def m1</span>
|
1871
|
+
<span class="c"># 1</span>
|
1872
|
+
<span class="c"># end</span>
|
1873
|
+
<span class="c"># alias_for :m1, :a1</span>
|
1874
|
+
<span class="c">#</span>
|
1875
|
+
<span class="c"># proc do</span>
|
1876
|
+
<span class="c"># a1.should == 1</span>
|
1877
|
+
<span class="c"># end.should_not raise_exception</span>
|
1878
|
+
<span class="r">end</span>
|
1879
|
+
|
1880
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create an alias for instance methods</span><span class="dl">'</span></span> <span class="r">do</span>
|
1881
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
1882
|
+
<span class="r">def</span> <span class="fu">m2</span>
|
1883
|
+
<span class="i">2</span>
|
1884
|
+
<span class="r">end</span>
|
1885
|
+
alias_for <span class="sy">:m2</span>, <span class="sy">:a2</span>
|
1886
|
+
<span class="r">end</span>
|
1887
|
+
proc <span class="r">do</span>
|
1888
|
+
|
1889
|
+
[<span class="i">1</span>,<span class="i">2</span>,<span class="i">3</span>].a2.should == <span class="i">2</span>
|
1890
|
+
<span class="r">end</span>.should_not raise_exception
|
1891
|
+
<span class="r">end</span>
|
1892
|
+
|
1893
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create an alias for class (singleton) methods</span><span class="dl">'</span></span> <span class="r">do</span>
|
1894
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
1895
|
+
<span class="r">class</span> << <span class="cl">Array</span>
|
1896
|
+
<span class="r">def</span> <span class="fu">m3</span>
|
1897
|
+
<span class="i">3</span>
|
1898
|
+
<span class="r">end</span>
|
1899
|
+
alias_for <span class="sy">:m3</span>, <span class="sy">:a3</span>
|
1900
|
+
<span class="r">end</span>
|
1901
|
+
<span class="r">end</span>
|
1902
|
+
|
1903
|
+
proc <span class="r">do</span>
|
1904
|
+
<span class="co">Array</span>.a3.should == <span class="i">3</span>
|
1905
|
+
<span class="r">end</span>.should_not raise_exception
|
1906
|
+
<span class="r">end</span>
|
1907
|
+
|
1908
|
+
|
1909
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create aliases for the first argument with all other arguments</span><span class="dl">'</span></span> <span class="r">do</span>
|
1910
|
+
<span class="r">class</span> <span class="cl">Object</span>
|
1911
|
+
<span class="r">def</span> <span class="fu">m4</span>
|
1912
|
+
<span class="i">4</span>
|
1913
|
+
<span class="r">end</span>
|
1914
|
+
alias_for <span class="sy">:m4</span>, <span class="sy">:ma</span>, <span class="sy">:mb</span>, <span class="sy">:mc</span>
|
1915
|
+
<span class="r">end</span>
|
1916
|
+
proc <span class="r">do</span>
|
1917
|
+
<span class="i">1</span>.ma.should == <span class="i">4</span>
|
1918
|
+
<span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>.mb.should == <span class="i">4</span>
|
1919
|
+
[<span class="i">1</span>].mc.should == <span class="i">4</span>
|
1920
|
+
<span class="r">end</span>.should_not raise_exception
|
1921
|
+
<span class="r">end</span>
|
1922
|
+
<span class="r">end</span>
|
1923
|
+
|
1924
|
+
<span class="c"># alias_method</span>
|
1925
|
+
<span class="r">module</span> <span class="cl">Blip</span>
|
1926
|
+
<span class="r">def</span> <span class="fu">blip</span>
|
1927
|
+
<span class="s"><span class="dl">'</span><span class="k">blip</span><span class="dl">'</span></span>
|
1928
|
+
<span class="r">end</span>
|
1929
|
+
alias_methods_for <span class="sy">:blip</span>, <span class="sy">:blap</span>, <span class="sy">:blup</span>
|
1930
|
+
|
1931
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
1932
|
+
<span class="r">def</span> <span class="fu">self_blip</span>
|
1933
|
+
<span class="s"><span class="dl">'</span><span class="k">blip</span><span class="dl">'</span></span>
|
1934
|
+
<span class="r">end</span>
|
1935
|
+
alias_methods_for <span class="sy">:self_blip</span>, <span class="sy">:self_blap</span>, <span class="sy">:self_blup</span>
|
1936
|
+
<span class="r">end</span>
|
1937
|
+
<span class="r">end</span>
|
1938
|
+
|
1939
|
+
<span class="r">class</span> <span class="cl">Hello</span>
|
1940
|
+
include <span class="co">Blip</span>
|
1941
|
+
|
1942
|
+
<span class="r">def</span> <span class="fu">hello</span>
|
1943
|
+
<span class="s"><span class="dl">'</span><span class="k">hello</span><span class="dl">'</span></span>
|
1944
|
+
<span class="r">end</span>
|
1945
|
+
alias_methods_for <span class="sy">:hello</span>, <span class="sy">:hi</span>, <span class="sy">:howdy</span>
|
1946
|
+
|
1947
|
+
<span class="r">class</span> << <span class="cl">self</span>
|
1948
|
+
<span class="r">def</span> <span class="fu">self_hello</span>
|
1949
|
+
<span class="s"><span class="dl">'</span><span class="k">hello</span><span class="dl">'</span></span>
|
1950
|
+
<span class="r">end</span>
|
1951
|
+
alias_methods_for <span class="sy">:self_hello</span>, <span class="sy">:self_hi</span>, <span class="sy">:self_howdy</span>
|
1952
|
+
<span class="r">end</span>
|
1953
|
+
<span class="r">end</span>
|
1954
|
+
|
1955
|
+
describe <span class="s"><span class="dl">'</span><span class="k">alias_methods_for</span><span class="dl">'</span></span> <span class="r">do</span>
|
1956
|
+
let(<span class="sy">:h</span>) { <span class="co">Hello</span>.new }
|
1957
|
+
|
1958
|
+
context <span class="s"><span class="dl">"</span><span class="k">module context</span><span class="dl">"</span></span> <span class="r">do</span>
|
1959
|
+
it <span class="s"><span class="dl">"</span><span class="k">should alias instance methods</span><span class="dl">"</span></span> <span class="r">do</span>
|
1960
|
+
h.blap.should == h.blip
|
1961
|
+
h.blup.should == h.blip
|
1962
|
+
<span class="r">end</span>
|
1963
|
+
|
1964
|
+
it <span class="s"><span class="dl">"</span><span class="k">should alias class methods</span><span class="dl">"</span></span> <span class="r">do</span>
|
1965
|
+
<span class="co">Blip</span>.self_blap.should == <span class="co">Blip</span>.self_blip
|
1966
|
+
<span class="co">Blip</span>.self_blup.should == <span class="co">Blip</span>.self_blip
|
1967
|
+
<span class="r">end</span>
|
1968
|
+
<span class="r">end</span>
|
1969
|
+
|
1970
|
+
context <span class="s"><span class="dl">"</span><span class="k">class context</span><span class="dl">"</span></span> <span class="r">do</span>
|
1971
|
+
it <span class="s"><span class="dl">"</span><span class="k">should alias instance methods</span><span class="dl">"</span></span> <span class="r">do</span>
|
1972
|
+
h.hi.should == h.hello
|
1973
|
+
h.howdy.should == h.hello
|
1974
|
+
<span class="r">end</span>
|
1975
|
+
|
1976
|
+
it <span class="s"><span class="dl">"</span><span class="k">should alias class methods</span><span class="dl">"</span></span> <span class="r">do</span>
|
1977
|
+
<span class="co">Hello</span>.self_hi.should == <span class="co">Hello</span>.self_hello
|
1978
|
+
<span class="co">Hello</span>.self_howdy.should == <span class="co">Hello</span>.self_hello
|
1979
|
+
<span class="r">end</span>
|
1980
|
+
<span class="r">end</span>
|
1981
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_alias_for_source">(<a href="javascript:show('alias_for_source')">show</a>)</span>
|
1982
|
+
<pre class="source" id="alias_for_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
1983
|
+
|
1984
|
+
<span class="r">def</span> <span class="fu">alias_for</span>(m, *aliases)
|
1985
|
+
aliases.each{ |a|
|
1986
|
+
class_eval <span class="s"><span class="dl">"</span><span class="k">alias </span><span class="il"><span class="idl">#{</span>a<span class="idl">}</span></span><span class="k"> </span><span class="il"><span class="idl">#{</span>m<span class="idl">}</span></span><span class="dl">"</span></span>
|
1987
|
+
}
|
1988
|
+
<span class="r">end</span>
|
1989
|
+
<span class="r">alias</span> <span class="fu">aliases_for</span> <span class="fu">alias_for</span>
|
1990
|
+
|
1991
|
+
<span class="r">class</span> <span class="cl">Module</span>
|
1992
|
+
<span class="r">def</span> <span class="fu">alias_method_for</span>(m, *alias_methods)
|
1993
|
+
alias_methods.each{ |a|
|
1994
|
+
class_eval <span class="r">do</span>
|
1995
|
+
alias_method a.to_sym, m.to_sym
|
1996
|
+
<span class="r">end</span>
|
1997
|
+
}
|
1998
|
+
<span class="r">end</span>
|
1999
|
+
<span class="r">alias</span> <span class="fu">alias_methods_for</span> <span class="fu">alias_method_for</span>
|
2000
|
+
<span class="r">end</span>
|
2001
|
+
|
2002
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2003
|
+
<table class="cube_table"
|
2004
|
+
id="aliases_cube"
|
2005
|
+
title="require 'zucker/aliases'"> <tr><th>Summary</th> <td>Some convenient aliases (and constants) for existing methods.</td></tr><tr><th>Methods/Usage</th> <td><h5>Object#is_an?</h5><pre class="usage source" style="display:block"><span class="co">Object</span>.is_a?</pre><h5>Enumerable#with</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Enumerable#%</h5><pre class="usage source" style="display:block"><span class="co">Enumerable</span><span class="c">#zip</span></pre><h5>Array#**</h5><pre class="usage source" style="display:block"><span class="co">Array</span><span class="c">#product</span></pre><h5>Hash#+</h5><pre class="usage source" style="display:block"><span class="co">Hash</span><span class="c">#merge</span></pre><h5>Binding#[]</h5><pre class="usage source" style="display:block"><span class="co">Binding</span><span class="c">#eval</span></pre><h5>File.filename</h5><pre class="usage source" style="display:block"><span class="co">File</span>.basename</pre><h5>Dir.join</h5><pre class="usage source" style="display:block"><span class="co">File</span>.join</pre><h5>Dir.split</h5><pre class="usage source" style="display:block"><span class="co">File</span>.split</pre><h5>Infinity</h5><pre class="usage source" style="display:block"><span class="fl">1.0</span> / <span class="fl">0.0</span></pre><h5>NaN</h5><pre class="usage source" style="display:block"><span class="fl">0.0</span> / <span class="fl">0.0</span></pre></td></tr><tr><th>Specification</th> <td> <span id="show_aliases_spec">(<a href="javascript:show('aliases_spec')">show</a>)</span>
|
2006
|
+
<pre class="source" id="aliases_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/aliases</span><span class="dl">'</span></span>
|
2007
|
+
|
2008
|
+
describe <span class="s"><span class="dl">'</span><span class="k">(aliases)</span><span class="dl">'</span></span> <span class="r">do</span>
|
2009
|
+
it <span class="s"><span class="dl">'</span><span class="k">should create these aliases</span><span class="dl">'</span></span> <span class="r">do</span>
|
2010
|
+
<span class="c"># see aliases.rb for aliase list</span>
|
2011
|
+
<span class="r">end</span>
|
2012
|
+
|
2013
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define these constants</span><span class="dl">'</span></span> <span class="r">do</span>
|
2014
|
+
<span class="co">Infinity</span>.finite?.should == <span class="pc">false</span>
|
2015
|
+
<span class="co">NaN</span>.nan?.should == <span class="pc">true</span>
|
2016
|
+
<span class="r">end</span>
|
2017
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_aliases_source">(<a href="javascript:show('aliases_source')">show</a>)</span>
|
2018
|
+
<pre class="source" id="aliases_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2019
|
+
|
2020
|
+
<span class="r">class</span> <span class="cl">Object</span>
|
2021
|
+
<span class="r">alias</span> <span class="fu">is_an?</span> <span class="fu">is_a?</span> <span class="c"># thanks to utility_belt</span>
|
2022
|
+
<span class="r">end</span>
|
2023
|
+
|
2024
|
+
<span class="r">module</span> <span class="cl">Enumerable</span>
|
2025
|
+
<span class="r">alias</span> <span class="fu">with</span> <span class="fu">zip</span>
|
2026
|
+
<span class="r">alias</span> <span class="fu">%</span> <span class="fu">zip</span>
|
2027
|
+
<span class="r">end</span>
|
2028
|
+
|
2029
|
+
<span class="r">class</span> <span class="cl">Array</span>
|
2030
|
+
<span class="r">alias</span> <span class="fu">**</span> <span class="fu">product</span>
|
2031
|
+
<span class="r">alias</span> <span class="fu">contains?</span> <span class="fu">include?</span>
|
2032
|
+
<span class="r">end</span>
|
2033
|
+
|
2034
|
+
<span class="r">class</span> <span class="cl">String</span>
|
2035
|
+
<span class="r">alias</span> <span class="fu">contains?</span> <span class="fu">include?</span>
|
2036
|
+
<span class="r">end</span>
|
2037
|
+
|
2038
|
+
<span class="r">class</span> <span class="cl">Hash</span>
|
2039
|
+
<span class="r">alias</span> <span class="fu">+</span> <span class="fu">merge</span>
|
2040
|
+
<span class="r">end</span>
|
2041
|
+
|
2042
|
+
<span class="r">class</span> <span class="cl">Binding</span>
|
2043
|
+
<span class="c">#alias [] eval</span>
|
2044
|
+
<span class="r">def</span> <span class="fu">[]</span>(expr)
|
2045
|
+
<span class="pc">self</span>.eval <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>expr<span class="idl">}</span></span><span class="dl">"</span></span>
|
2046
|
+
<span class="r">end</span>
|
2047
|
+
<span class="r">end</span>
|
2048
|
+
|
2049
|
+
<span class="r">class</span> << <span class="cl">File</span>
|
2050
|
+
<span class="r">alias</span> <span class="fu">filename</span> <span class="fu">basename</span> <span class="c"># thanks rdp :)</span>
|
2051
|
+
<span class="r">end</span>
|
2052
|
+
|
2053
|
+
<span class="r">class</span> << <span class="cl">Dir</span>
|
2054
|
+
<span class="r">def</span> <span class="fu">join</span>(*args)
|
2055
|
+
<span class="co">File</span>.join(*args)
|
2056
|
+
<span class="r">end</span>
|
2057
|
+
|
2058
|
+
<span class="r">def</span> <span class="fu">split</span>(*args)
|
2059
|
+
<span class="co">File</span>.split(*args)
|
2060
|
+
<span class="r">end</span>
|
2061
|
+
<span class="r">end</span>
|
2062
|
+
|
2063
|
+
<span class="c"># constants - who would use these in real-world code for other things?</span>
|
2064
|
+
<span class="co">Infinity</span> = <span class="fl">1.0</span> / <span class="fl">0.0</span> <span class="c"># or 2*Float::MAX or Float::INFINITY</span>
|
2065
|
+
<span class="co">NaN</span> = <span class="fl">0.0</span> / <span class="fl">0.0</span>
|
2066
|
+
|
2067
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2068
|
+
<table class="cube_table"
|
2069
|
+
id="ivars_cube"
|
2070
|
+
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><pre class="usage source" style="display:block"><span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
2071
|
+
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
2072
|
+
|
2073
|
+
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
2074
|
+
instance_variables_from params <span class="c"># # assigns @c and @d</span>
|
2075
|
+
<span class="r">end</span>
|
2076
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_ivars_spec">(<a href="javascript:show('ivars_spec')">show</a>)</span>
|
2077
|
+
<pre class="source" id="ivars_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/ivars</span><span class="dl">'</span></span>
|
2078
|
+
|
2079
|
+
describe <span class="s"><span class="dl">'</span><span class="k">instance_variables_from</span><span class="dl">'</span></span> <span class="r">do</span>
|
2080
|
+
it <span class="s"><span class="dl">'</span><span class="k">should tansform the given parameter to instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
2081
|
+
<span class="r">def</span> <span class="fu">a_method</span>(a = <span class="i">1</span>, b = <span class="i">2</span>)
|
2082
|
+
instance_variables_from binding <span class="c"># assigns @a and @b</span>
|
2083
|
+
|
2084
|
+
params = {<span class="sy">:c</span> => <span class="i">3</span>, <span class="sy">:d</span> => <span class="i">4</span>}
|
2085
|
+
ivars params <span class="c"># # assigns @c and @d</span>
|
2086
|
+
<span class="r">end</span>
|
2087
|
+
|
2088
|
+
a_method
|
2089
|
+
<span class="iv">@a</span>.should == <span class="i">1</span>
|
2090
|
+
<span class="iv">@b</span>.should == <span class="i">2</span>
|
2091
|
+
<span class="iv">@c</span>.should == <span class="i">3</span>
|
2092
|
+
<span class="iv">@d</span>.should == <span class="i">4</span>
|
2093
|
+
|
2094
|
+
<span class="r">end</span>
|
2095
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_ivars_source">(<a href="javascript:show('ivars_source')">show</a>)</span>
|
2096
|
+
<pre class="source" id="ivars_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2097
|
+
|
2098
|
+
<span class="r">def</span> <span class="fu">instance_variables_from</span>(obj, *only)
|
2099
|
+
iter =
|
2100
|
+
<span class="r">if</span> obj.is_a? <span class="co">Binding</span>
|
2101
|
+
obj.eval(<span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span>).map{|e| [obj.eval(<span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="dl">"</span></span>), e] }
|
2102
|
+
<span class="r">elsif</span> obj.is_a? <span class="co">Hash</span>
|
2103
|
+
obj.map{|k,v| [v,k] }
|
2104
|
+
<span class="r">else</span>
|
2105
|
+
<span class="c"># elsif obj.is_a? Enumerable</span>
|
2106
|
+
obj.each.with_index
|
2107
|
+
<span class="r">end</span>
|
2108
|
+
|
2109
|
+
ret = []
|
2110
|
+
iter.each{ |value, arg|
|
2111
|
+
arg = arg.to_s
|
2112
|
+
<span class="r">if</span> only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
2113
|
+
arg = <span class="s"><span class="dl">'</span><span class="k">_</span><span class="dl">'</span></span> + arg <span class="r">if</span> (<span class="i">48</span>..<span class="i">57</span>).member? arg.unpack(<span class="s"><span class="dl">'</span><span class="k">C</span><span class="dl">'</span></span>)[<span class="i">0</span>] <span class="c"># 1.8+1.9</span>
|
2114
|
+
ret << ivar = <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>arg<span class="idl">}</span></span><span class="dl">"</span></span>
|
2115
|
+
<span class="pc">self</span>.instance_variable_set ivar, value
|
2116
|
+
<span class="r">end</span>
|
2117
|
+
}
|
2118
|
+
ret
|
2119
|
+
<span class="r">end</span>
|
2120
|
+
<span class="r">alias</span> <span class="fu">ivars</span> <span class="fu">instance_variables_from</span>
|
2121
|
+
|
2122
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2123
|
+
<table class="cube_table"
|
2124
|
+
id="square_brackets_for_cube"
|
2125
|
+
title="require 'zucker/square_brackets_for'"> <tr><th>Summary</th> <td>This helper methods defines <code>[]</code> and <code>[]=</code> for accesing an instance variable.</td></tr><tr><th>Methods/Usage</th> <td><h5>square_brackets_for</h5><pre class="usage source" style="display:block"><span class="r">class</span> <span class="cl">Klass</span>
|
2126
|
+
<span class="r">def</span> <span class="fu">initialize</span>
|
2127
|
+
<span class="iv">@var</span> = {
|
2128
|
+
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
2129
|
+
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
2130
|
+
}
|
2131
|
+
<span class="r">end</span>
|
2132
|
+
|
2133
|
+
square_brackets_for <span class="sy">:var</span> <span class="c"># creates [] and []=</span>
|
2134
|
+
<span class="c"># square_brackets_for :var, false # would create only []</span>
|
2135
|
+
<span class="r">end</span>
|
2136
|
+
|
2137
|
+
a = <span class="co">Klass</span>.new
|
2138
|
+
a[<span class="sy">:a_key</span>] <span class="c"># => 1</span>
|
2139
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_square_brackets_for_spec">(<a href="javascript:show('square_brackets_for_spec')">show</a>)</span>
|
2140
|
+
<pre class="source" id="square_brackets_for_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/square_brackets_for</span><span class="dl">'</span></span>
|
2141
|
+
|
2142
|
+
describe <span class="s"><span class="dl">'</span><span class="k">square_brackets_for</span><span class="dl">'</span></span> <span class="r">do</span>
|
2143
|
+
|
2144
|
+
before <span class="r">do</span>
|
2145
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
2146
|
+
<span class="r">def</span> <span class="fu">initialize</span>
|
2147
|
+
<span class="iv">@var</span> = {
|
2148
|
+
<span class="sy">:a_key</span> => <span class="i">1</span>,
|
2149
|
+
<span class="sy">:another_one</span> => <span class="i">2</span>,
|
2150
|
+
}
|
2151
|
+
<span class="r">end</span>
|
2152
|
+
|
2153
|
+
<span class="iv">@eigenvar</span> = {
|
2154
|
+
<span class="sy">:a_key</span> => <span class="i">99</span>
|
2155
|
+
}
|
2156
|
+
<span class="r">end</span>
|
2157
|
+
<span class="r">end</span>
|
2158
|
+
|
2159
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define a [] getter (not a setter) for an instance var, if the second parameter is false</span><span class="dl">'</span></span> <span class="r">do</span>
|
2160
|
+
|
2161
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
2162
|
+
square_brackets_for <span class="sy">:var</span>, <span class="pc">nil</span>
|
2163
|
+
<span class="r">end</span>
|
2164
|
+
|
2165
|
+
a = <span class="co">Klass</span>.new
|
2166
|
+
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
2167
|
+
|
2168
|
+
proc <span class="r">do</span>
|
2169
|
+
a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">not possible</span><span class="dl">'</span></span>
|
2170
|
+
<span class="r">end</span>.should raise_exception <span class="co">NoMethodError</span>
|
2171
|
+
<span class="r">end</span>
|
2172
|
+
|
2173
|
+
it <span class="s"><span class="dl">'</span><span class="k">should define [] and []= for accessing an instance variable</span><span class="dl">'</span></span> <span class="r">do</span>
|
2174
|
+
|
2175
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
2176
|
+
square_brackets_for <span class="sy">:var</span>
|
2177
|
+
<span class="r">end</span>
|
2178
|
+
|
2179
|
+
a = <span class="co">Klass</span>.new
|
2180
|
+
a[<span class="sy">:a_key</span>].should == <span class="i">1</span>
|
2181
|
+
|
2182
|
+
a[<span class="sy">:this_is</span>] = <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
|
2183
|
+
a[<span class="sy">:this_is</span>].should == <span class="s"><span class="dl">'</span><span class="k">useful</span><span class="dl">'</span></span>
|
2184
|
+
<span class="r">end</span>
|
2185
|
+
|
2186
|
+
it <span class="s"><span class="dl">'</span><span class="k">should also work for class-instance variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
2187
|
+
|
2188
|
+
<span class="r">class</span> <span class="cl">Klass</span>
|
2189
|
+
<span class="r">class</span> << <span class="cl">Klass</span>
|
2190
|
+
square_brackets_for <span class="sy">:eigenvar</span>
|
2191
|
+
<span class="r">end</span>
|
2192
|
+
<span class="r">end</span>
|
2193
|
+
|
2194
|
+
<span class="co">Klass</span>[<span class="sy">:a_key</span>].should == <span class="i">99</span>
|
2195
|
+
<span class="r">end</span>
|
2196
|
+
|
2197
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_square_brackets_for_source">(<a href="javascript:show('square_brackets_for_source')">show</a>)</span>
|
2198
|
+
<pre class="source" id="square_brackets_for_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2199
|
+
|
2200
|
+
<span class="r">class</span> <span class="cl">Module</span>
|
2201
|
+
<span class="r">def</span> <span class="fu">square_brackets_for</span>(ivar, assignment = <span class="pc">true</span>)
|
2202
|
+
<span class="c"># undef [] if respond_to? :[]</span>
|
2203
|
+
<span class="c"># undef []= if respond_to? :[]=</span>
|
2204
|
+
|
2205
|
+
define_method <span class="sy">:[]</span> <span class="r">do</span> |key|
|
2206
|
+
(instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">"</span></span>)[key]
|
2207
|
+
<span class="r">end</span>
|
2208
|
+
|
2209
|
+
<span class="r">if</span> assignment
|
2210
|
+
define_method <span class="sy">:[]=</span> <span class="r">do</span> |key, value|
|
2211
|
+
(instance_variable_get <span class="sy"><span class="sy">:</span><span class="dl">"</span><span class="k">@</span><span class="il"><span class="idl">#{</span>ivar<span class="idl">}</span></span><span class="dl">"</span></span>)[key] = value
|
2212
|
+
<span class="r">end</span>
|
2213
|
+
<span class="r">end</span>
|
2214
|
+
<span class="r">end</span>
|
2215
|
+
<span class="r">end</span>
|
2216
|
+
|
2217
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2218
|
+
</div>
|
2219
|
+
<h2 title="require 'zucker/debug'" id="debug">Cubes[debug]</h2>
|
2220
|
+
<p class="text">(not included by <code>require 'zucker/default'</code>)</p>
|
2221
|
+
<div class="cubes">
|
2222
|
+
<h3 title="require 'zucker/binding'" id="binding">binding</h3>
|
2223
|
+
<table class="cube_table"
|
2224
|
+
id="binding_cube"
|
2225
|
+
title="require 'zucker/binding'"> <tr><th>Summary</th> <td>Adds a <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><pre class="usage source" style="display:block"><span class="c"># shows your current environment's variables, irb example:</span>
|
2226
|
+
>> a=<span class="i">3</span>
|
2227
|
+
=> <span class="i">3</span>
|
2228
|
+
>> binding.vars
|
2229
|
+
=> <span class="c">#<Binding:0x94c4b50></span>
|
2230
|
+
local variables
|
2231
|
+
- <span class="ke">a</span>: <span class="i">3</span>
|
2232
|
+
- <span class="ke">_</span>: <span class="c">#<Binding:0x94c4b50></span>
|
2233
|
+
(instance variables)
|
2234
|
+
- none
|
2235
|
+
<span class="pc">self</span>
|
2236
|
+
- main
|
2237
|
+
block_given?
|
2238
|
+
- <span class="pc">false</span>
|
2239
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_binding_spec">(<a href="javascript:show('binding_spec')">show</a>)</span>
|
2240
|
+
<pre class="source" id="binding_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/binding</span><span class="dl">'</span></span>
|
2241
|
+
|
2242
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Binding#variables</span><span class="dl">'</span></span> <span class="r">do</span>
|
2243
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output information about the current environment</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
2244
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_binding_source">(<a href="javascript:show('binding_source')">show</a>)</span>
|
2245
|
+
<pre class="source" id="binding_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2246
|
+
|
2247
|
+
<span class="r">class</span> <span class="cl">Binding</span>
|
2248
|
+
<span class="r">def</span> <span class="fu">variables</span>
|
2249
|
+
put_vars = lambda { |array|
|
2250
|
+
<span class="r">if</span> array.empty?
|
2251
|
+
<span class="s"><span class="dl">'</span><span class="k"> - none</span><span class="dl">'</span></span>
|
2252
|
+
<span class="r">else</span>
|
2253
|
+
array.map{|e|
|
2254
|
+
val = (<span class="pc">self</span>.eval <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="dl">"</span></span>).inspect
|
2255
|
+
<span class="s"><span class="dl">"</span><span class="k"> - </span><span class="il"><span class="idl">#{</span>e<span class="idl">}</span></span><span class="k">: </span><span class="il"><span class="idl">#{</span> val <span class="idl">}</span></span><span class="dl">"</span></span>
|
2256
|
+
}.join <span class="s"><span class="dl">"</span><span class="ch">\n</span><span class="dl">"</span></span>
|
2257
|
+
<span class="r">end</span>
|
2258
|
+
}
|
2259
|
+
|
2260
|
+
puts <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.to_s<span class="idl">}</span></span><span class="k">
|
2261
|
+
local variables
|
2262
|
+
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">local_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
2263
|
+
(instance variables)
|
2264
|
+
</span><span class="il"><span class="idl">#{</span> put_vars[ <span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">instance_variables</span><span class="dl">'</span></span> ] <span class="idl">}</span></span><span class="k">
|
2265
|
+
self
|
2266
|
+
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">self</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="k">
|
2267
|
+
block_given?
|
2268
|
+
- </span><span class="il"><span class="idl">#{</span><span class="pc">self</span>.eval <span class="s"><span class="dl">'</span><span class="k">block_given?</span><span class="dl">'</span></span><span class="idl">}</span></span><span class="dl">"</span></span>
|
2269
|
+
|
2270
|
+
<span class="r">end</span>
|
2271
|
+
|
2272
|
+
<span class="r">alias</span> <span class="fu">vars</span> <span class="fu">variables</span>
|
2273
|
+
<span class="r">end</span>
|
2274
|
+
|
2275
|
+
<span class="c"># J-_-L</span></pre> </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/binding">github wiki</a></td></tr></table> <h3 title="require 'zucker/cc'" id="cc">cc</h3>
|
2276
|
+
<table class="cube_table"
|
2277
|
+
id="cc_cube"
|
2278
|
+
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><pre class="usage source" style="display:block">c <span class="c"># shows the method callstack</span>
|
2279
|
+
c <span class="pc">true</span> <span class="c"># do not hide irb/ripl callstack</span>
|
2280
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_cc_spec">(<a href="javascript:show('cc_spec')">show</a>)</span>
|
2281
|
+
<pre class="source" id="cc_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/cc</span><span class="dl">'</span></span>
|
2282
|
+
|
2283
|
+
describe <span class="s"><span class="dl">'</span><span class="k">c</span><span class="dl">'</span></span> <span class="r">do</span>
|
2284
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output the method callstack</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
2285
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_cc_source">(<a href="javascript:show('cc_source')">show</a>)</span>
|
2286
|
+
<pre class="source" id="cc_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2287
|
+
|
2288
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
2289
|
+
private
|
2290
|
+
|
2291
|
+
<span class="r">def</span> <span class="fu">c</span>(show_irb = <span class="pc">false</span>)
|
2292
|
+
method_stack = caller.reverse.map{ |m|
|
2293
|
+
m.rindex( <span class="rx"><span class="dl">/</span><span class="k">:</span><span class="ch">\d</span><span class="k">+(:in `(.*)')?$</span><span class="dl">/</span></span> )
|
2294
|
+
<span class="gv">$2</span>
|
2295
|
+
}.compact
|
2296
|
+
|
2297
|
+
<span class="r">if</span> !show_irb
|
2298
|
+
<span class="r">if</span> a = method_stack.index( <span class="s"><span class="dl">'</span><span class="k">irb_binding</span><span class="dl">'</span></span> )
|
2299
|
+
method_stack = [ method_stack[<span class="i">0</span>], <span class="s"><span class="dl">'</span><span class="k">(irb)</span><span class="dl">'</span></span>, *method_stack[a+<span class="i">1</span>..<span class="i">-1</span>] ]
|
2300
|
+
<span class="r">elsif</span> a = method_stack[<span class="i">1</span>..<span class="i">-1</span>].index( <span class="s"><span class="dl">'</span><span class="k"><main></span><span class="dl">'</span></span> )
|
2301
|
+
method_stack = [ method_stack[<span class="i">0</span>], <span class="s"><span class="dl">'</span><span class="k">(ripl)</span><span class="dl">'</span></span>, *method_stack[a+<span class="i">2</span>..<span class="i">-1</span>] ]
|
2302
|
+
<span class="r">end</span>
|
2303
|
+
<span class="r">end</span>
|
2304
|
+
|
2305
|
+
<span class="c"># puts method_stack.map.with_index{ |m, i|</span>
|
2306
|
+
method_stack.each_with_index{ |m, i|
|
2307
|
+
puts <span class="s"><span class="dl">"</span><span class="k"> </span><span class="dl">"</span></span>*i + m
|
2308
|
+
}
|
2309
|
+
<span class="pc">nil</span>
|
2310
|
+
<span class="r">end</span>
|
2311
|
+
|
2312
|
+
<span class="r">alias</span> <span class="fu">cc</span> <span class="fu">c</span>
|
2313
|
+
<span class="r">end</span>
|
2314
|
+
|
2315
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2316
|
+
<table class="cube_table"
|
2317
|
+
id="dd_cube"
|
2318
|
+
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><pre class="usage source" style="display:block">some.d.methods.d.noone.d.knows.d
|
2319
|
+
<span class="c"># ...outputs 4 lines with the inspected objects</span>
|
2320
|
+
<span class="c"># => (result)</span>
|
2321
|
+
|
2322
|
+
<span class="i">21</span>+<span class="co">Math</span>.sin(<span class="i">42</span>).d
|
2323
|
+
<span class="c"># outputs -0.916521547915634</span>
|
2324
|
+
<span class="c"># => 20.0834784520844</span>
|
2325
|
+
|
2326
|
+
name = <span class="s"><span class="dl">'</span><span class="k">Earth</span><span class="dl">'</span></span>
|
2327
|
+
<span class="s"><span class="dl">'</span><span class="k">Hello </span><span class="dl">'</span></span> + name.d{|e| <span class="s"><span class="dl">"</span><span class="k">The length is: </span><span class="il"><span class="idl">#{</span>e.size<span class="idl">}</span></span><span class="dl">"</span></span>}
|
2328
|
+
<span class="c"># outputs "The length is: 5"</span>
|
2329
|
+
<span class="c"># => 'Hello Earth'</span>
|
2330
|
+
</pre></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>
|
2331
|
+
<pre class="source" id="dd_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/dd</span><span class="dl">'</span></span>
|
2332
|
+
|
2333
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#d</span><span class="dl">'</span></span> <span class="r">do</span>
|
2334
|
+
|
2335
|
+
let <span class="sy">:a</span> <span class="r">do</span>
|
2336
|
+
[ <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span> ]
|
2337
|
+
<span class="r">end</span>
|
2338
|
+
|
2339
|
+
it <span class="s"><span class="dl">"</span><span class="k">should not change the object's value</span><span class="dl">"</span></span> <span class="r">do</span>
|
2340
|
+
capture_stdout <span class="r">do</span>
|
2341
|
+
a.each{ |e|
|
2342
|
+
(e.d).should == e
|
2343
|
+
}
|
2344
|
+
|
2345
|
+
a.each{ |e|
|
2346
|
+
(e.d{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}).should == e
|
2347
|
+
}
|
2348
|
+
<span class="r">end</span>
|
2349
|
+
<span class="r">end</span>
|
2350
|
+
|
2351
|
+
it <span class="s"><span class="dl">"</span><span class="k">should puts .inspect if no block is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
2352
|
+
capture_stdout <span class="r">do</span>
|
2353
|
+
a[<span class="i">0</span>].d
|
2354
|
+
a[<span class="i">1</span>].d
|
2355
|
+
a[<span class="i">6</span>].d
|
2356
|
+
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">1</span><span class="ch">\n</span><span class="k">"1"</span><span class="ch">\n</span><span class="k">true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
2357
|
+
<span class="r">end</span>
|
2358
|
+
|
2359
|
+
it <span class="s"><span class="dl">"</span><span class="k">should puts the block if it is given (and not change the object's value)</span><span class="dl">"</span></span> <span class="r">do</span>
|
2360
|
+
capture_stdout <span class="r">do</span>
|
2361
|
+
a[<span class="i">0</span>].d{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
2362
|
+
a[<span class="i">1</span>].d{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
2363
|
+
a[<span class="i">6</span>].d{|value| <span class="s"><span class="dl">"</span><span class="k">This is a: </span><span class="il"><span class="idl">#{</span>value<span class="idl">}</span></span><span class="dl">"</span></span>}
|
2364
|
+
<span class="r">end</span>.should == <span class="s"><span class="dl">%{</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: 1</span><span class="ch">\n</span><span class="k">This is a: true</span><span class="ch">\n</span><span class="dl">}</span></span>
|
2365
|
+
<span class="r">end</span>
|
2366
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_dd_source">(<a href="javascript:show('dd_source')">show</a>)</span>
|
2367
|
+
<pre class="source" id="dd_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2368
|
+
|
2369
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
2370
|
+
<span class="r">def</span> <span class="fu">d</span>(*args, &block)
|
2371
|
+
<span class="r">if</span> args.empty?
|
2372
|
+
tap{
|
2373
|
+
<span class="r">if</span> block_given?
|
2374
|
+
puts <span class="r">yield</span> <span class="pc">self</span>
|
2375
|
+
<span class="r">else</span>
|
2376
|
+
puts <span class="pc">self</span>.inspect
|
2377
|
+
<span class="r">end</span>
|
2378
|
+
}
|
2379
|
+
<span class="r">else</span>
|
2380
|
+
raise <span class="co">ArgumentError</span>, <span class="s"><span class="dl">"</span><span class="k">.d - The parser thought that the code after .d are method arguments... Please don't put a space after d or use .d() or .d{} in this case!</span><span class="dl">"</span></span>
|
2381
|
+
<span class="c"># eval ...</span>
|
2382
|
+
<span class="r">end</span>
|
2383
|
+
<span class="r">end</span>
|
2384
|
+
|
2385
|
+
<span class="r">alias</span> <span class="fu">dd</span> <span class="fu">d</span>
|
2386
|
+
<span class="r">end</span>
|
2387
|
+
|
2388
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2389
|
+
<table class="cube_table"
|
2390
|
+
id="mm_cube"
|
2391
|
+
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><pre class="usage source" style="display:block">>> <span class="i">5</span>.mm <span class="i">2</span> <span class="c"># outputs the list (2 levels deep), for example:</span>
|
2392
|
+
<span class="c"># = Fixnum</span>
|
2393
|
+
<span class="c"># to_s -@ + - * / div % modulo divmod fdiv ** abs magnitude == === <=> > >= < <= ~ & | ^ [] << >> to_f size zero? odd? even? succ</span>
|
2394
|
+
<span class="c"># = Integer</span>
|
2395
|
+
<span class="c"># 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>
|
2396
|
+
</pre></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>
|
2397
|
+
<pre class="source" id="mm_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/mm</span><span class="dl">'</span></span>
|
2398
|
+
|
2399
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Object#mm</span><span class="dl">'</span></span> <span class="r">do</span>
|
2400
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display an ordered method list</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
2401
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_mm_source">(<a href="javascript:show('mm_source')">show</a>)</span>
|
2402
|
+
<pre class="source" id="mm_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2403
|
+
|
2404
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
2405
|
+
<span class="r">def</span> <span class="fu">m</span>(levels = <span class="i">1</span>)
|
2406
|
+
<span class="c"># output helper</span>
|
2407
|
+
mputs = proc{ |e| puts e.map(&<span class="sy">:to_s</span>)*<span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span> }
|
2408
|
+
|
2409
|
+
<span class="c"># how to get results</span>
|
2410
|
+
<span class="r">if</span> <span class="pc">self</span>.is_a? <span class="co">Module</span>
|
2411
|
+
klass, method_function = <span class="pc">self</span>, <span class="sy">:public_methods</span>
|
2412
|
+
<span class="r">else</span>
|
2413
|
+
klass, method_function = <span class="pc">self</span>.class, <span class="sy">:public_instance_methods</span>
|
2414
|
+
<span class="r">end</span>
|
2415
|
+
|
2416
|
+
<span class="c"># eigenclass</span>
|
2417
|
+
eigen = <span class="pc">self</span>.singleton_methods
|
2418
|
+
<span class="r">if</span> !eigen.empty?
|
2419
|
+
puts <span class="s"><span class="dl">'</span><span class="k">= Eigenclass</span><span class="dl">'</span></span> <span class="c"># sorry for not being up to date, I just love the word</span>
|
2420
|
+
mputs[ <span class="pc">self</span>.singleton_methods ]
|
2421
|
+
<span class="r">end</span>
|
2422
|
+
|
2423
|
+
<span class="c"># display</span>
|
2424
|
+
levels.times{ |level|
|
2425
|
+
<span class="r">if</span> cur = klass.ancestors[level]
|
2426
|
+
puts <span class="s"><span class="dl">"</span><span class="k">= </span><span class="il"><span class="idl">#{</span>cur<span class="idl">}</span></span><span class="dl">"</span></span> <span class="c"># put class name</span>
|
2427
|
+
mputs[ cur.send method_function, <span class="pc">false</span> ] <span class="c"># put methods of the class</span>
|
2428
|
+
<span class="r">else</span>
|
2429
|
+
<span class="r">break</span>
|
2430
|
+
<span class="r">end</span>
|
2431
|
+
}
|
2432
|
+
|
2433
|
+
<span class="pc">nil</span>
|
2434
|
+
<span class="r">end</span>
|
2435
|
+
|
2436
|
+
<span class="r">alias</span> <span class="fu">mm</span> <span class="fu">m</span>
|
2437
|
+
<span class="r">alias</span> <span class="fu">method_list</span> <span class="fu">m</span>
|
2438
|
+
<span class="r">end</span>
|
2439
|
+
|
2440
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2441
|
+
<table class="cube_table"
|
2442
|
+
id="oo_cube"
|
2443
|
+
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><pre class="usage source" style="display:block">o <span class="c"># e.g: Reached method `irb_binding', line 1 of file (irb)</span>
|
2444
|
+
o<span class="sy">:Label</span> <span class="c"># e.g: Label: reached method `abc', line 7 of file (irb)</span>
|
2445
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_oo_spec">(<a href="javascript:show('oo_spec')">show</a>)</span>
|
2446
|
+
<pre class="source" id="oo_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/oo</span><span class="dl">'</span></span>
|
2447
|
+
|
2448
|
+
describe <span class="s"><span class="dl">'</span><span class="k">o</span><span class="dl">'</span></span> <span class="r">do</span>
|
2449
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output the current line, file, method and label (if given)</span><span class="dl">'</span></span> <span class="r">do</span>; <span class="r">end</span>
|
2450
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_oo_source">(<a href="javascript:show('oo_source')">show</a>)</span>
|
2451
|
+
<pre class="source" id="oo_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2452
|
+
|
2453
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
2454
|
+
private
|
2455
|
+
|
2456
|
+
<span class="r">def</span> <span class="fu">o</span>(desc = <span class="pc">nil</span>)
|
2457
|
+
caller[<span class="i">0</span>].rindex( <span class="rx"><span class="dl">/</span><span class="k">:(</span><span class="ch">\d</span><span class="k">+)(:in (`.*'))?$</span><span class="dl">/</span></span> )
|
2458
|
+
m = <span class="gv">$3</span> ? <span class="s"><span class="dl">"</span><span class="k">method </span><span class="dl">#</span><span class="gv">$3</span><span class="k">, </span><span class="dl">"</span></span> : <span class="s"><span class="dl">"</span><span class="dl">"</span></span>
|
2459
|
+
d = desc ? <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>desc<span class="idl">}</span></span><span class="k">: r</span><span class="dl">"</span></span> : <span class="s"><span class="dl">'</span><span class="k">R</span><span class="dl">'</span></span>
|
2460
|
+
|
2461
|
+
puts <span class="s"><span class="dl">"</span><span class="il"><span class="idl">#{</span>d<span class="idl">}</span></span><span class="k">eached </span><span class="il"><span class="idl">#{</span>m<span class="idl">}</span></span><span class="k">line </span><span class="dl">#</span><span class="gv">$1</span><span class="k"> of file </span><span class="dl">#</span><span class="gv">$`</span><span class="dl">"</span></span>
|
2462
|
+
<span class="c"># [$`, $1.to_i, $3.to_sym, desc]</span>
|
2463
|
+
<span class="r">end</span>
|
2464
|
+
<span class="r">alias</span> <span class="fu">oo</span> <span class="fu">o</span>
|
2465
|
+
<span class="r">end</span>
|
2466
|
+
|
2467
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2468
|
+
<table class="cube_table"
|
2469
|
+
id="qq_cube"
|
2470
|
+
title="require 'zucker/qq'"> <tr><th>Summary</th> <td>The <code>q</code> method does the same like <code>p</code>, but puts out multiple arguments on the same line.</td></tr><tr><th>Methods/Usage</th> <td><h5>q, qq</h5><pre class="usage source" style="display:block">q <span class="s"><span class="dl">"</span><span class="k">zucker</span><span class="dl">"</span></span>, <span class="s"><span class="dl">'</span><span class="dl">'</span></span>, {<span class="i">6</span>=><span class="i">7</span>}, <span class="i">5</span>, <span class="fl">2.3</span>, <span class="sy">:o</span> <span class="c"># outputs "zucker" "" {6=>7} 5 2.3 :o</span>
|
2471
|
+
</pre></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>
|
2472
|
+
<pre class="source" id="qq_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/qq</span><span class="dl">'</span></span>
|
2473
|
+
|
2474
|
+
describe <span class="s"><span class="dl">'</span><span class="k">q</span><span class="dl">'</span></span> <span class="r">do</span>
|
2475
|
+
|
2476
|
+
it <span class="s"><span class="dl">'</span><span class="k">should output the same as p for a single arg</span><span class="dl">'</span></span> <span class="r">do</span>
|
2477
|
+
capture_stdout{p <span class="rx"><span class="dl">/</span><span class="k">some object</span><span class="dl">/</span></span>}.should ==
|
2478
|
+
capture_stdout{q <span class="rx"><span class="dl">/</span><span class="k">some object</span><span class="dl">/</span></span>}
|
2479
|
+
<span class="r">end</span>
|
2480
|
+
|
2481
|
+
it <span class="s"><span class="dl">"</span><span class="k">should output the same as p but for multiple args one one line, values separated by two spaces</span><span class="dl">"</span></span> <span class="r">do</span>
|
2482
|
+
capture_stdout <span class="r">do</span>
|
2483
|
+
q <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span>
|
2484
|
+
<span class="r">end</span>.chop.should ==
|
2485
|
+
capture_stdout <span class="r">do</span>
|
2486
|
+
p <span class="i">1</span>, <span class="s"><span class="dl">"</span><span class="k">1</span><span class="dl">"</span></span>, <span class="i">2</span>..<span class="i">5</span>, [], {<span class="sy">:hallo</span> => <span class="sy">:du</span>}, <span class="pc">nil</span>, <span class="pc">true</span>
|
2487
|
+
<span class="r">end</span>.chop.gsub( <span class="s"><span class="dl">"</span><span class="ch">\n</span><span class="dl">"</span></span>, <span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span> )
|
2488
|
+
<span class="r">end</span>
|
2489
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_qq_source">(<a href="javascript:show('qq_source')">show</a>)</span>
|
2490
|
+
<pre class="source" id="qq_source">require <span class="s"><span class="dl">'</span><span class="k">zucker</span><span class="dl">'</span></span>
|
2491
|
+
|
2492
|
+
<span class="r">module</span> <span class="cl">Kernel</span>
|
2493
|
+
private
|
2494
|
+
|
2495
|
+
<span class="r">def</span> <span class="fu">q</span>(*args)
|
2496
|
+
puts args.map( &<span class="sy">:inspect</span> )*<span class="s"><span class="dl">'</span><span class="k"> </span><span class="dl">'</span></span> <span class="r">unless</span> args.empty?
|
2497
|
+
<span class="r">end</span>
|
2498
|
+
<span class="r">alias</span> <span class="fu">qq</span> <span class="fu">q</span>
|
2499
|
+
<span class="r">end</span>
|
2500
|
+
|
2501
|
+
<span class="c"># J-_-L</span></pre> </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>
|
2502
|
+
<table class="cube_table"
|
2503
|
+
id="regexp_visualize_cube"
|
2504
|
+
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><pre class="usage source" style="display:block"><span class="c"># regex = /\b([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})\b/i</span>
|
2505
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">I do not contain an email address.</span><span class="dl">'</span></span>
|
2506
|
+
<span class="c"># no match</span>
|
2507
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">I contain an email address: mail@example.com</span><span class="dl">'</span></span>
|
2508
|
+
<span class="c"># I contain an email address: >mail@example.com< </span>
|
2509
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, <span class="i">1</span>
|
2510
|
+
<span class="c"># 1: >mail< @example.com</span>
|
2511
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, <span class="i">2</span>
|
2512
|
+
<span class="c"># 2: mail@ >example.com< </span>
|
2513
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, <span class="i">3</span>
|
2514
|
+
<span class="c"># 3: no match</span>
|
2515
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, [<span class="i">0</span>,<span class="i">1</span>,<span class="i">2</span>]
|
2516
|
+
<span class="c"># 0: >mail@example.com< </span>
|
2517
|
+
<span class="c"># 1: >mail< @example.com</span>
|
2518
|
+
<span class="c"># 2: mail@ >example.com< </span>
|
2519
|
+
</pre></td></tr><tr><th>Specification</th> <td> <span id="show_regexp_visualize_spec">(<a href="javascript:show('regexp_visualize_spec')">show</a>)</span>
|
2520
|
+
<pre class="source" id="regexp_visualize_spec">require <span class="s"><span class="dl">'</span><span class="k">zucker/regexp_visualize</span><span class="dl">'</span></span>
|
2521
|
+
|
2522
|
+
describe <span class="s"><span class="dl">'</span><span class="k">Regexp#visualize</span><span class="dl">'</span></span> <span class="r">do</span>
|
2523
|
+
let <span class="sy">:regex</span> <span class="r">do</span> <span class="rx"><span class="dl">/</span><span class="ch">\b</span><span class="k">([A-Z0-9._%+-]+)@([A-Z0-9.-]+</span><span class="ch">\.</span><span class="k">[A-Z]{2,4})</span><span class="ch">\b</span><span class="dl">/</span><span class="mod">i</span></span> <span class="r">end</span> <span class="c"># regex from regular-expressions.info/email.html</span>
|
2524
|
+
|
2525
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display a string where >and< mark the found regex</span><span class="dl">'</span></span> <span class="r">do</span>
|
2526
|
+
capture_stdout <span class="r">do</span>
|
2527
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">I do not contain an email address.</span><span class="dl">'</span></span>
|
2528
|
+
<span class="r">end</span>.chomp.should == <span class="s"><span class="dl">'</span><span class="k">no match</span><span class="dl">'</span></span>
|
2529
|
+
<span class="r">end</span>
|
2530
|
+
|
2531
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display "no match" if the regex could not be matched</span><span class="dl">'</span></span> <span class="r">do</span>
|
2532
|
+
capture_stdout <span class="r">do</span>
|
2533
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">I contain an email address: mail@example.com</span><span class="dl">'</span></span>
|
2534
|
+
<span class="r">end</span>.chomp.should == <span class="s"><span class="dl">'</span><span class="k">I contain an email address: >mail@example.com< </span><span class="dl">'</span></span>
|
2535
|
+
<span class="r">end</span>
|
2536
|
+
|
2537
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display a string where >and< mark the found regex group if group identifier is given as arg2</span><span class="dl">'</span></span> <span class="r">do</span>
|
2538
|
+
capture_stdout <span class="r">do</span>
|
2539
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, <span class="i">1</span>
|
2540
|
+
<span class="r">end</span>.chomp.should == <span class="s"><span class="dl">'</span><span class="k">1: >mail< @example.com</span><span class="dl">'</span></span>
|
2541
|
+
<span class="r">end</span>
|
2542
|
+
|
2543
|
+
it <span class="s"><span class="dl">'</span><span class="k">should display "group identifier: no match" the arg2 group could not be matched</span><span class="dl">'</span></span> <span class="r">do</span>
|
2544
|
+
capture_stdout <span class="r">do</span>
|
2545
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, <span class="i">3</span>
|
2546
|
+
<span class="r">end</span>.chomp.should == <span class="s"><span class="dl">'</span><span class="k">3: no match</span><span class="dl">'</span></span>
|
2547
|
+
<span class="r">end</span>
|
2548
|
+
|
2549
|
+
it <span class="s"><span class="dl">'</span><span class="k">should also take an array of group identfiers as arg2</span><span class="dl">'</span></span> <span class="r">do</span>
|
2550
|
+
capture_stdout <span class="r">do</span>
|
2551
|
+
regex.vis <span class="s"><span class="dl">'</span><span class="k">mail@example.com</span><span class="dl">'</span></span>, [<span class="i">0</span>,<span class="i">1</span>,<span class="i">2</span>]
|
2552
|
+
<span class="r">end</span>.chomp.should == <span class="s"><span class="dl">"</span><span class="k">0: >mail@example.com< </span><span class="ch">\n</span><span class="dl">"</span></span>\
|
2553
|
+
<span class="s"><span class="dl">"</span><span class="k">1: >mail< @example.com</span><span class="ch">\n</span><span class="dl">"</span></span>\
|
2554
|
+
<span class="s"><span class="dl">"</span><span class="k">2: mail@ >example.com< </span><span class="dl">"</span></span>
|
2555
|
+
<span class="r">end</span>
|
2556
|
+
<span class="r">end</span></pre> </td></tr><tr><th>Source</th> <td> <span id="show_regexp_visualize_source">(<a href="javascript:show('regexp_visualize_source')">show</a>)</span>
|
2557
|
+
<pre class="source" id="regexp_visualize_source"><span class="r">class</span> <span class="cl">Regexp</span>
|
2558
|
+
<span class="r">def</span> <span class="fu">visualize</span>(string, groups = <span class="pc">nil</span>)
|
2559
|
+
<span class="r">if</span> <span class="pc">self</span> =~ string
|
2560
|
+
<span class="r">if</span> !groups
|
2561
|
+
puts <span class="gv">$`</span> + <span class="s"><span class="dl">'</span><span class="k"> ></span><span class="dl">'</span></span> + <span class="gv">$&</span> + <span class="s"><span class="dl">'</span><span class="k">< </span><span class="dl">'</span></span> + <span class="gv">$'</span>
|
2562
|
+
<span class="r">else</span>
|
2563
|
+
Array( groups ).each{ |group|
|
2564
|
+
<span class="r">begin</span>
|
2565
|
+
gr_string = string.dup
|
2566
|
+
gr_string[ <span class="gv">$~</span>.end( group ), <span class="i">0</span> ] = <span class="s"><span class="dl">'</span><span class="k">< </span><span class="dl">'</span></span>
|
2567
|
+
gr_string[ <span class="gv">$~</span>.begin( group ), <span class="i">0</span> ] = <span class="s"><span class="dl">'</span><span class="k"> ></span><span class="dl">'</span></span>
|
2568
|
+
puts group.to_s + <span class="s"><span class="dl">'</span><span class="k">: </span><span class="dl">'</span></span> + gr_string
|
2569
|
+
<span class="r">rescue</span> <span class="co">IndexError</span>
|
2570
|
+
puts group.to_s + <span class="s"><span class="dl">'</span><span class="k">: no match</span><span class="dl">'</span></span>
|
2571
|
+
<span class="r">end</span>
|
2572
|
+
}
|
2573
|
+
<span class="r">end</span>
|
2574
|
+
<span class="r">else</span>
|
2575
|
+
puts <span class="s"><span class="dl">'</span><span class="k">no match</span><span class="dl">'</span></span>
|
2576
|
+
<span class="r">end</span>
|
2577
|
+
|
2578
|
+
<span class="pc">nil</span>
|
2579
|
+
<span class="r">end</span>
|
2580
|
+
|
2581
|
+
<span class="r">alias</span> <span class="fu">vis</span> <span class="fu">visualize</span>
|
2582
|
+
<span class="r">end</span></pre> </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>
|
2583
|
+
</div>
|
2584
|
+
<h2>Extras</h2>
|
2585
|
+
<p class="text">
|
2586
|
+
You can activate some additional aliases for Zucker functionality by executing:
|
2587
|
+
<code class="scode">Zucker.more_aliases! # => [:copy, :n, :init, :returning, :lib?, :RV, :RE]</code>
|
2588
|
+
</p>
|
2589
|
+
|
2590
|
+
<h2>Changelog</h2>
|
2591
|
+
<div class="cubes">
|
2592
|
+
<pre class="scode">2011-01-22 | <a href="http://rubyzucker.info/9/">Zucker 9</a>
|
2593
|
+
* removed history versions (sorry, it caused too much gem/rdoc troubles)
|
2594
|
+
* add file cube: File.delete! and File.gsub
|
2595
|
+
* debug pack improvements
|
2596
|
+
* binding: typos + return nil
|
2597
|
+
* cc: support for ripl + return nil
|
2598
|
+
* mm: also show eigenclass for modules + nicer displaying + return nil
|
2599
|
+
* added Regexp#visualize
|
2600
|
+
* removed optional sandbox param
|
2601
|
+
* renamed xxx2proc to xxx_to_proc
|
2602
|
+
* changed rakefile/rspec/gemspec structure
|
2603
|
+
* more minor fixes and improvements
|
2604
|
+
|
2605
|
+
|
2606
|
+
2010-10-06 | <a href="http://rubyzucker.info/8/">Zucker 8</a>
|
2607
|
+
* fixed a little alias_for bug
|
2608
|
+
* disabled rdoc creation when installing (in favour of the custom docs)
|
2609
|
+
* changed Binding#inspect to Binding.variables (was too verbose and dangerous)
|
2610
|
+
|
2611
|
+
|
2612
|
+
2010-10-03 | <a href="http://rubyzucker.info/7/">Zucker 7</a>
|
2613
|
+
* fixed critical OS.windows? bug
|
2614
|
+
|
2615
|
+
|
2616
|
+
2010-10-03 | <a href="http://rubyzucker.info/6/">Zucker 6</a>
|
2617
|
+
* no new cubes
|
2618
|
+
* bugfix for OS.posix?
|
2619
|
+
* small changes + bugfixes + doc improvements
|
2620
|
+
* added two user methods to Info
|
2621
|
+
* changed egonil semantics ( using method_missing, see http://rbjl.net/26/catch_nil.rb )
|
2622
|
+
* bugfix for vv
|
2623
|
+
|
2624
|
+
|
2625
|
+
2010-09-04 | <a href="http://rubyzucker.info/5/">Zucker 5</a>
|
2626
|
+
* debug edition - added two debug helpers: oo (output line, method, file) and cc (output method callstack)
|
2627
|
+
* renamed cube D to dd add added more debug aliases (for mm and binding)
|
2628
|
+
* fixed __SPECIAL_VARS__ in info and kernel cube and some minor bugfixes
|
2629
|
+
* Zucker.activate_more_aliases! option
|
2630
|
+
* added Hash#&
|
2631
|
+
* added aliases: File.filename (for basename), Dir.join and Dir.split (for File.join, split)
|
2632
|
+
* added a flexible requiring mechansim in zucker.rb (no api changes)
|
2633
|
+
* restructered packages
|
2634
|
+
* added rake tasks for releasing
|
2635
|
+
* improved RubyVersion constant (cleaner and more flexible)
|
2636
|
+
|
2637
|
+
|
2638
|
+
2010-09-01 | <a href="http://rubyzucker.info/4/">Zucker 4</a>
|
2639
|
+
* fixed Binding#inspect
|
2640
|
+
* added RubyEngine constant
|
2641
|
+
* added RubyVersion constant
|
2642
|
+
* added OS constant
|
2643
|
+
* added q debug method (like p but on one line)
|
2644
|
+
* added String#-
|
2645
|
+
|
2646
|
+
|
2647
|
+
2010-08-14 | <a href="http://rubyzucker.info/3/">Zucker 3</a>
|
2648
|
+
* added tap cube
|
2649
|
+
* added Object#not
|
2650
|
+
* added alias_for for an alternative alias syntax
|
2651
|
+
* added String#constantize (improved AS version)
|
2652
|
+
* improved Info module
|
2653
|
+
* made Array#sum Rails compatibile
|
2654
|
+
* improved docs
|
2655
|
+
* changed directory layout (no changes for requiring)
|
2656
|
+
* more small changes
|
2657
|
+
|
2658
|
+
|
2659
|
+
2010-08-08 | <a href="http://rubyzucker.info/2/">Zucker 2</a>
|
2660
|
+
* added info cube
|
2661
|
+
* added chaining for array2proc
|
2662
|
+
* fixed Hash.zip
|
2663
|
+
* fixed instance_variables_from binding for 1.9
|
2664
|
+
* more specs
|
2665
|
+
|
2666
|
+
|
2667
|
+
2010-08-06 | <a href="http://rubyzucker.info/1/">Zucker 1</a>
|
2668
|
+
* initial release
|
2669
|
+
|
2670
|
+
|
2671
|
+
2010-08-05 | <a href="http://rubyzucker.info/rubyzucker.pdf">Zucker 0</a>
|
2672
|
+
* pre-release for rug-b talk
|
2673
|
+
|
2674
|
+
</pre>
|
2675
|
+
</div>
|
2676
|
+
<br/>
|
2677
|
+
</div>
|
2678
|
+
<div id="foot">
|
2679
|
+
<div id="smile"><a href="http://rbjl.net">J-_-L</a></div>
|
2680
|
+
This is the Ruby Zucker 9 documentation (2011-01-22).
|
2681
|
+
The current version is always available at <a href="http://rubyzucker.info">rubyzucker.info</a>.
|
2682
|
+
Source at <a href="http://github.com/janlelis/zucker">github</a>.
|
2683
|
+
Ruby Logo CC-BY-SA Yukihiro Matsumoto.
|
2684
|
+
</div>
|
2685
|
+
|
2686
|
+
</body>
|
2687
|
+
</html>
|
2688
|
+
|