facets 2.1.3 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +14 -12
- data/CHANGES +45 -2
- data/NOTES +9 -7
- data/lib/core/facets/1stclassmethod.rb +15 -11
- data/lib/core/facets/array.rb +0 -1
- data/lib/core/facets/array/conjoin.rb +40 -8
- data/lib/core/facets/array/delete.rb +8 -2
- data/lib/core/facets/array/indexable.rb +30 -3
- data/lib/core/facets/array/merge.rb +11 -0
- data/lib/core/facets/array/only.rb +3 -3
- data/lib/core/facets/array/pad.rb +4 -46
- data/lib/core/facets/array/rotate.rb +4 -4
- data/lib/core/facets/array/select.rb +2 -0
- data/lib/core/facets/array/splice.rb +16 -2
- data/lib/core/facets/array/stackable.rb +6 -40
- data/lib/core/facets/binding.rb +3 -0
- data/lib/core/facets/binding/cflow.rb +10 -64
- data/lib/core/facets/binding/defined.rb +10 -0
- data/lib/core/facets/binding/eval.rb +10 -76
- data/lib/core/facets/binding/here.rb +9 -0
- data/lib/core/facets/binding/self.rb +10 -0
- data/lib/core/facets/binding/vars.rb +0 -45
- data/lib/core/facets/boolean.rb +31 -103
- data/lib/core/facets/class/descendents.rb +17 -44
- data/lib/core/facets/comparable/bound.rb +8 -70
- data/lib/core/facets/comparable/cmp.rb +12 -92
- data/lib/core/facets/conversion.rb +122 -321
- data/lib/core/facets/dir/multiglob.rb +0 -13
- data/lib/core/facets/dir/traverse.rb +61 -111
- data/lib/core/facets/enumerable.rb +4 -2
- data/lib/core/facets/enumerable/cartesian.rb +36 -85
- data/lib/core/facets/enumerable/collect.rb +64 -152
- data/lib/core/facets/enumerable/combination.rb +40 -42
- data/lib/core/facets/enumerable/count.rb +99 -174
- data/lib/core/facets/enumerable/each.rb +86 -0
- data/lib/core/facets/enumerable/mash.rb +58 -0
- data/lib/core/facets/enumerable/permutation.rb +39 -44
- data/lib/core/facets/enumerable/probability.rb +16 -90
- data/lib/core/facets/enumerable/split.rb +113 -0
- data/lib/core/facets/exception/detail.rb +1 -2
- data/lib/core/facets/facets.rb +3 -2
- data/lib/core/facets/file/read.rb +4 -59
- data/lib/core/facets/file/topath.rb +8 -96
- data/lib/core/facets/file/write.rb +21 -49
- data/lib/core/facets/filetest/root.rb +4 -3
- data/lib/core/facets/functor.rb +5 -43
- data/lib/core/facets/hash/at.rb +7 -37
- data/lib/core/facets/hash/delete.rb +9 -58
- data/lib/core/facets/hash/has_keys.rb +4 -43
- data/lib/core/facets/hash/insert.rb +5 -38
- data/lib/core/facets/hash/inverse.rb +2 -41
- data/lib/core/facets/hash/iterate.rb +4 -45
- data/lib/core/facets/hash/keyize.rb +24 -92
- data/lib/core/facets/hash/merge.rb +4 -38
- data/lib/core/facets/hash/new.rb +5 -38
- data/lib/core/facets/hash/op.rb +20 -66
- data/lib/core/facets/hash/rekey.rb +9 -87
- data/lib/core/facets/hash/select.rb +1 -1
- data/lib/core/facets/hash/slice.rb +42 -0
- data/lib/core/facets/hash/traverse.rb +6 -46
- data/lib/core/facets/hash/update.rb +27 -67
- data/lib/core/facets/hash/weave.rb +22 -42
- data/lib/core/facets/indexable.rb +42 -14
- data/lib/core/facets/integer/bitmask.rb +69 -106
- data/lib/core/facets/integer/factorial.rb +1 -44
- data/lib/core/facets/integer/multiples.rb +27 -22
- data/lib/core/facets/integer/of.rb +1 -33
- data/lib/core/facets/kernel.rb +4 -2
- data/lib/core/facets/kernel/ask.rb +15 -3
- data/lib/core/facets/kernel/callstack.rb +38 -69
- data/lib/core/facets/kernel/constant.rb +2 -38
- data/lib/core/facets/kernel/deepcopy.rb +3 -55
- data/lib/core/facets/kernel/dir.rb +2 -0
- data/lib/core/facets/kernel/ergo.rb +2 -2
- data/lib/core/facets/kernel/instance.rb +51 -120
- data/lib/core/facets/kernel/metaid.rb +35 -73
- data/lib/core/facets/kernel/object.rb +14 -39
- data/lib/core/facets/kernel/op_esc.rb +24 -2
- data/lib/core/facets/kernel/populate.rb +6 -69
- data/lib/core/facets/kernel/report.rb +28 -33
- data/lib/core/facets/kernel/require.rb +7 -21
- data/lib/core/facets/kernel/respond.rb +11 -1
- data/lib/core/facets/kernel/returning.rb +50 -0
- data/lib/core/facets/kernel/silence.rb +12 -24
- data/lib/core/facets/kernel/super.rb +12 -76
- data/lib/core/facets/kernel/tap.rb +25 -107
- data/lib/core/facets/kernel/val.rb +7 -46
- data/lib/core/facets/kernel/withattr.rb +6 -64
- data/lib/core/facets/matchdata/matchset.rb +8 -50
- data/lib/core/facets/module.rb +3 -1
- data/lib/core/facets/module/abstract.rb +9 -47
- data/lib/core/facets/module/alias.rb +11 -68
- data/lib/core/facets/module/attr.rb +36 -15
- data/lib/core/facets/module/cattr.rb +8 -76
- data/lib/core/facets/module/clone.rb +15 -66
- data/lib/core/facets/module/include.rb +54 -114
- data/lib/core/facets/module/methods.rb +15 -13
- data/lib/core/facets/module/modify.rb +20 -206
- data/lib/core/facets/module/name.rb +19 -83
- data/lib/core/facets/module/require.rb +48 -53
- data/lib/core/facets/module/traits.rb +74 -33
- data/lib/core/facets/nilclass/status.rb +25 -2
- data/lib/core/facets/numeric/round.rb +24 -89
- data/lib/core/facets/proc/bind.rb +16 -59
- data/lib/core/facets/proc/compose.rb +6 -40
- data/lib/core/facets/proc/fn.rb +2 -0
- data/lib/core/facets/range/combine.rb +15 -51
- data/lib/core/facets/range/overlap.rb +8 -64
- data/lib/core/facets/regexp/arity.rb +6 -41
- data/lib/core/facets/stackable.rb +43 -4
- data/lib/core/facets/string.rb +2 -0
- data/lib/core/facets/string/align.rb +45 -55
- data/lib/core/facets/string/blank.rb +2 -46
- data/lib/core/facets/string/bracket.rb +14 -87
- data/lib/core/facets/string/case.rb +45 -32
- data/lib/core/facets/string/crypt.rb +3 -40
- data/lib/core/facets/string/filter.rb +7 -49
- data/lib/core/facets/string/format.rb +24 -126
- data/lib/core/facets/string/indexable.rb +50 -133
- data/lib/core/facets/string/interpolate.rb +24 -59
- data/lib/core/facets/string/natcmp.rb +5 -39
- data/lib/core/facets/string/nchar.rb +37 -45
- data/lib/core/facets/string/op.rb +13 -0
- data/lib/core/facets/string/partitions.rb +38 -99
- data/lib/core/facets/string/range.rb +8 -49
- data/lib/core/facets/string/regesc.rb +9 -42
- data/lib/core/facets/string/scan.rb +6 -52
- data/lib/core/facets/string/splice.rb +8 -13
- data/lib/core/facets/string/tabs.rb +16 -153
- data/lib/core/facets/symbol.rb +3 -1
- data/lib/core/facets/symbol/chomp.rb +11 -29
- data/lib/core/facets/symbol/generate.rb +4 -43
- data/lib/core/facets/symbol/not.rb +13 -25
- data/lib/core/facets/symbol/shadow.rb +2 -40
- data/lib/core/facets/symbol/succ.rb +14 -13
- data/lib/core/facets/symbol/to_proc.rb +33 -28
- data/lib/core/facets/time.rb +1 -0
- data/lib/core/facets/time/change.rb +5 -38
- data/lib/core/facets/time/elapse.rb +2 -41
- data/lib/core/facets/time/stamp.rb +47 -0
- data/lib/core/facets/unboundmethod.rb +3 -0
- data/lib/core/facets/unboundmethod/arguments.rb +27 -4
- data/lib/core/facets/unboundmethod/name.rb +21 -7
- data/lib/methods/facets/enumerable/cluster_by.rb +1 -1
- data/lib/methods/facets/enumerable/collate.rb +1 -0
- data/lib/methods/facets/enumerable/divide.rb +1 -1
- data/lib/methods/facets/enumerable/each_by.rb +1 -1
- data/lib/methods/facets/enumerable/each_pair.rb +1 -1
- data/lib/methods/facets/enumerable/eachn.rb +1 -1
- data/lib/methods/facets/enumerable/graph.rb +1 -1
- data/lib/methods/facets/enumerable/group_by.rb +1 -1
- data/lib/methods/facets/enumerable/inject.rb +1 -0
- data/lib/methods/facets/enumerable/map_send.rb +1 -0
- data/lib/methods/facets/enumerable/modulate.rb +1 -0
- data/lib/methods/facets/enumerable/partition_by.rb +1 -1
- data/lib/methods/facets/hash/collate.rb +1 -1
- data/lib/methods/facets/hash/except.rb +1 -0
- data/lib/methods/facets/hash/graph.rb +1 -1
- data/lib/methods/facets/hash/mash.rb +1 -0
- data/lib/methods/facets/kernel/Bit.rb +1 -0
- data/lib/methods/facets/kernel/complete.rb +1 -1
- data/lib/methods/facets/kernel/here.rb +1 -1
- data/lib/methods/facets/kernel/non_nil.rb +1 -0
- data/lib/methods/facets/kernel/respond_with_value.rb +1 -0
- data/lib/methods/facets/kernel/with.rb +1 -1
- data/lib/methods/facets/module/class_def.rb +1 -0
- data/lib/methods/facets/string/camelcase.rb +1 -0
- data/lib/methods/facets/string/op_minus.rb +1 -0
- data/lib/methods/facets/string/snakecase.rb +1 -0
- data/lib/more/facets/attributes.rb +5 -5
- data/lib/more/facets/basicobject.rb +0 -62
- data/lib/{core/facets/continuation/create.rb → more/facets/continuation.rb} +4 -21
- data/lib/more/facets/duration.rb +534 -0
- data/lib/more/facets/lazy.rb +3 -3
- data/lib/more/facets/namespace.rb +23 -71
- data/lib/more/facets/rbsystem.rb +42 -8
- data/lib/more/facets/stylize.rb +6 -3
- data/lib/more/facets/thread.rb +55 -0
- data/meta/MANIFEST +49 -18
- data/meta/ROLLRC +1 -1
- data/meta/project.yaml +2 -2
- data/task/install +1 -1
- data/task/test/general +8 -2
- data/test/unit/array/test_pad.rb +30 -45
- data/test/unit/array/test_stackable.rb +14 -28
- data/test/unit/binding/test_cflow.rb +34 -49
- data/test/unit/binding/test_defined.rb +17 -0
- data/test/unit/binding/test_eval.rb +11 -39
- data/test/unit/binding/test_here.rb +17 -0
- data/test/unit/binding/test_self.rb +17 -0
- data/test/unit/binding/test_vars.rb +22 -37
- data/test/unit/class/test_descendents.rb +15 -30
- data/test/unit/class/test_initializer.rb +1 -8
- data/test/unit/comparable/test_bound.rb +38 -53
- data/test/unit/comparable/test_cmp.rb +45 -60
- data/test/unit/dir/test_traverse.rb +56 -0
- data/test/unit/enumerable/test_cartesian.rb +34 -47
- data/test/unit/enumerable/test_collect.rb +16 -125
- data/test/unit/enumerable/test_combination.rb +1 -8
- data/test/unit/enumerable/test_count.rb +51 -66
- data/test/unit/enumerable/test_each.rb +77 -0
- data/test/unit/enumerable/test_mash.rb +51 -0
- data/test/unit/enumerable/test_permutation.rb +1 -7
- data/test/unit/enumerable/test_probability.rb +42 -55
- data/test/unit/enumerable/test_split.rb +52 -0
- data/test/unit/exception/test_detail.rb +19 -0
- data/test/unit/file/test_read.rb +34 -0
- data/test/unit/file/test_topath.rb +17 -25
- data/test/unit/file/test_write.rb +6 -12
- data/test/unit/filetest/test_root.rb +14 -0
- data/test/unit/hash/test_at.rb +9 -21
- data/test/unit/hash/test_delete.rb +22 -37
- data/test/unit/hash/test_has_keys.rb +13 -28
- data/test/unit/hash/test_insert.rb +9 -23
- data/test/unit/hash/test_inverse.rb +11 -26
- data/test/unit/hash/test_iterate.rb +16 -31
- data/test/unit/hash/test_keyize.rb +40 -55
- data/test/unit/hash/test_merge.rb +10 -25
- data/test/unit/hash/test_new.rb +9 -24
- data/test/unit/hash/test_op.rb +1 -7
- data/test/unit/hash/test_rekey.rb +47 -62
- data/test/unit/hash/test_traverse.rb +17 -32
- data/test/unit/hash/test_update.rb +30 -45
- data/test/unit/hash/test_weave.rb +10 -23
- data/test/unit/integer/test_bitmask.rb +48 -44
- data/test/unit/integer/test_factorial.rb +11 -26
- data/test/unit/integer/test_multiples.rb +1 -5
- data/test/unit/integer/test_of.rb +14 -29
- data/test/unit/kernel/test_callstack.rb +13 -28
- data/test/unit/kernel/test_constant.rb +14 -28
- data/test/unit/kernel/test_deepcopy.rb +20 -35
- data/test/unit/kernel/test_instance.rb +46 -61
- data/test/unit/kernel/test_metaid.rb +42 -57
- data/test/unit/kernel/test_object.rb +14 -29
- data/test/unit/kernel/test_populate.rb +38 -53
- data/test/unit/kernel/test_report.rb +1 -7
- data/test/unit/kernel/test_returning.rb +16 -0
- data/test/unit/kernel/test_silence.rb +8 -23
- data/test/unit/kernel/test_super.rb +41 -56
- data/test/unit/kernel/test_tap.rb +15 -39
- data/test/unit/kernel/test_val.rb +7 -20
- data/test/unit/kernel/test_withattr.rb +29 -44
- data/test/unit/matchdata/test_matchset.rb +20 -35
- data/test/unit/module/test_abstract.rb +15 -30
- data/test/unit/module/test_alias.rb +32 -45
- data/test/unit/module/test_cattr.rb +39 -54
- data/test/unit/module/test_clone.rb +31 -43
- data/test/unit/module/test_include.rb +1 -35
- data/test/unit/module/test_modify.rb +102 -114
- data/test/unit/module/test_name.rb +1 -7
- data/test/unit/numeric/test_round.rb +46 -61
- data/test/unit/proc/test_bind.rb +29 -44
- data/test/unit/proc/test_compose.rb +20 -35
- data/test/unit/range/test_combine.rb +14 -29
- data/test/unit/range/test_overlap.rb +34 -51
- data/test/unit/regexp/test_arity.rb +14 -29
- data/test/unit/string/test_align.rb +13 -27
- data/test/unit/string/test_blank.rb +12 -27
- data/test/unit/string/test_bracket.rb +52 -67
- data/test/unit/string/test_case.rb +19 -38
- data/test/unit/string/test_crypt.rb +10 -25
- data/test/unit/string/test_filter.rb +1 -7
- data/test/unit/string/test_format.rb +2 -7
- data/test/unit/string/test_indexable.rb +54 -69
- data/test/unit/string/test_interpolate.rb +8 -21
- data/test/unit/string/test_natcmp.rb +11 -26
- data/test/unit/string/test_nchar.rb +1 -14
- data/test/unit/string/test_op.rb +14 -0
- data/test/unit/string/test_partitions.rb +39 -54
- data/test/unit/string/test_range.rb +17 -32
- data/test/unit/string/test_regesc.rb +1 -7
- data/test/unit/string/test_scan.rb +23 -38
- data/test/unit/string/test_stackable.rb +79 -98
- data/test/unit/string/test_tabs.rb +1 -7
- data/test/unit/symbol/test_chomp.rb +10 -21
- data/test/unit/symbol/test_generate.rb +7 -22
- data/test/unit/symbol/test_not.rb +10 -25
- data/test/unit/symbol/test_shadow.rb +8 -23
- data/test/unit/symbol/test_succ.rb +1 -7
- data/test/unit/symbol/test_to_proc.rb +1 -7
- data/test/unit/test_attributes.rb +1 -1
- data/test/unit/test_continuation.rb +13 -0
- data/test/unit/test_conversion.rb +1 -14
- data/test/unit/test_namespace.rb +30 -3
- data/test/unit/test_thread.rb +23 -0
- data/test/unit/time/test_change.rb +11 -26
- data/test/unit/time/test_elapse.rb +10 -25
- data/test/unit/time/test_stamp.rb +28 -0
- metadata +65 -25
- data/lib/core/facets/array/unzip.rb +0 -14
- data/lib/core/facets/continuation.rb +0 -1
- data/lib/core/facets/enumerable/collate.rb +0 -104
- data/lib/core/facets/kernel/require_esc.rb +0 -44
- data/lib/more/facets/mapsend.rb +0 -98
- data/lib/more/facets/pp_s.rb +0 -30
- data/log/history.rd +0 -38
- data/log/todo.rd +0 -4
- data/task/special/quickopts +0 -15
- data/test/unit/continuation/test_create.rb +0 -28
- data/test/unit/enumerable/test_collate.rb +0 -51
- data/test/unit/kernel/test_require_esc.rb +0 -29
- data/test/unit/test_mapsend.rb +0 -18
- data/test/unit/test_pp_s.rb +0 -17
@@ -1,18 +1,15 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Class Descendent Extensions
|
4
|
-
#
|
5
|
-
# SUMMARY:
|
6
|
-
#
|
7
|
-
# Extension methods for Class class for accessing
|
8
|
-
# and even removing subclasses.
|
9
|
-
#
|
10
|
-
# CREDITS:
|
11
|
-
#
|
12
|
-
# - ?
|
13
|
-
|
14
1
|
class Class
|
15
2
|
|
3
|
+
# List all descedents of this class.
|
4
|
+
#
|
5
|
+
# class X ; end
|
6
|
+
# class A < X; end
|
7
|
+
# class B < X; end
|
8
|
+
# X.descendents #=> [A,B]
|
9
|
+
#
|
10
|
+
# NOTE: This is a intesive operation. Do not
|
11
|
+
# expect it to be super fast.
|
12
|
+
|
16
13
|
def descendents
|
17
14
|
subclass = []
|
18
15
|
ObjectSpace.each_object( Class ) do |c|
|
@@ -23,8 +20,13 @@ class Class
|
|
23
20
|
return subclass
|
24
21
|
end
|
25
22
|
|
23
|
+
# Obvious alias for descendents.
|
24
|
+
|
26
25
|
alias_method :subclasses, :descendents
|
27
26
|
|
27
|
+
# Remove descendents. This simple deletes the constant
|
28
|
+
# associated to the descendents name.
|
29
|
+
|
28
30
|
def remove_descendents
|
29
31
|
self.descendents.each do |subclass|
|
30
32
|
Object.send(:remove_const, subclass.name) rescue nil
|
@@ -32,37 +34,8 @@ class Class
|
|
32
34
|
ObjectSpace.garbage_collect
|
33
35
|
end
|
34
36
|
|
37
|
+
# Obvious alias for remove_descendents.
|
38
|
+
|
35
39
|
alias_method :remove_subclasses, :remove_descendents
|
36
40
|
|
37
41
|
end
|
38
|
-
|
39
|
-
|
40
|
-
# _____ _
|
41
|
-
# |_ _|__ ___| |_
|
42
|
-
# | |/ _ \/ __| __|
|
43
|
-
# | | __/\__ \ |_
|
44
|
-
# |_|\___||___/\__|
|
45
|
-
#
|
46
|
-
=begin test
|
47
|
-
|
48
|
-
require 'test/unit'
|
49
|
-
|
50
|
-
class TestClassDescendents < Test::Unit::TestCase
|
51
|
-
|
52
|
-
class A ; end
|
53
|
-
class B < A ; end
|
54
|
-
class C < B ; end
|
55
|
-
|
56
|
-
def test_descendents
|
57
|
-
assert_equal( [C,B], A.descendents )
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_remove_descendents
|
61
|
-
assert_nothing_raised { B.remove_descendents }
|
62
|
-
# doesn't work despite above; not sure why
|
63
|
-
#assert_equal( [B], A.descendents )
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
=end
|
@@ -1,22 +1,3 @@
|
|
1
|
-
# +FILE+
|
2
|
-
#
|
3
|
-
# bound.rb
|
4
|
-
#
|
5
|
-
# +DESCRIPTION+
|
6
|
-
#
|
7
|
-
# Limit comparisons.
|
8
|
-
#
|
9
|
-
# +AUTHORS+
|
10
|
-
#
|
11
|
-
# CREDIT Florian Gross
|
12
|
-
#
|
13
|
-
# +NOTES+
|
14
|
-
#
|
15
|
-
# NOTE Are the module methods better as parameterized mixins?
|
16
|
-
#
|
17
|
-
# TODO Maybe improve the naming or the module methods.
|
18
|
-
|
19
|
-
#
|
20
1
|
module Comparable
|
21
2
|
|
22
3
|
# Returns the lower of self or x.
|
@@ -24,6 +5,8 @@ module Comparable
|
|
24
5
|
# 4.at_least(5) #=> 5
|
25
6
|
# 6.at_least(5) #=> 6
|
26
7
|
#
|
8
|
+
# CREDIT Florian Gross
|
9
|
+
|
27
10
|
def at_least(x)
|
28
11
|
(self >= x) ? self : x
|
29
12
|
end
|
@@ -33,6 +16,8 @@ module Comparable
|
|
33
16
|
# 4.at_most(5) #=> 4
|
34
17
|
# 6.at_most(5) #=> 5
|
35
18
|
#
|
19
|
+
# CREDIT Florian Gross
|
20
|
+
|
36
21
|
def at_most(x)
|
37
22
|
(self <= x) ? self : x
|
38
23
|
end
|
@@ -48,6 +33,8 @@ module Comparable
|
|
48
33
|
# 9.clip(2,7) #=> 7
|
49
34
|
# 1.clip(2,7) #=> 2
|
50
35
|
#
|
36
|
+
# CREDIT Trans
|
37
|
+
|
51
38
|
def clip(lower, upper=nil)
|
52
39
|
return lower if self < lower
|
53
40
|
return self unless upper
|
@@ -62,59 +49,10 @@ module Comparable
|
|
62
49
|
# 4.cap(5) #=> 4
|
63
50
|
# 6.cap(5) #=> 5
|
64
51
|
#
|
52
|
+
# CREDIT Trans
|
53
|
+
|
65
54
|
def cap(upper)
|
66
55
|
(self <= upper) ? self : upper
|
67
56
|
end
|
68
57
|
|
69
58
|
end
|
70
|
-
|
71
|
-
|
72
|
-
# _____ _
|
73
|
-
# |_ _|__ ___| |_
|
74
|
-
# | |/ _ \/ __| __|
|
75
|
-
# | | __/\__ \ |_
|
76
|
-
# |_|\___||___/\__|
|
77
|
-
#
|
78
|
-
=begin test
|
79
|
-
|
80
|
-
require 'test/unit'
|
81
|
-
|
82
|
-
class TestComparable < Test::Unit::TestCase
|
83
|
-
|
84
|
-
def test_at_most
|
85
|
-
assert_equal( 3, 3.at_most(4) )
|
86
|
-
assert_equal( 4, 4.at_most(4) )
|
87
|
-
assert_equal( 4, 5.at_most(4) )
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_at_least
|
91
|
-
assert_equal( 4, 3.at_least(4) )
|
92
|
-
assert_equal( 4, 4.at_least(4) )
|
93
|
-
assert_equal( 5, 5.at_least(4) )
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_clip
|
97
|
-
assert_equal( 4, 3.clip(4) )
|
98
|
-
assert_equal( 4, 4.clip(4) )
|
99
|
-
assert_equal( 5, 5.clip(4) )
|
100
|
-
assert_equal( 4, 4.clip(3,5) )
|
101
|
-
assert_equal( 3, 3.clip(3,5) )
|
102
|
-
assert_equal( 5, 5.clip(3,5) )
|
103
|
-
assert_equal( 3, 2.clip(3,5) )
|
104
|
-
assert_equal( 5, 6.clip(3,5) )
|
105
|
-
assert_equal( 'd', 'd'.clip('c','e') )
|
106
|
-
assert_equal( 'c', 'c'.clip('c','e') )
|
107
|
-
assert_equal( 'e', 'e'.clip('c','e') )
|
108
|
-
assert_equal( 'c', 'b'.clip('c','e') )
|
109
|
-
assert_equal( 'e', 'f'.clip('c','e') )
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_cap
|
113
|
-
assert_equal( 3, 3.cap(4) )
|
114
|
-
assert_equal( 4, 4.cap(4) )
|
115
|
-
assert_equal( 4, 5.cap(4) )
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
=end
|
@@ -1,23 +1,3 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Cmp and Succ/Pred
|
4
|
-
#
|
5
|
-
# SUMMARY:
|
6
|
-
#
|
7
|
-
# Comparison and Succ/Pred extensions.
|
8
|
-
#
|
9
|
-
# AUTHORS:
|
10
|
-
#
|
11
|
-
# - Florian Gross
|
12
|
-
# - Thomas Sawyer
|
13
|
-
#
|
14
|
-
# NOTES:
|
15
|
-
#
|
16
|
-
# - NOTE Are the module methods better as parameterized mixins?
|
17
|
-
#
|
18
|
-
# - TODO Maybe improve the naming or the module methods.
|
19
|
-
|
20
|
-
#
|
21
1
|
class Integer
|
22
2
|
# This is needed to allow Integer to look back to
|
23
3
|
# its Numeric ancestor for the new definition of #succ.
|
@@ -25,7 +5,6 @@ class Integer
|
|
25
5
|
remove_method( :succ )
|
26
6
|
end
|
27
7
|
|
28
|
-
#
|
29
8
|
module Comparable
|
30
9
|
# Alternate name for comparison operator #<=>.
|
31
10
|
#
|
@@ -35,6 +14,8 @@ module Comparable
|
|
35
14
|
#
|
36
15
|
# This fundamental compare method is used to keep
|
37
16
|
# comparison compatible with <tt>#succ</tt>.
|
17
|
+
#
|
18
|
+
# CREDIT Peter Vanbroekhoven
|
38
19
|
|
39
20
|
def cmp(o)
|
40
21
|
self<=>o
|
@@ -50,6 +31,8 @@ class String # Compare method that takes length into account.
|
|
50
31
|
# "abcd".cmp("abc") #=> 1
|
51
32
|
# "abc".cmp("abcd") #=> -1
|
52
33
|
# "xyz".cmp("abc") #=> 1
|
34
|
+
#
|
35
|
+
# CREDIT Peter Vanbroekhoven
|
53
36
|
|
54
37
|
def cmp(other)
|
55
38
|
return -1 if length < other.length
|
@@ -62,6 +45,8 @@ class String # Compare method that takes length into account.
|
|
62
45
|
# "abc".succ #=> "abd"
|
63
46
|
# "abc".succ(4) #=> "abg"
|
64
47
|
# "abc".succ(24) #=> "aca"
|
48
|
+
#
|
49
|
+
# CREDIT Trans
|
65
50
|
|
66
51
|
def succ(n=1)
|
67
52
|
s = self
|
@@ -77,6 +62,8 @@ class Numeric
|
|
77
62
|
#
|
78
63
|
# 4.distance(3) #=> 1
|
79
64
|
#
|
65
|
+
# CREDIT Trans
|
66
|
+
|
80
67
|
def distance( other )
|
81
68
|
self - other
|
82
69
|
end
|
@@ -85,6 +72,8 @@ class Numeric
|
|
85
72
|
#
|
86
73
|
# 3.succ(2) #=> 5
|
87
74
|
#
|
75
|
+
# CREDIT Trans
|
76
|
+
|
88
77
|
def succ(n=nil)
|
89
78
|
n ||= 1
|
90
79
|
self + n
|
@@ -94,79 +83,10 @@ class Numeric
|
|
94
83
|
#
|
95
84
|
# 3.pred(2) #=> 1
|
96
85
|
#
|
86
|
+
# CREDIT Trans
|
87
|
+
|
97
88
|
def pred(n=nil)
|
98
89
|
n ||= 1
|
99
90
|
self - n
|
100
91
|
end
|
101
92
|
end
|
102
|
-
|
103
|
-
|
104
|
-
# _____ _
|
105
|
-
# |_ _|__ ___| |_
|
106
|
-
# | |/ _ \/ __| __|
|
107
|
-
# | | __/\__ \ |_
|
108
|
-
# |_|\___||___/\__|
|
109
|
-
#
|
110
|
-
=begin test
|
111
|
-
|
112
|
-
require 'test/unit'
|
113
|
-
|
114
|
-
class TestComparable < Test::Unit::TestCase
|
115
|
-
|
116
|
-
def test_cmp
|
117
|
-
assert_equal( -1, 3.cmp(4) )
|
118
|
-
assert_equal( 0, 3.cmp(3) )
|
119
|
-
assert_equal( 1, 3.cmp(2) )
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
class TestStringCompare < Test::Unit::TestCase
|
125
|
-
|
126
|
-
def test_cmp
|
127
|
-
assert_equal( 0, "abc".cmp("abc") )
|
128
|
-
assert_equal( -1, "abc".cmp("abcd") )
|
129
|
-
assert_equal( 1, "abcd".cmp("abc") )
|
130
|
-
assert_equal( -1, "abc".cmp("bcd") )
|
131
|
-
assert_equal( 1, "bcd".cmp("abc") )
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_succ
|
135
|
-
assert_equal( "b", "a".succ )
|
136
|
-
assert_equal( "b", "a".succ(1) )
|
137
|
-
assert_equal( "c", "a".succ(2) )
|
138
|
-
assert_equal( "d", "a".succ(3) )
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
class TestNumericCompare < Test::Unit::TestCase
|
144
|
-
|
145
|
-
def test_distance
|
146
|
-
assert_equal( 4, 10.distance(6) )
|
147
|
-
assert_equal( 2, 10.distance(8) )
|
148
|
-
assert_equal( -2, 7.distance(9) )
|
149
|
-
end
|
150
|
-
|
151
|
-
|
152
|
-
def test_pred
|
153
|
-
assert_equal( 3, 4.pred )
|
154
|
-
assert_equal( -3, -2.pred )
|
155
|
-
assert_equal( 2, 4.pred(2) )
|
156
|
-
assert_equal( -4, -2.pred(2) )
|
157
|
-
assert_equal( 6, 4.pred(-2) )
|
158
|
-
assert_equal( 0, -2.pred(-2) )
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_succ
|
162
|
-
assert_equal( 5, 4.succ )
|
163
|
-
assert_equal( -1, -2.succ )
|
164
|
-
assert_equal( 6, 4.succ(2) )
|
165
|
-
assert_equal( 0, -2.succ(2) )
|
166
|
-
assert_equal( 2, 4.succ(-2) )
|
167
|
-
assert_equal( -4, -2.succ(-2) )
|
168
|
-
end
|
169
|
-
|
170
|
-
end
|
171
|
-
|
172
|
-
=end
|
@@ -6,7 +6,9 @@
|
|
6
6
|
#
|
7
7
|
# Conversion extensions.
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# _A horse is a horse, a horse of course!_
|
10
|
+
#
|
11
|
+
# AUTHORS:
|
10
12
|
#
|
11
13
|
# - Thomas Sawyer
|
12
14
|
# - Willian James
|
@@ -17,7 +19,7 @@
|
|
17
19
|
|
18
20
|
require 'facets/functor'
|
19
21
|
|
20
|
-
|
22
|
+
|
21
23
|
class Array
|
22
24
|
|
23
25
|
# Converts a two-element associative array into a hash.
|
@@ -30,11 +32,13 @@ class Array
|
|
30
32
|
# a = [ [:a,1,2], [:b,3] ]
|
31
33
|
# a.to_h(true) #=> { :a=>[1,2], :b=>[3] }
|
32
34
|
#
|
33
|
-
#
|
35
|
+
# Note that the use of a values parameter has been deprecated
|
34
36
|
# because that functionality is as simple as:
|
35
37
|
#
|
36
38
|
# array1.zip(array2).to_h
|
37
39
|
#
|
40
|
+
# CREDIT: Trans
|
41
|
+
|
38
42
|
def to_h(arrayed=nil)
|
39
43
|
h = {}
|
40
44
|
if arrayed #or (flatten.size % 2 == 1)
|
@@ -52,6 +56,10 @@ class Array
|
|
52
56
|
end
|
53
57
|
|
54
58
|
# Turn an array into a file path.
|
59
|
+
#
|
60
|
+
# ["usr", "local", "lib"].to_path #=> "usr/local/lib"
|
61
|
+
#
|
62
|
+
# CREDIT: Trans
|
55
63
|
|
56
64
|
def to_path
|
57
65
|
File.join( *(self.compact) )
|
@@ -59,10 +67,8 @@ class Array
|
|
59
67
|
|
60
68
|
end
|
61
69
|
|
62
|
-
#
|
63
|
-
class Class
|
64
70
|
|
65
|
-
|
71
|
+
class Class
|
66
72
|
|
67
73
|
# Convert instatiation of a class into a Proc.
|
68
74
|
#
|
@@ -78,6 +84,7 @@ class Class
|
|
78
84
|
#
|
79
85
|
# %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
|
80
86
|
#
|
87
|
+
# CREDIT: Daniel Schierbeck
|
81
88
|
|
82
89
|
def to_proc
|
83
90
|
proc{|*args| new(*args)}
|
@@ -85,7 +92,7 @@ class Class
|
|
85
92
|
|
86
93
|
end
|
87
94
|
|
88
|
-
|
95
|
+
|
89
96
|
module Enumerable
|
90
97
|
|
91
98
|
# Produces a hash from an Enumerable with index for keys.
|
@@ -100,7 +107,9 @@ module Enumerable
|
|
100
107
|
# enu = 'a'..'b'
|
101
108
|
# enu.to_hash{ |e,i| e } #=> { 0=>'a', 1=>'b' }
|
102
109
|
#
|
103
|
-
# See also #
|
110
|
+
# See also #mash.
|
111
|
+
#
|
112
|
+
# CREDIT: Trans
|
104
113
|
|
105
114
|
def to_hash( &blk )
|
106
115
|
h = {}
|
@@ -112,7 +121,10 @@ module Enumerable
|
|
112
121
|
h
|
113
122
|
end
|
114
123
|
|
124
|
+
# Convert an Enumerable object into a hash bu first
|
125
|
+
# turning it into an array.
|
115
126
|
#
|
127
|
+
# CREDIT: Trans
|
116
128
|
|
117
129
|
def to_h(arrayed=nil)
|
118
130
|
to_a.to_h(arrayed)
|
@@ -120,11 +132,13 @@ module Enumerable
|
|
120
132
|
|
121
133
|
end
|
122
134
|
|
123
|
-
|
135
|
+
|
124
136
|
class Hash
|
125
137
|
|
126
138
|
# Like Array#join but specialized to Hash.
|
127
139
|
#
|
140
|
+
# CREDIT: Mauricio Fernandez
|
141
|
+
|
128
142
|
def join(pair_divider='', elem_divider='')
|
129
143
|
s = []
|
130
144
|
each_pair { |k,v| s << "#{k}#{pair_divider}#{v}" }
|
@@ -135,13 +149,9 @@ class Hash
|
|
135
149
|
#
|
136
150
|
# {"a"=>1,"b"=>2}.to_h #=> {"b"=>2,"a"=>1}
|
137
151
|
#
|
138
|
-
|
139
|
-
# A horse is a horse, a horse of course.
|
140
|
-
#++
|
141
|
-
def to_h; rehash; end
|
152
|
+
# CREDIT: Forian Gross
|
142
153
|
|
143
|
-
|
144
|
-
# CREDIT Mauricio Fernandez
|
154
|
+
def to_h; rehash; end
|
145
155
|
|
146
156
|
# Constructs a Proc object from a hash such
|
147
157
|
# that the parameter of the Proc is assigned
|
@@ -153,6 +163,7 @@ class Hash
|
|
153
163
|
# p.call(o)
|
154
164
|
# o.a #=> 1
|
155
165
|
#
|
166
|
+
# CREDIT: Trans
|
156
167
|
|
157
168
|
def to_proc
|
158
169
|
lambda do |o|
|
@@ -163,6 +174,13 @@ class Hash
|
|
163
174
|
end
|
164
175
|
end
|
165
176
|
|
177
|
+
# A fault-tolerent version of #to_proc.
|
178
|
+
#
|
179
|
+
# It works just like #to_proc, but the block will make
|
180
|
+
# sure# the object responds to the assignment.
|
181
|
+
#
|
182
|
+
# CREDIT: Trans
|
183
|
+
|
166
184
|
def to_proc_with_reponse
|
167
185
|
lambda do |o|
|
168
186
|
self.each do |k,v|
|
@@ -172,12 +190,12 @@ class Hash
|
|
172
190
|
end
|
173
191
|
end
|
174
192
|
|
175
|
-
# CREDIT Daniel Berger
|
176
|
-
|
177
193
|
# A method to convert a Hash into a Struct.
|
178
194
|
#
|
179
195
|
# h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
|
180
196
|
# s = h.to_struct("Foo")
|
197
|
+
#
|
198
|
+
# CREDIT: Daniel Berger
|
181
199
|
|
182
200
|
def to_struct(struct_name)
|
183
201
|
Struct.new(struct_name,*keys).new(*values)
|
@@ -186,23 +204,30 @@ class Hash
|
|
186
204
|
end
|
187
205
|
|
188
206
|
|
189
|
-
#
|
190
207
|
class NilClass
|
191
208
|
|
192
209
|
# Why doesn't Ruby have therese already?
|
193
210
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
211
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
212
|
+
|
213
|
+
# Allows <tt>nil</tt> to respond to #to_f.
|
214
|
+
# Always returns <tt>0</tt>.
|
215
|
+
#
|
216
|
+
# nil.to_f #=> 0.0
|
217
|
+
#
|
218
|
+
# CREDIT: Matz
|
219
|
+
|
220
|
+
def to_f; 0.0; end
|
221
|
+
|
222
|
+
end
|
200
223
|
|
201
224
|
# Allows <tt>nil</tt> to create an empty hash,
|
202
225
|
# similar to #to_a and #to_s.
|
203
226
|
#
|
204
|
-
#
|
227
|
+
# nil.to_h #=> {}
|
205
228
|
#
|
229
|
+
# CREDIT: Trans
|
230
|
+
|
206
231
|
def to_h; {}; end
|
207
232
|
|
208
233
|
end
|
@@ -219,6 +244,7 @@ class Proc
|
|
219
244
|
# }
|
220
245
|
# l.to_h #=> {:a=>1, :b=>2, :c=>3}
|
221
246
|
#
|
247
|
+
# CREDIT: Trans
|
222
248
|
|
223
249
|
def to_h
|
224
250
|
h = {}
|
@@ -229,12 +255,14 @@ class Proc
|
|
229
255
|
|
230
256
|
end
|
231
257
|
|
232
|
-
|
258
|
+
|
233
259
|
class Range
|
234
260
|
|
235
261
|
# A thing really should know itself.
|
236
262
|
# This simply returns _self_.
|
237
263
|
#
|
264
|
+
# CREDIT: Trans
|
265
|
+
|
238
266
|
def to_r
|
239
267
|
self
|
240
268
|
end
|
@@ -246,13 +274,15 @@ class Range
|
|
246
274
|
# interpretor such that it can coerce Range-like
|
247
275
|
# objects into a Range.
|
248
276
|
#
|
277
|
+
# CREDIT: Trans
|
278
|
+
|
249
279
|
def to_range
|
250
280
|
self
|
251
281
|
end
|
252
282
|
|
253
283
|
end
|
254
284
|
|
255
|
-
|
285
|
+
|
256
286
|
class Regexp
|
257
287
|
|
258
288
|
# Simply returns itself. Helpful when converting
|
@@ -263,6 +293,8 @@ class Regexp
|
|
263
293
|
#
|
264
294
|
# /abc/.to_re #=> /abc/
|
265
295
|
#
|
296
|
+
# CREDIT: Trans
|
297
|
+
|
266
298
|
def to_re( esc=false )
|
267
299
|
self # Of course, things really should know how to say "I" ;)
|
268
300
|
end
|
@@ -270,14 +302,17 @@ class Regexp
|
|
270
302
|
# Like #to_re, but following Ruby's formal definitions,
|
271
303
|
# only a Regular expression type object will respond to this.
|
272
304
|
#
|
273
|
-
# Note
|
305
|
+
# Note that to be of much real use this should be defined in core Ruby.
|
306
|
+
#
|
307
|
+
# CREDIT: Florian Gross
|
308
|
+
|
274
309
|
def to_regexp
|
275
310
|
self
|
276
311
|
end
|
277
312
|
|
278
313
|
end
|
279
314
|
|
280
|
-
|
315
|
+
|
281
316
|
class String
|
282
317
|
|
283
318
|
# Get a constant by a given string name.
|
@@ -288,12 +323,16 @@ class String
|
|
288
323
|
# since it can not access contants relative to the current
|
289
324
|
# execution context. But without a binding_of_caller that
|
290
325
|
# does not seem possible.
|
326
|
+
#
|
327
|
+
|
291
328
|
def to_const
|
292
329
|
split('::').inject(Object){ |namespace,name| namespace.const_get(name) }
|
293
330
|
end
|
294
331
|
|
295
332
|
# Parse data from string.
|
296
333
|
#
|
334
|
+
# CREDIT: Trans
|
335
|
+
|
297
336
|
def to_date
|
298
337
|
require 'date'
|
299
338
|
require 'parsedate'
|
@@ -302,6 +341,8 @@ class String
|
|
302
341
|
|
303
342
|
# Parse string to time.
|
304
343
|
#
|
344
|
+
# CREDIT: Trans
|
345
|
+
|
305
346
|
def to_time
|
306
347
|
require 'time'
|
307
348
|
Time.parse(self)
|
@@ -313,6 +354,8 @@ class String
|
|
313
354
|
#
|
314
355
|
# "[".to_re #=> /\[/
|
315
356
|
#
|
357
|
+
# CREDIT: Trans
|
358
|
+
|
316
359
|
def to_re(esc=true)
|
317
360
|
Regexp.new((esc ? Regexp.escape(self) : self))
|
318
361
|
end
|
@@ -322,6 +365,8 @@ class String
|
|
322
365
|
#
|
323
366
|
# "a?".to_rx #=> /a?/
|
324
367
|
#
|
368
|
+
# CREDIT: Trans
|
369
|
+
|
325
370
|
def to_rx
|
326
371
|
Regexp.new(self)
|
327
372
|
end
|
@@ -332,15 +377,15 @@ class String
|
|
332
377
|
# xyp.class #=> Proc
|
333
378
|
# xyp.call(1,2) #=> 3
|
334
379
|
#
|
335
|
-
|
336
|
-
#
|
337
|
-
#
|
338
|
-
|
380
|
+
# NOTE: Sure would be nice if this could grab the caller's context!
|
381
|
+
#
|
382
|
+
# CREDIT: Trans
|
383
|
+
|
339
384
|
def to_proc(context=nil)
|
340
385
|
if context
|
341
386
|
if context.kind_of?(Binding) or context.kind_of?(Proc)
|
342
387
|
Kernel.eval "proc { #{self} }", context
|
343
|
-
else context
|
388
|
+
else #context
|
344
389
|
context.instance_eval "proc { #{self} }"
|
345
390
|
end
|
346
391
|
else
|
@@ -364,9 +409,34 @@ class String
|
|
364
409
|
|
365
410
|
end
|
366
411
|
|
367
|
-
|
412
|
+
|
368
413
|
class Symbol
|
369
414
|
|
415
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
416
|
+
|
417
|
+
# Turn a symbol into a proc calling the method to
|
418
|
+
# which it refers.
|
419
|
+
#
|
420
|
+
# up = :upcase.to_proc
|
421
|
+
# up.call("hello") #=> HELLO
|
422
|
+
#
|
423
|
+
# More useful is the fact that this allows <tt>&</tt>
|
424
|
+
# to be used to coerce Symbol into Proc.
|
425
|
+
#
|
426
|
+
# %w{foo bar qux}.map(&:upcase) #=> ["FOO","BAR","QUX"]
|
427
|
+
# [1, 2, 3].inject(&:+) #=> 6
|
428
|
+
#
|
429
|
+
# TODO: Deprecate for Ruby 1.9+.
|
430
|
+
#
|
431
|
+
# CREDIT: Florian Gross
|
432
|
+
# CREDIT: Nobuhiro Imai
|
433
|
+
|
434
|
+
def to_proc
|
435
|
+
Proc.new{|*args| args.shift.__send__(self, *args)}
|
436
|
+
end
|
437
|
+
|
438
|
+
end
|
439
|
+
|
370
440
|
# Get a constant by a given symbol name.
|
371
441
|
#
|
372
442
|
# :Class.to_const #=> Class
|
@@ -375,31 +445,11 @@ class Symbol
|
|
375
445
|
# since it can not access contants relative to the current
|
376
446
|
# execution context. But without a binding_of_caller that
|
377
447
|
# does not seem possible.
|
448
|
+
|
378
449
|
def to_const
|
379
450
|
to_s.split('::').inject(Object){ |namespace,name| namespace.const_get(name) }
|
380
451
|
end
|
381
452
|
|
382
|
-
# CREDIT Florian Gross
|
383
|
-
# CREDIT Nobuhiro Imai
|
384
|
-
|
385
|
-
# NOTE Symbol#to_proc is already in 1.9+.
|
386
|
-
|
387
|
-
# Turn a symbol into a proc calling the method to
|
388
|
-
# which it refers.
|
389
|
-
#
|
390
|
-
# up = :upcase.to_proc
|
391
|
-
# up.call("hello") #=> HELLO
|
392
|
-
#
|
393
|
-
# More useful is the fact that this allows <tt>&</tt>
|
394
|
-
# to be used to coerce Symbol into Proc.
|
395
|
-
#
|
396
|
-
# %w{foo bar qux}.map(&:upcase) #=> ["FOO","BAR","QUX"]
|
397
|
-
# [1, 2, 3].inject(&:+) #=> 6
|
398
|
-
|
399
|
-
def to_proc
|
400
|
-
Proc.new{|*args| args.shift.__send__(self, *args)}
|
401
|
-
end
|
402
|
-
|
403
453
|
# Same functionality as before, just a touch more efficient.
|
404
454
|
#
|
405
455
|
#--
|
@@ -426,279 +476,30 @@ class Symbol
|
|
426
476
|
|
427
477
|
end
|
428
478
|
|
429
|
-
#
|
430
|
-
class Time
|
431
|
-
|
432
|
-
#--
|
433
|
-
# CHANGE 2006-12-10 <transfire@gmail.com>
|
434
|
-
# * Added FORMAT constant to make more flexible.
|
435
|
-
#++
|
436
|
-
|
437
|
-
unless const_defined?('FORMAT')
|
438
|
-
FORMAT = {
|
439
|
-
:db => "%Y-%m-%d %H:%M:%S",
|
440
|
-
:dbase => "%Y-%m-%d %H:%M:%S",
|
441
|
-
:datbase => "%Y-%m-%d %H:%M:%S",
|
442
|
-
:utc => "%Y-%m-%d %H:%M:%S",
|
443
|
-
:short => "%d %b %H:%M",
|
444
|
-
:long => "%B %d, %Y %H:%M",
|
445
|
-
:day1st => "%d-%m-%Y %H:%M",
|
446
|
-
:dmYHM => "%d-%m-%Y %H:%M",
|
447
|
-
:rfc822 => "%a, %d %b %Y %H:%M:%S %z",
|
448
|
-
nil => "%a %b %d %H:%M:%S %Z %Y"
|
449
|
-
}
|
450
|
-
end
|
451
|
-
|
452
|
-
def stamp( fmt = nil )
|
453
|
-
unless String === fmt
|
454
|
-
fmt = FORMAT[fmt]
|
455
|
-
end
|
456
|
-
strftime(fmt).strip
|
457
|
-
end
|
458
|
-
|
459
|
-
def self.stamp(*args)
|
460
|
-
now.stamp(*args)
|
461
|
-
end
|
462
|
-
|
463
|
-
# Enhance #to_s by aliasing to #stamp.
|
464
|
-
alias_method :to_s, :stamp
|
465
|
-
|
466
|
-
# Convert a Time to a Date. Time is a superset of Date.
|
467
|
-
# It is the year, month and day that are carried over.
|
468
|
-
def to_date
|
469
|
-
require 'date'
|
470
|
-
::Date.new(year, month, day)
|
471
|
-
end
|
472
|
-
|
473
|
-
# To be able to keep Dates and Times interchangeable
|
474
|
-
# on conversions.
|
475
|
-
def to_time
|
476
|
-
self
|
477
|
-
end
|
478
|
-
|
479
|
-
end
|
480
|
-
|
481
|
-
|
482
|
-
# _____ _
|
483
|
-
# |_ _|__ ___| |_
|
484
|
-
# | |/ _ \/ __| __|
|
485
|
-
# | | __/\__ \ |_
|
486
|
-
# |_|\___||___/\__|
|
487
|
-
#
|
488
|
-
=begin test
|
489
|
-
|
490
|
-
require 'test/unit'
|
491
|
-
|
492
|
-
|
493
|
-
class TestArrayConversion < Test::Unit::TestCase
|
494
|
-
|
495
|
-
def test_to_h_1
|
496
|
-
k = [1,3,5]
|
497
|
-
v = [2,4,6]
|
498
|
-
assert_equal( { 1=>2, 3=>4, 5=>6 }, k.zip(v).to_h )
|
499
|
-
end
|
500
|
-
|
501
|
-
def test_to_h_2
|
502
|
-
a = [[1,2],[3,4],[5,6]]
|
503
|
-
assert_equal( { 1=>2, 3=>4, 5=>6 }, a.to_h )
|
504
|
-
end
|
505
|
-
|
506
|
-
def test_to_hash
|
507
|
-
a = [:a,:b,:c]
|
508
|
-
assert_equal( { 0=>:a, 1=>:b, 2=>:c }, a.to_hash )
|
509
|
-
end
|
510
|
-
|
511
|
-
end
|
512
|
-
|
513
|
-
|
514
|
-
class TestClassConversion < Test::Unit::TestCase
|
515
|
-
|
516
|
-
Person = Struct.new(:name)
|
517
|
-
|
518
|
-
def test_to_proc
|
519
|
-
people = ["joe"].map(&Person)
|
520
|
-
assert_equal("joe", people[0].name)
|
521
|
-
end
|
522
|
-
|
523
|
-
end
|
524
|
-
|
525
|
-
|
526
|
-
class TestEnumerableConversion < Test::Unit::TestCase
|
527
|
-
|
528
|
-
def test_to_hash
|
529
|
-
a = [:a,:b,:c]
|
530
|
-
assert_equal( { 0=>:a, 1=>:b, 2=>:c }, a.to_h )
|
531
|
-
end
|
532
|
-
|
533
|
-
end
|
534
|
-
|
535
|
-
|
536
|
-
class TestHashConversion < Test::Unit::TestCase
|
537
|
-
|
538
|
-
def test_to_h
|
539
|
-
a = { :a => 1, :b => 2, :c => 3 }
|
540
|
-
assert_equal( a, a.to_h )
|
541
|
-
end
|
542
|
-
|
543
|
-
end
|
544
|
-
|
545
|
-
|
546
|
-
class TestNilClassConversion < Test::Unit::TestCase
|
547
|
-
|
548
|
-
def test_to_f
|
549
|
-
assert_equal( 0, nil.to_f )
|
550
|
-
end
|
551
|
-
|
552
|
-
def test_to_h
|
553
|
-
assert_equal( {}, nil.to_h )
|
554
|
-
end
|
555
|
-
|
556
|
-
end
|
557
|
-
|
558
|
-
|
559
|
-
class TestRangeConversion < Test::Unit::TestCase
|
560
|
-
|
561
|
-
def test_to_r
|
562
|
-
a = (0..10)
|
563
|
-
assert_equal( a, a.to_r )
|
564
|
-
end
|
565
|
-
|
566
|
-
def test_to_range
|
567
|
-
a = (0..10)
|
568
|
-
assert_equal( a, a.to_range )
|
569
|
-
end
|
570
|
-
|
571
|
-
end
|
572
479
|
|
480
|
+
class Time
|
573
481
|
|
574
|
-
|
575
|
-
|
576
|
-
def test_to_re
|
577
|
-
r = /0..10/
|
578
|
-
assert_equal( r, r.to_re )
|
579
|
-
end
|
580
|
-
|
581
|
-
def test_to_regexp
|
582
|
-
r = /0..10/
|
583
|
-
assert_equal( r, r.to_regexp )
|
584
|
-
end
|
585
|
-
|
586
|
-
end
|
587
|
-
|
588
|
-
|
589
|
-
class TestStringConversion < Test::Unit::TestCase
|
590
|
-
|
591
|
-
TestConst = 4
|
592
|
-
|
593
|
-
def test_to_const
|
594
|
-
assert_equal( 4, "TestStringConversion::TestConst".to_const )
|
595
|
-
end
|
596
|
-
|
597
|
-
def test_to_date
|
598
|
-
s = "2005-10-31"
|
599
|
-
d = s.to_date
|
600
|
-
assert_equal( 31, d.day )
|
601
|
-
assert_equal( 10, d.month )
|
602
|
-
assert_equal( 2005, d.year )
|
603
|
-
end
|
604
|
-
|
605
|
-
def test_to_time
|
606
|
-
s = "2:31:15 PM"
|
607
|
-
t = s.to_time
|
608
|
-
assert_equal( 14, t.hour )
|
609
|
-
assert_equal( 31, t.min )
|
610
|
-
assert_equal( 15, t.sec )
|
611
|
-
end
|
612
|
-
|
613
|
-
def test_to_re
|
614
|
-
assert_equal( /abc/, "abc".to_re )
|
615
|
-
assert_equal( /a\+bc/, "a+bc".to_re )
|
616
|
-
assert_equal( /a+bc/, "a+bc".to_re(false) )
|
617
|
-
end
|
618
|
-
|
619
|
-
def test_to_proc
|
620
|
-
assert_nothing_raised { @add = '|x,y| x + y'.to_proc }
|
621
|
-
assert_equal(4, @add.call(2,2))
|
622
|
-
@t = 3
|
623
|
-
@multi = '|y| @t * y'.to_proc(self)
|
624
|
-
assert_equal(6, @multi.call(2))
|
625
|
-
x = 4
|
626
|
-
@div = '|y| x / y'.to_proc(binding)
|
627
|
-
assert_equal(2, @div.call(2))
|
628
|
-
end
|
629
|
-
|
630
|
-
#def test_to_a
|
631
|
-
# arr = 'abc'
|
632
|
-
# assert_equal( ['a','b','c'], arr.to_a )
|
633
|
-
#end
|
634
|
-
|
635
|
-
end
|
636
|
-
|
637
|
-
|
638
|
-
class TestSymbolConversion < Test::Unit::TestCase
|
639
|
-
|
640
|
-
TESTCONST = 1
|
641
|
-
|
642
|
-
def test_to_const
|
643
|
-
assert_equal( 1, :"TestSymbolConversion::TESTCONST".to_const )
|
644
|
-
end
|
645
|
-
|
646
|
-
def test_proc_01
|
647
|
-
assert_instance_of(Proc, up = :upcase.to_proc )
|
648
|
-
assert_equal( "HELLO", up.call("hello") )
|
649
|
-
end
|
650
|
-
|
651
|
-
def test_proc_02
|
652
|
-
q = [[1,2,3], [4,5,6], [7,8,9]].map(&:reverse)
|
653
|
-
a = [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
|
654
|
-
assert_equal( a, q )
|
655
|
-
end
|
656
|
-
|
657
|
-
# Deprecated b/c of stringe issues.
|
658
|
-
|
659
|
-
#def test_to_str
|
660
|
-
# assert_equal( "a", :a.to_str )
|
661
|
-
# assert_equal( "abc", :abc.to_str )
|
662
|
-
# assert_equal( '#$%', :'#$%'.to_str )
|
663
|
-
#end
|
664
|
-
|
665
|
-
end
|
666
|
-
|
667
|
-
|
668
|
-
require 'time'
|
669
|
-
|
670
|
-
class TestTimeConversion < Test::Unit::TestCase
|
671
|
-
|
672
|
-
def setup
|
673
|
-
@t = Time.parse('4/20/2005 15:37')
|
674
|
-
end
|
675
|
-
|
676
|
-
# FIXME Problem with Timezone on different systems.
|
677
|
-
|
678
|
-
#def test_stamp_01
|
679
|
-
# assert_equal( "Wed Apr 20 15:37:00 PDT 2005", @t.stamp )
|
680
|
-
#end
|
482
|
+
unless (RUBY_VERSION[0,3] == '1.9')
|
681
483
|
|
682
|
-
|
683
|
-
|
684
|
-
|
484
|
+
# Convert a Time to a Date. Time is a superset of Date.
|
485
|
+
# It is the year, month and day that are carried over.
|
486
|
+
#
|
487
|
+
# CREDIT: Trans
|
685
488
|
|
686
|
-
def
|
687
|
-
|
489
|
+
def to_date
|
490
|
+
require 'date'
|
491
|
+
::Date.new(year, month, day)
|
688
492
|
end
|
689
493
|
|
690
|
-
|
691
|
-
|
692
|
-
|
494
|
+
# To be able to keep Dates and Times interchangeable
|
495
|
+
# on conversions.
|
496
|
+
#
|
497
|
+
# CREDIT: Trans
|
693
498
|
|
694
|
-
|
695
|
-
|
696
|
-
#end
|
697
|
-
|
698
|
-
def test_to_time
|
699
|
-
assert_instance_of( ::Time, @t.to_time )
|
499
|
+
def to_time
|
500
|
+
self
|
700
501
|
end
|
701
502
|
|
702
503
|
end
|
703
504
|
|
704
|
-
|
505
|
+
end
|