facets 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +3 -3
- data/README +6 -7
- data/lib/core/facets.rb +1 -46
- data/lib/core/facets/array.rb +3 -0
- data/lib/core/facets/array/indexable.rb +6 -1
- data/lib/core/facets/array/only.rb +20 -0
- data/lib/core/facets/dir/multiglob.rb +12 -1
- data/lib/core/facets/enumerable.rb +0 -1
- data/lib/core/facets/enumerable/collect.rb +1 -0
- data/lib/core/facets/enumerable/combination.rb +44 -90
- data/lib/core/facets/facets.rb +46 -0
- data/lib/core/facets/file/write.rb +46 -58
- data/lib/core/facets/hash.rb +2 -0
- data/lib/core/facets/hash/select.rb +14 -0
- data/lib/core/facets/integer/multiples.rb +12 -55
- data/lib/core/facets/kernel/val.rb +14 -0
- data/lib/core/facets/module/alias.rb +28 -9
- data/lib/core/facets/module/methods.rb +18 -0
- data/lib/core/facets/module/traits.rb +65 -70
- data/lib/core/facets/proc/compose.rb +15 -12
- data/lib/core/facets/stackable.rb +3 -2
- data/lib/core/facets/string/format.rb +4 -6
- data/lib/core/facets/string/tabs.rb +34 -0
- data/lib/core/facets/symbol.rb +1 -0
- data/lib/core/facets/symbol/succ.rb +1 -42
- data/lib/core/facets/symbol/to_proc.rb +34 -0
- data/lib/methods/facets/array/contains.rb +1 -0
- data/lib/methods/facets/facets/require_core.rb +1 -0
- data/lib/methods/facets/file/writelines.rb +1 -0
- data/lib/methods/facets/io/writelines.rb +1 -0
- data/lib/methods/facets/kernel/not_nil.rb +1 -0
- data/lib/methods/facets/module/conflict.rb +1 -0
- data/lib/methods/facets/module/instance_method_defined.rb +1 -0
- data/lib/methods/facets/module/module_method_defined.rb +1 -0
- data/lib/methods/facets/module/private_conflict.rb +1 -0
- data/lib/methods/facets/module/protected_conflict.rb +1 -0
- data/lib/methods/facets/module/public_conflict.rb +1 -0
- data/lib/methods/facets/string/expand_tabs.rb +1 -0
- data/lib/methods/facets/string/outdent.rb +1 -0
- data/lib/methods/facets/string/taballto.rb +1 -0
- data/lib/more/facets/arguments.rb +1 -1
- data/lib/more/facets/association.rb +0 -46
- data/lib/more/facets/autoarray.rb +0 -28
- data/lib/more/facets/command.rb +341 -8
- data/lib/more/facets/dictionary.rb +25 -131
- data/lib/more/facets/downloader.rb +1 -1
- data/lib/more/facets/infinity.rb +3 -3
- data/lib/more/facets/interval.rb +0 -161
- data/lib/more/facets/multiton.rb +16 -12
- data/lib/more/facets/namespace.rb +1 -1
- data/lib/more/facets/ostruct.rb +46 -10
- data/lib/more/facets/overload.rb +0 -51
- data/lib/more/facets/paramix.rb +0 -97
- data/lib/more/facets/pp_s.rb +30 -0
- data/lib/more/facets/progressbar.rb +18 -10
- data/lib/more/facets/prototype.rb +1 -40
- data/lib/more/facets/random.rb +1 -0
- data/lib/more/facets/rbsystem.rb +4 -1
- data/lib/more/facets/snapshot.rb +8 -1
- data/lib/more/facets/stylize.rb +2 -0
- data/meta/{project.yaml → facets-2.0.3.roll} +22 -14
- data/meta/manifest.txt +38 -8
- data/task/{config.yaml → config/general.yaml} +7 -2
- data/task/{rdoc.yaml → config/rdoc.yaml} +1 -1
- data/task/crosstest +309 -0
- data/task/isotest +293 -0
- data/task/loadtest +28 -0
- data/task/methods +4 -4
- data/task/prepare +5 -0
- data/task/publish +2 -2
- data/task/rdoc +1 -0
- data/task/syntax +29 -0
- data/task/test +0 -1
- data/task/testeach +42 -0
- data/task/testpairs +50 -0
- data/test/lib/rq.rb +15 -0
- data/test/unit/array/test_merge.rb +21 -43
- data/test/unit/array/test_only.rb +21 -0
- data/test/unit/enumerable/test_collect.rb +1 -21
- data/test/unit/enumerable/test_combination.rb +50 -44
- data/test/unit/file/test_topath.rb +48 -57
- data/test/unit/file/test_write.rb +82 -0
- data/test/unit/hash/test_select.rb +43 -0
- data/test/unit/integer/test_multiples.rb +28 -32
- data/test/unit/kernel/test_report.rb +9 -12
- data/test/unit/kernel/test_val.rb +50 -45
- data/test/unit/module/test_include.rb +56 -57
- data/test/unit/module/test_name.rb +42 -55
- data/test/unit/module/test_traits.rb +46 -47
- data/test/unit/string/test_filter.rb +19 -34
- data/test/unit/string/test_format.rb +87 -96
- data/test/unit/string/test_regesc.rb +18 -26
- data/test/unit/string/test_tabs.rb +226 -119
- data/test/unit/symbol/test_succ.rb +14 -23
- data/test/unit/symbol/test_to_proc.rb +41 -0
- data/test/unit/test_association.rb +38 -47
- data/test/unit/test_attributes.rb +24 -33
- data/test/unit/test_autoarray.rb +23 -32
- data/test/unit/test_command.rb +26 -0
- data/test/unit/test_dictionary.rb +123 -117
- data/test/unit/test_infinity.rb +41 -47
- data/test/unit/test_inheritor.rb +133 -142
- data/test/unit/test_interval.rb +129 -93
- data/test/unit/test_ostruct.rb +148 -101
- data/test/unit/test_overload.rb +8 -15
- data/test/unit/test_paramix.rb +67 -73
- data/test/unit/test_pp_s.rb +23 -0
- data/test/unit/test_prototype.rb +28 -38
- metadata +47 -11
- data/lib/core/facets/enumerable/instance_map.rb +0 -0
- data/lib/more/facets/command_options.rb +0 -328
- data/meta/version.txt +0 -1
- data/task/load +0 -39
- data/test/unit/test_command_options.rb +0 -29
data/AUTHORS
CHANGED
@@ -7,13 +7,13 @@
|
|
7
7
|
== Toplevel Developers
|
8
8
|
|
9
9
|
The following developers have contributed significant
|
10
|
-
amounts of time to the
|
10
|
+
amounts of time to the development of Facets. They are held
|
11
11
|
in the highest regard for their efforts.
|
12
12
|
|
13
|
+
* Noah Gibbs
|
14
|
+
* Florian Gross
|
13
15
|
* George Moschovitis
|
14
16
|
* Tyler Rick
|
15
|
-
* Florian Gross
|
16
|
-
* Peter Vanbroekhoven
|
17
17
|
|
18
18
|
== Rubyists of Distinction
|
19
19
|
|
data/README
CHANGED
@@ -36,10 +36,9 @@ IMPORTANT! Note that setup.rb is no longer used b/c of Facets' new layout.
|
|
36
36
|
|
37
37
|
== Compatibility with 1.x series.
|
38
38
|
|
39
|
-
Prior to 2.0, Facets was divided between CORE and MORE --standalone extensions vs. classes and modules, respectively. With 2.0, the notion of "CORE" has taken only a slightly
|
39
|
+
Prior to 2.0, Facets was divided between CORE and MORE --standalone extensions vs. classes and modules, respectively. With 2.0, the notion of "CORE" has taken only a slightly different meaning. Instead CORE now consists of the libraries that are thought essential and as such are loaded automatically when using ++require "facets"++. While still primarily made up of extension methods a few classes now belong to core as well.
|
40
40
|
|
41
|
-
Additionally, the extension methods are no longer stored on a per-method basis. While dividing the extension methods up on a per-method basis had certain advantages, not the least of which was a simple organization, it proved too granular --more "subatomic" than "atomic". With 2.0 we have address this issue. All the extension methods have now been organized into small tightly related groups.
|
42
|
-
However, being able to require on the basis of a method is still a useful approach, so a compatibility layer for the 1.x series has been created. It makes it possible to load Facets libraries on a per method basis, just as before, via require redirection. For example:
|
41
|
+
Additionally, the extension methods are no longer stored on a per-method basis. While dividing the extension methods up on a per-method basis had certain advantages, not the least of which was a simple organization, it proved too granular --more "subatomic" than "atomic". With 2.0 we have address this issue. All the extension methods have now been organized into small tightly related groups. However, being able to require on the basis of a method is still a useful approach, so a compatibility layer for the 1.x series has been created. It makes it possible to load Facets libraries on a per method basis, just as before, via require redirection. For example:
|
43
42
|
|
44
43
|
require 'facets/string/underscore'
|
45
44
|
|
@@ -89,7 +88,7 @@ For example:
|
|
89
88
|
|
90
89
|
require 'facets/time/stamp'
|
91
90
|
|
92
|
-
Most "atoms" contain only a few methods, sometimes only one, but a few exceptions provide quite a few
|
91
|
+
Most "atoms" contain only a few methods, sometimes only one, but a few exceptions provide quite a few methods, such as ++string/indexable.rb++.
|
93
92
|
|
94
93
|
You can load per-class or per-module groups of core methods by requiring the class or module by name. For example"
|
95
94
|
|
@@ -97,7 +96,7 @@ You can load per-class or per-module groups of core methods by requiring the cla
|
|
97
96
|
|
98
97
|
Will require all the Time method extensions.
|
99
98
|
|
100
|
-
Note that some methods that were part of CORE in 1.8 and earlier are now part of MORE libraries. A good example is 'random.rb'. There were separated
|
99
|
+
Note that some methods that were part of CORE in 1.8 and earlier are now part of MORE libraries. A good example is 'random.rb'. There were separated because they had more specialized use cases, where as CORE extensions are intended as general purpose.
|
101
100
|
|
102
101
|
Using a Facets/MORE library of modules, classes or microframeworks is essentially the same. For example:
|
103
102
|
|
@@ -156,7 +155,7 @@ Facets simply takes the '*' and translates it into a string acceptable to all fi
|
|
156
155
|
|
157
156
|
This project thrives on contribution.
|
158
157
|
|
159
|
-
If you have any extension methods, classes, modules or small frameworks that you think have general applicability and would like to see them included in this project, don't hesitiate to submit. There's a very good chance
|
158
|
+
If you have any extension methods, classes, modules or small frameworks that you think have general applicability and would like to see them included in this project, don't hesitiate to submit. There's a very good chance they will be included. Also, if you have better versions of any thing already included or simply have a patch, they too are more than welcome. We want Ruby Facets to be of the highest quality.
|
160
159
|
|
161
160
|
|
162
161
|
== Authors
|
@@ -195,7 +194,7 @@ Acknowledgments and Copyrights for particular snippets of borrowed code are give
|
|
195
194
|
|
196
195
|
----
|
197
196
|
|
198
|
-
[1] A lot of mental anguish went into finding this good project name Ruby Facets. Of course, in the end only one name can take the honor. Other good names which were considered: Calibre, Florida and California, Warchest w/ Atomix, Downs & Ace, Trix & Atomx and even Pillbox & Pills (a _why suggestion). Then the names that almost won out and were used for a good while: Nano Methods and Mega Modules --great names but a little
|
197
|
+
[1] A lot of mental anguish went into finding this good project name Ruby Facets. Of course, in the end only one name can take the honor. Other good names which were considered: Calibre, Florida and California, Warchest w/ Atomix, Downs & Ace, Trix & Atomx and even Pillbox & Pills (a _why suggestion). Then the names that almost won out and were used for a good while: Nano Methods and Mega Modules --great names but a little too "fad". Finally let's not forget even older "working" titles that were used along the way: Raspberry, ABC, Succ and the very original Tomslib.
|
199
198
|
|
200
199
|
----
|
201
200
|
|
data/lib/core/facets.rb
CHANGED
@@ -1,46 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module Facets
|
4
|
-
extend self
|
5
|
-
|
6
|
-
def require_core
|
7
|
-
require "facets/array"
|
8
|
-
require "facets/basicobject"
|
9
|
-
require "facets/binding"
|
10
|
-
require "facets/boolean"
|
11
|
-
require "facets/class"
|
12
|
-
require "facets/comparable"
|
13
|
-
require "facets/continuation"
|
14
|
-
require "facets/conversion"
|
15
|
-
require "facets/curry"
|
16
|
-
require "facets/dir"
|
17
|
-
require "facets/enumerable"
|
18
|
-
require "facets/file"
|
19
|
-
require "facets/filetest"
|
20
|
-
require "facets/float"
|
21
|
-
require "facets/functor"
|
22
|
-
require "facets/hash"
|
23
|
-
require "facets/indexable"
|
24
|
-
require "facets/integer"
|
25
|
-
require "facets/kernel"
|
26
|
-
require "facets/lazy"
|
27
|
-
require "facets/matchdata"
|
28
|
-
require "facets/module"
|
29
|
-
require "facets/nackclass"
|
30
|
-
require "facets/nilclass"
|
31
|
-
require "facets/nullclass"
|
32
|
-
require "facets/numeric"
|
33
|
-
require "facets/pathname"
|
34
|
-
require "facets/proc"
|
35
|
-
require "facets/range"
|
36
|
-
require "facets/regexp"
|
37
|
-
require "facets/stackable"
|
38
|
-
require "facets/string"
|
39
|
-
require "facets/symbol"
|
40
|
-
require "facets/time"
|
41
|
-
require "facets/unboundmethod"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Bring it on-line!
|
46
|
-
Facets.require_core
|
1
|
+
require 'facets/facets.rb'
|
data/lib/core/facets/array.rb
CHANGED
@@ -4,7 +4,10 @@ require 'facets/array/merge.rb'
|
|
4
4
|
require 'facets/array/select.rb'
|
5
5
|
require 'facets/array/splice.rb'
|
6
6
|
require 'facets/array/delete.rb'
|
7
|
+
require 'facets/array/only.rb'
|
7
8
|
require 'facets/array/pad.rb'
|
8
9
|
require 'facets/array/unzip.rb'
|
10
|
+
|
9
11
|
require 'facets/array/stackable.rb'
|
10
12
|
require 'facets/array/indexable.rb'
|
13
|
+
|
@@ -4,7 +4,12 @@ require 'facets/array/splice'
|
|
4
4
|
class Array
|
5
5
|
include Indexable
|
6
6
|
|
7
|
-
|
7
|
+
#--
|
8
|
+
# Maybe not appropriate for indexable.rb, but where?
|
9
|
+
#++
|
10
|
+
alias_method :contains?, :include?
|
11
|
+
|
12
|
+
#alias_method :/, :[]
|
8
13
|
|
9
14
|
# Alias for shift, which removes and returns
|
10
15
|
# the first element in an array.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Array
|
2
|
+
#
|
3
|
+
# Returns the _only_ element in the array. Raises an IndexError if
|
4
|
+
# the array's size is not 1.
|
5
|
+
#
|
6
|
+
# [5].only # -> 5
|
7
|
+
# [1,2,3].only # -> IndexError
|
8
|
+
# [].only # -> IndexError
|
9
|
+
#
|
10
|
+
# CREDIT Noah Gibbs
|
11
|
+
# CREDIT Gavin Sinclair
|
12
|
+
|
13
|
+
def only
|
14
|
+
unless size == 1
|
15
|
+
raise IndexError, "Array#only called on non-single-element array"
|
16
|
+
end
|
17
|
+
first
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -47,7 +47,7 @@ class Dir
|
|
47
47
|
# This is very useful in collecting files as specificed by a configuration
|
48
48
|
# parameter.
|
49
49
|
|
50
|
-
def self.multiglob(
|
50
|
+
def self.multiglob(*patterns)
|
51
51
|
options = (Hash === patterns.last ? patterns.pop : {})
|
52
52
|
|
53
53
|
bitflags = 0
|
@@ -58,6 +58,15 @@ class Dir
|
|
58
58
|
|
59
59
|
patterns = [patterns].flatten.compact
|
60
60
|
|
61
|
+
if options[:default]
|
62
|
+
default = [options[:default]].flatten.compact
|
63
|
+
#if patterns.empty?
|
64
|
+
# patterns = default
|
65
|
+
if patterns.first =~ /^[+-]/ || patterns.empty?
|
66
|
+
patterns = default + patterns
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
61
70
|
patterns_include = patterns.select{ |f| f !~ /^[-]/ }
|
62
71
|
patterns_exclude = patterns.select{ |f| f =~ /^[-]/ }
|
63
72
|
|
@@ -105,6 +114,8 @@ class Dir
|
|
105
114
|
#
|
106
115
|
# This is useful when a configuration option needs to supply a file list
|
107
116
|
# that may include files, exclude files or append files to a default list.
|
117
|
+
#
|
118
|
+
# TODO Deprecate this and replace with :default option on regular multiglob?
|
108
119
|
|
109
120
|
def self.multiglob_with_default(default, *patterns)
|
110
121
|
default = [default].flatten.compact
|
@@ -1,21 +1,14 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
#
|
4
|
-
# DESCRIPTION:
|
5
|
-
# Enumerble combination extensions.
|
6
|
-
#
|
7
|
-
# AUTHORS:
|
8
|
-
# - Florian Gross
|
9
|
-
# - Thomas Sawyer
|
1
|
+
# LOG:
|
2
|
+
# - 2007.11.01 Trans
|
3
|
+
# Deprecated #each_uniq_pair, just use #each_combination instead.
|
10
4
|
|
11
5
|
require 'facets/integer/factorial'
|
12
6
|
|
13
|
-
#
|
14
7
|
module Enumerable
|
15
8
|
|
16
9
|
# Produces an array of arrays of all possible combinations
|
17
10
|
# of the given arrays in the positions given. (Imagine
|
18
|
-
# it like a slot machine dial. This gives
|
11
|
+
# it like a slot machine dial. This gives every combination
|
19
12
|
# that could come up.)
|
20
13
|
#
|
21
14
|
# a = %w|a b|
|
@@ -34,6 +27,8 @@ module Enumerable
|
|
34
27
|
# ["b", "x", "x"]
|
35
28
|
# ["b", "x", "y"]
|
36
29
|
#
|
30
|
+
# CREDIT Florian Gross
|
31
|
+
|
37
32
|
def self.combinations(head, *rest)
|
38
33
|
crest = rest.empty? ? [[]] : combinations(*rest)
|
39
34
|
head.inject([]) { |combs, item|
|
@@ -55,6 +50,8 @@ module Enumerable
|
|
55
50
|
# ["a", "c", "d"]
|
56
51
|
# ["b", "c", "d"]
|
57
52
|
#
|
53
|
+
# CREDIT Florian Gross
|
54
|
+
|
58
55
|
def each_combination(k=2)
|
59
56
|
s = to_a
|
60
57
|
n = s.size
|
@@ -70,85 +67,42 @@ module Enumerable
|
|
70
67
|
end
|
71
68
|
end
|
72
69
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
#
|
102
|
-
# This is equivalent to <tt>each_combination(2){ ... }</tt>.
|
103
|
-
def each_unique_pair(&yld)
|
104
|
-
self.each_combination(2,&yld)
|
105
|
-
#s = self.to_a
|
106
|
-
#self.each_with_index{ |a,i|
|
107
|
-
# self[(i+1)..-1].each{ |b| yield a,b }
|
108
|
-
#}
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
# _____ _
|
116
|
-
# |_ _|__ ___| |_
|
117
|
-
# | |/ _ \/ __| __|
|
118
|
-
# | | __/\__ \ |_
|
119
|
-
# |_|\___||___/\__|
|
70
|
+
# # Processes each unique pair (of indices, not value)
|
71
|
+
# # in the array by yielding them to the supplied block.
|
72
|
+
# #
|
73
|
+
# # a = [1,2,3,4]
|
74
|
+
# # a.each_unique_pair{ |a,b| puts a+','+b }
|
75
|
+
# #
|
76
|
+
# # produces
|
77
|
+
# #
|
78
|
+
# # 1,2
|
79
|
+
# # 1,3
|
80
|
+
# # 1,4
|
81
|
+
# # 2,3
|
82
|
+
# # 2,4
|
83
|
+
# # 3,4
|
84
|
+
# #
|
85
|
+
# # This does not guarantee the uniqueness of values.
|
86
|
+
# # For example:
|
87
|
+
# #
|
88
|
+
# # a = [1,2,1]
|
89
|
+
# # a.each_unique_pair{ |a,b| puts a+','+b }
|
90
|
+
# #
|
91
|
+
# # prduces
|
92
|
+
# #
|
93
|
+
# # 1,2
|
94
|
+
# # 1,1
|
95
|
+
# # 2,1
|
96
|
+
# #
|
97
|
+
# # This is equivalent to <tt>each_combination(2){ ... }</tt>.
|
120
98
|
#
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
99
|
+
# def each_unique_pair(&yld)
|
100
|
+
# self.each_combination(2,&yld)
|
101
|
+
# #s = self.to_a
|
102
|
+
# #self.each_with_index{ |a,i|
|
103
|
+
# # self[(i+1)..-1].each{ |b| yield a,b }
|
104
|
+
# #}
|
105
|
+
# end
|
125
106
|
|
126
|
-
|
127
|
-
|
128
|
-
def test_combinations_01
|
129
|
-
a = [1,2]
|
130
|
-
b = [3,4]
|
131
|
-
z = Enumerable.combinations(a,b)
|
132
|
-
r = [[1,3],[1,4],[2,3],[2,4]]
|
133
|
-
assert_equal( r, z )
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_combinations_02
|
137
|
-
a = %w|a b|
|
138
|
-
b = %w|a x|
|
139
|
-
c = %w|x y|
|
140
|
-
z = Enumerable.combinations(a, b, c)
|
141
|
-
r = [ ["a", "a", "x"],
|
142
|
-
["a", "a", "y"],
|
143
|
-
["a", "x", "x"],
|
144
|
-
["a", "x", "y"],
|
145
|
-
["b", "a", "x"],
|
146
|
-
["b", "a", "y"],
|
147
|
-
["b", "x", "x"],
|
148
|
-
["b", "x", "y"] ]
|
149
|
-
assert_equal( r, z )
|
150
|
-
end
|
151
|
-
|
152
|
-
end
|
107
|
+
end
|
153
108
|
|
154
|
-
=end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Facets Module
|
2
|
+
|
3
|
+
module Facets
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def require_core
|
7
|
+
require "facets/array"
|
8
|
+
require "facets/basicobject"
|
9
|
+
require "facets/binding"
|
10
|
+
require "facets/boolean"
|
11
|
+
require "facets/class"
|
12
|
+
require "facets/comparable"
|
13
|
+
require "facets/continuation"
|
14
|
+
require "facets/conversion"
|
15
|
+
require "facets/curry"
|
16
|
+
require "facets/dir"
|
17
|
+
require "facets/enumerable"
|
18
|
+
require "facets/file"
|
19
|
+
require "facets/filetest"
|
20
|
+
require "facets/float"
|
21
|
+
require "facets/functor"
|
22
|
+
require "facets/hash"
|
23
|
+
require "facets/indexable"
|
24
|
+
require "facets/integer"
|
25
|
+
require "facets/kernel"
|
26
|
+
require "facets/lazy"
|
27
|
+
require "facets/matchdata"
|
28
|
+
require "facets/module"
|
29
|
+
require "facets/nackclass"
|
30
|
+
require "facets/nilclass"
|
31
|
+
require "facets/nullclass"
|
32
|
+
require "facets/numeric"
|
33
|
+
require "facets/pathname"
|
34
|
+
require "facets/proc"
|
35
|
+
require "facets/range"
|
36
|
+
require "facets/regexp"
|
37
|
+
require "facets/stackable"
|
38
|
+
require "facets/string"
|
39
|
+
require "facets/symbol"
|
40
|
+
require "facets/time"
|
41
|
+
require "facets/unboundmethod"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Bring it on-line!
|
46
|
+
Facets.require_core
|
@@ -12,6 +12,14 @@
|
|
12
12
|
# CREDIT Thomas Sawyer
|
13
13
|
# CREDIT George Moschovitis
|
14
14
|
# CREDIT Jeffrey Schwab
|
15
|
+
#
|
16
|
+
# LOG:
|
17
|
+
#
|
18
|
+
# - 2007.10.31 Trans
|
19
|
+
# Added 'b' option to all write methods. Is this okay for Windows?
|
20
|
+
#
|
21
|
+
# - 2007.10.31 Trans
|
22
|
+
# Added writelines method.
|
15
23
|
|
16
24
|
#
|
17
25
|
class File
|
@@ -19,7 +27,7 @@ class File
|
|
19
27
|
# Append to a file.
|
20
28
|
|
21
29
|
def self.append( file, str )
|
22
|
-
File.open( file, '
|
30
|
+
File.open( file, 'ab' ) { |f|
|
23
31
|
f << str
|
24
32
|
}
|
25
33
|
end
|
@@ -35,7 +43,7 @@ class File
|
|
35
43
|
# File.create('myfile.txt', str)
|
36
44
|
|
37
45
|
def self.create(path, str='', &blk)
|
38
|
-
File.open(path, '
|
46
|
+
File.open(path, 'wb') { |f|
|
39
47
|
f << str
|
40
48
|
blk.call(f) if blk
|
41
49
|
}
|
@@ -43,8 +51,42 @@ class File
|
|
43
51
|
|
44
52
|
# Write file with string.
|
45
53
|
|
46
|
-
def self.write(
|
47
|
-
File.open(
|
54
|
+
def self.write(path, data)
|
55
|
+
File.open(path, 'wb'){ |f| f.write(data) }
|
56
|
+
end
|
57
|
+
|
58
|
+
# # Writes the given data to the given path and closes the file. This is
|
59
|
+
# # done in binary mode, complementing <tt>IO.read</tt> in standard Ruby.
|
60
|
+
# #
|
61
|
+
# # Returns the number of bytes written.
|
62
|
+
# #
|
63
|
+
# # CREDIT Noah Gibbs
|
64
|
+
# # CREDIT Gavin Sinclair
|
65
|
+
#
|
66
|
+
# def self.write(path, data)
|
67
|
+
# File.open(path, "wb") do |file|
|
68
|
+
# return file.write(data)
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
|
72
|
+
# Writes the given array of data to the given path and closes the file.
|
73
|
+
# This is done in binary mode, complementing <tt>IO.readlines</tt> in
|
74
|
+
# standard Ruby.
|
75
|
+
#
|
76
|
+
# Note that +readlines+ (the standard Ruby method) returns an array of lines
|
77
|
+
# <em>with newlines intact</em>, whereas +writelines+ uses +puts+, and so
|
78
|
+
# appends newlines if necessary. In this small way, +readlines+ and
|
79
|
+
# +writelines+ are not exact opposites.
|
80
|
+
#
|
81
|
+
# Returns +nil+.
|
82
|
+
#
|
83
|
+
# CREDIT Noah Gibbs
|
84
|
+
# CREDIT Gavin Sinclair
|
85
|
+
|
86
|
+
def self.writelines(path, data)
|
87
|
+
File.open(path, "wb") do |file|
|
88
|
+
file.puts(data)
|
89
|
+
end
|
48
90
|
end
|
49
91
|
|
50
92
|
# Opens a file as a string and writes back the string to the file at
|
@@ -97,57 +139,3 @@ class File
|
|
97
139
|
end
|
98
140
|
|
99
141
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
# _____ _
|
104
|
-
# |_ _|__ ___| |_
|
105
|
-
# | |/ _ \/ __| __|
|
106
|
-
# | | __/\__ \ |_
|
107
|
-
# |_|\___||___/\__|
|
108
|
-
#
|
109
|
-
# TODO This isn't right, and I'm concerned about acidentally writing a real file.
|
110
|
-
|
111
|
-
=begin #test
|
112
|
-
|
113
|
-
require 'test/unit'
|
114
|
-
|
115
|
-
class TestFileWrite < Test::Unit::TestCase
|
116
|
-
|
117
|
-
class MockFile < ::File
|
118
|
-
def open( fname, mode, &blk )
|
119
|
-
blk.call(self)
|
120
|
-
end
|
121
|
-
def ead( fname=nil )
|
122
|
-
@mrock_content.clone
|
123
|
-
end
|
124
|
-
def write( str )
|
125
|
-
@mock_content = str
|
126
|
-
end
|
127
|
-
def <<( str )
|
128
|
-
(@mock_content ||="") << str
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
File = MockFile.new
|
133
|
-
|
134
|
-
def test_create
|
135
|
-
f = "not a real file"
|
136
|
-
t = 'This is a test'
|
137
|
-
File.create( f, t )
|
138
|
-
s = File.read( f )
|
139
|
-
assert_equal( t, s )
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_rewrite
|
143
|
-
f = "not a real file"
|
144
|
-
t = 'This is a test'
|
145
|
-
File.write( t )
|
146
|
-
File.rewrite(f) { |s| s.reverse! }
|
147
|
-
s = File.read(f)
|
148
|
-
assert_equal( t.reverse, s )
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
=end
|