extensions 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +18 -0
- data/README +44 -28
- data/VERSION +1 -1
- data/lib/extensions/all.rb +2 -0
- data/lib/extensions/array.rb +44 -0
- data/lib/extensions/enumerable.rb +67 -0
- data/lib/extensions/kernel.rb +42 -0
- data/lib/extensions/module.rb +114 -0
- data/lib/extensions/numeric.rb +26 -0
- data/lib/extensions/string.rb +1 -1
- data/test/data/kernel_test/global_var_1.rb +1 -0
- data/test/data/kernel_test/global_var_2.rb +1 -0
- data/test/tc_array.rb +20 -0
- data/test/tc_enumerable.rb +32 -0
- data/test/tc_kernel.rb +39 -0
- data/test/tc_module.rb +43 -0
- metadata +12 -3
- data/InstalledFiles +0 -50
data/ChangeLog
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
2004-12-09 Gavin Sinclair <gsinclair@soyabean.com.au>
|
2
|
+
|
3
|
+
* RELEASE: 0.6.0 ===================================================
|
4
|
+
* lib/extensions/array.rb: Implemented Array#only and #rand.
|
5
|
+
* test/tc_array.rb: Test Array#only and #rand.
|
6
|
+
* lib/extensions/module.rb: [NEW] Implemented Module.by_name,
|
7
|
+
Module#basename, and Module#deep_const_get.
|
8
|
+
* test/tc_module.rb: [NEW] Test the aforementioned Module methods.
|
9
|
+
* lib/extensions/enumerable.rb: Implemented Enumerable#none? and #one?
|
10
|
+
* test/tc_enumerable.rb: Test aforementioned Enumerable methods.
|
11
|
+
* lib/extensions/kernel.rb: Implemented Kernel#relative_require.
|
12
|
+
* test/tc_kernel.rb: Test Kernel#relative_require.
|
13
|
+
* lib/extensions/numeric.rb: Added comment for possible future method.
|
14
|
+
* lib/extensions/all.rb: Included new files module.rb and kernel.rb.
|
15
|
+
* test/data/kernel_test/global_var_1.rb: [NEW] test data file.
|
16
|
+
* test/data/kernel_test/global_var_2.rb: [NEW] test data file.
|
17
|
+
* README: Updated to describe release of 0.6.
|
18
|
+
|
1
19
|
2004-10-04 Gavin Sinclair <gsinclair@soyabean.com.au>
|
2
20
|
|
3
21
|
* RELEASE: 0.5.0 ===================================================
|
data/README
CHANGED
@@ -32,18 +32,20 @@ hoping that they don't conflict with any code your users have written.
|
|
32
32
|
|
33
33
|
Long story short: it's useful that Ruby allows you to add methods to existing
|
34
34
|
classes, even built-ins like String and Array. Some people rightfully are
|
35
|
-
uneasy about actually doing so, because of possible
|
36
|
-
offers a standard set of extensions in order to
|
37
|
-
making them publicly available, and well
|
35
|
+
uneasy about actually doing so in some situations, because of possible
|
36
|
+
conflicts. This project offers a standard set of extensions in order to
|
37
|
+
mitigate that uneasiness by making them publicly available, and well
|
38
|
+
documented.
|
38
39
|
|
39
40
|
Thanks to the kind folk at InfoEther, Rich Kilmer and Tom Copeland, for
|
40
|
-
providing the means and encouragement to host projects like
|
41
|
+
providing the means and encouragement to host projects like this at
|
41
42
|
http://www.rubyforge.org.
|
42
43
|
|
43
44
|
|
44
45
|
== Sample Code
|
45
46
|
|
46
47
|
require 'extensions/all'
|
48
|
+
require_relative 'data/samples'
|
47
49
|
|
48
50
|
str = "Hello, \n\n world!"
|
49
51
|
str.define_method(:heading) {
|
@@ -79,7 +81,15 @@ http://www.rubyforge.org.
|
|
79
81
|
STR = "Hello, world!"
|
80
82
|
STR.starts_with? "Hello" # -> true
|
81
83
|
STR.ends_with? "world" # -> false
|
82
|
-
File.write("hello.txt",
|
84
|
+
File.write("hello.txt", STR)
|
85
|
+
|
86
|
+
arr = [1, 3, 5]
|
87
|
+
arr.rand # -> 1, 3, or 5
|
88
|
+
arr.none? { |n| n.even? } # -> true
|
89
|
+
arr.one? { |n| n > 4 } # -> true
|
90
|
+
|
91
|
+
Class.by_name "Process::Sys" # -> Process::Sys
|
92
|
+
Process::Sys.basename # -> "Sys"
|
83
93
|
|
84
94
|
|
85
95
|
== Links
|
@@ -91,16 +101,12 @@ Wiki Page:: http://www.rubygarden.org/ruby?StandardClassExtensions
|
|
91
101
|
|
92
102
|
== Current Version and Status
|
93
103
|
|
94
|
-
Version 0.
|
104
|
+
Version 0.6 was released on 2004-12-08 and can be installed as a gem,
|
95
105
|
installed via the RPA, or downloaded from the project page. Anybody is
|
96
106
|
welcome to access the CVS and see changes as they happen. As always,
|
97
107
|
everything included is unit tested and well documented.
|
98
108
|
|
99
|
-
|
100
|
-
the quick update is the addition of the important method
|
101
|
-
<tt>Binding.of_caller</tt>, on which the 'dev-utils' package depends.
|
102
|
-
|
103
|
-
Several methods have been added since version 0.3. <tt>ChangeLog</tt> has all
|
109
|
+
Several methods have been added since version 0.5. <tt>ChangeLog</tt> has all
|
104
110
|
the details, and the changes are highlighted below.
|
105
111
|
|
106
112
|
Version 1.0 will (may?) occur when sufficient time has passed to make me
|
@@ -176,17 +182,19 @@ manually download it.
|
|
176
182
|
When you install this package, you can run the command +rbxtm+ ("Ruby
|
177
183
|
Extension Methods") to get a list of the methods implemented by this project.
|
178
184
|
At time of writing, this command gives (<tt>+</tt> indicates new method since
|
179
|
-
0.
|
180
|
-
|
181
|
-
+ Array#
|
182
|
-
+
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
185
|
+
0.5).
|
186
|
+
|
187
|
+
+ Array#only
|
188
|
+
+ Array#rand
|
189
|
+
Array#select!
|
190
|
+
Binding#[]
|
191
|
+
Binding#[]=
|
192
|
+
Binding#defined?
|
193
|
+
Binding#eval
|
194
|
+
Binding#local_variables
|
195
|
+
Binding.of_caller
|
188
196
|
Class#autoinit
|
189
|
-
|
197
|
+
Continuation.create
|
190
198
|
Enumerable#build_hash
|
191
199
|
Enumerable#collect_with_index
|
192
200
|
Enumerable#collectf
|
@@ -195,25 +203,31 @@ At time of writing, this command gives (<tt>+</tt> indicates new method since
|
|
195
203
|
Enumerable#includes?
|
196
204
|
Enumerable#map_with_index
|
197
205
|
Enumerable#mapf
|
206
|
+
+ Enumerable#none?
|
207
|
+
+ Enumerable#one?
|
198
208
|
Enumerable#partition_by
|
199
209
|
Hash#select!
|
200
210
|
IO.write
|
201
211
|
IO.writelines
|
202
212
|
Integer#even?
|
203
213
|
Integer#odd?
|
214
|
+
+ Kernel#require_relative
|
215
|
+
+ Module.by_name
|
216
|
+
+ Module#basename
|
217
|
+
+ Module#deep_const_get
|
204
218
|
Numeric#format_s
|
205
|
-
|
219
|
+
Object#define_method
|
206
220
|
Object#in?
|
207
|
-
|
208
|
-
|
221
|
+
Object#non_nil?
|
222
|
+
Object#not_nil?
|
209
223
|
Object#pp_s
|
210
224
|
Object#singleton_class
|
211
|
-
|
225
|
+
OpenStruct.new
|
212
226
|
String#cmp
|
213
227
|
String#ends_with?
|
214
228
|
String#expand_tabs
|
215
229
|
String#indent
|
216
|
-
|
230
|
+
String#join
|
217
231
|
String#leftmost_indent
|
218
232
|
String#line
|
219
233
|
String#outdent
|
@@ -225,12 +239,14 @@ At time of writing, this command gives (<tt>+</tt> indicates new method since
|
|
225
239
|
|
226
240
|
The files that you can load are:
|
227
241
|
|
228
|
-
extensions/all
|
242
|
+
extensions/all (loads all the others)
|
229
243
|
extensions/binding
|
230
244
|
extensions/class
|
231
245
|
extensions/continuation
|
232
246
|
extensions/enumerable
|
233
247
|
extensions/io
|
248
|
+
extensions/kernel
|
249
|
+
extensions/module
|
234
250
|
extensions/numeric
|
235
251
|
extensions/object
|
236
252
|
extensions/ostruct
|
@@ -347,6 +363,6 @@ Standard disclaimer:
|
|
347
363
|
------
|
348
364
|
|
349
365
|
<i>
|
350
|
-
$Id: README,v 1.
|
366
|
+
$Id: README,v 1.14 2004/12/08 13:39:09 gsinclair Exp $
|
351
367
|
vim: et sw=2 ts=2 sts=2 ai
|
352
368
|
</i>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/extensions/all.rb
CHANGED
@@ -12,6 +12,8 @@ require 'extensions/continuation.rb'
|
|
12
12
|
require 'extensions/enumerable.rb'
|
13
13
|
require 'extensions/hash.rb'
|
14
14
|
require 'extensions/io.rb'
|
15
|
+
require 'extensions/kernel.rb'
|
16
|
+
require 'extensions/module.rb'
|
15
17
|
require 'extensions/numeric.rb'
|
16
18
|
require 'extensions/object.rb'
|
17
19
|
require 'extensions/ostruct.rb'
|
data/lib/extensions/array.rb
CHANGED
@@ -22,3 +22,47 @@ ExtensionsProject.implement(Array, :select!) do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
|
26
|
+
#
|
27
|
+
# * Array#only
|
28
|
+
#
|
29
|
+
ExtensionsProject.implement(Array, :only) do
|
30
|
+
class Array
|
31
|
+
#
|
32
|
+
# Returns the _only_ element in the array. Raises an IndexError if the array's size is not
|
33
|
+
# 1.
|
34
|
+
#
|
35
|
+
# [5].only # -> 5
|
36
|
+
# [1,2,3].only # -> IndexError
|
37
|
+
# [].only # -> IndexError
|
38
|
+
#
|
39
|
+
def only
|
40
|
+
unless size == 1
|
41
|
+
raise IndexError, "Array#only called on non-single-element array"
|
42
|
+
end
|
43
|
+
first
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# * Array#rand
|
50
|
+
#
|
51
|
+
ExtensionsProject.implement(Array, :rand) do
|
52
|
+
class Array
|
53
|
+
#
|
54
|
+
# Return a randomly-chosen (using Kernel.rand) element from the array.
|
55
|
+
#
|
56
|
+
# arr = [48, 71, 3, 39, 15]
|
57
|
+
# arr.rand # -> 71
|
58
|
+
# arr.rand # -> 39
|
59
|
+
# arr.rand # -> 48
|
60
|
+
# # etc.
|
61
|
+
#
|
62
|
+
def rand
|
63
|
+
idx = Kernel.rand(size)
|
64
|
+
at(idx)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -166,6 +166,73 @@ ExtensionsProject.implement(Enumerable, :partition_by) do
|
|
166
166
|
end
|
167
167
|
|
168
168
|
|
169
|
+
#
|
170
|
+
# * Enumerable#none?
|
171
|
+
#
|
172
|
+
ExtensionsProject.implement(Enumerable, :none?) do
|
173
|
+
module Enumerable
|
174
|
+
#
|
175
|
+
# Enumerable#none? is the logical opposite of the builtin method Enumerable#any?. It
|
176
|
+
# returns +true+ if and only if _none_ of the elements in the collection satisfy the
|
177
|
+
# predicate.
|
178
|
+
#
|
179
|
+
# If no predicate is provided, Enumerable#none? returns +true+ if and only if _none_ of the
|
180
|
+
# elements have a true value (i.e. not +nil+ or +false+).
|
181
|
+
#
|
182
|
+
# [].none? # true
|
183
|
+
# [nil].none? # true
|
184
|
+
# [5,8,9].none? # false
|
185
|
+
# (1...10).none? { |n| n < 0 } # true
|
186
|
+
# (1...10).none? { |n| n > 0 } # false
|
187
|
+
#
|
188
|
+
def none? # :yield: e
|
189
|
+
if block_given?
|
190
|
+
not self.any? { |e| yield e }
|
191
|
+
else
|
192
|
+
not self.any?
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
|
199
|
+
#
|
200
|
+
# * Enumerable#one?
|
201
|
+
#
|
202
|
+
ExtensionsProject.implement(Enumerable, :one?) do
|
203
|
+
module Enumerable
|
204
|
+
#
|
205
|
+
# Enumerable#one? returns +true+ if and only if <em>exactly one</em> element in the
|
206
|
+
# collection satisfies the given predicate.
|
207
|
+
#
|
208
|
+
# If no predicate is provided, Enumerable#one? returns +true+ if and only if <em>exactly
|
209
|
+
# one</em> element has a true value (i.e. not +nil+ or +false+).
|
210
|
+
#
|
211
|
+
# [].one? # false
|
212
|
+
# [nil].one? # false
|
213
|
+
# [5].one? # true
|
214
|
+
# [5,8,9].one? # false
|
215
|
+
# (1...10).one? { |n| n == 5 } # true
|
216
|
+
# (1...10).one? { |n| n < 5 } # false
|
217
|
+
#
|
218
|
+
def one? # :yield: e
|
219
|
+
matches = 0
|
220
|
+
if block_given?
|
221
|
+
self.each do |e|
|
222
|
+
if yield(e)
|
223
|
+
matches += 1
|
224
|
+
return false if matches > 1
|
225
|
+
end
|
226
|
+
end
|
227
|
+
return (matches == 1)
|
228
|
+
else
|
229
|
+
one? { |e| e }
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
|
169
236
|
#
|
170
237
|
# * Object.in?
|
171
238
|
# This has special treatment: it's included here and in object.rb, so we don't
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
#
|
3
|
+
# == extensions/module.rb
|
4
|
+
#
|
5
|
+
# Adds methods to the builtin Kernel module.
|
6
|
+
#
|
7
|
+
|
8
|
+
require "extensions/_base"
|
9
|
+
|
10
|
+
ExtensionsProject.implement(Kernel, :require_relative) do
|
11
|
+
module Kernel
|
12
|
+
#
|
13
|
+
# <tt>require_relative</tt> complements the builtin method <tt>require</tt> by allowing
|
14
|
+
# you to load a file that is <em>relative to the file containing the require_relative
|
15
|
+
# statement</em>.
|
16
|
+
#
|
17
|
+
# When you use <tt>require</tt> to load a file, you are usually accessing functionality
|
18
|
+
# that has been properly installed, and made accessible, in your system. <tt>require</tt>
|
19
|
+
# does not offer a good solution for loading files within the project's code. This may
|
20
|
+
# be useful during a development phase, for accessing test data, or even for accessing
|
21
|
+
# files that are "locked" away inside a project, not intended for outside use.
|
22
|
+
#
|
23
|
+
# For example, if you have unit test classes in the "test" directory, and data for them
|
24
|
+
# under the test "test/data" directory, then you might use a line like this in a test
|
25
|
+
# case:
|
26
|
+
#
|
27
|
+
# require_relative "data/customer_data_1"
|
28
|
+
#
|
29
|
+
# Since neither "test" nor "test/data" are likely to be in Ruby's library path (and for
|
30
|
+
# good reason), a normal <tt>require</tt> won't find them. <tt>require_relative</tt> is
|
31
|
+
# a good solution for this particular problem.
|
32
|
+
#
|
33
|
+
# You may include or omit the extension (<tt>.rb</tt> or <tt>.so</tt>) of the file you
|
34
|
+
# are loading.
|
35
|
+
#
|
36
|
+
# _path_ must respond to <tt>to_str</tt>.
|
37
|
+
#
|
38
|
+
def require_relative(path)
|
39
|
+
require File.join(File.dirname(caller[0]), path.to_str)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
#
|
3
|
+
# == extensions/module.rb
|
4
|
+
#
|
5
|
+
# Adds methods to the builtin Module class.
|
6
|
+
#
|
7
|
+
|
8
|
+
require "extensions/_base"
|
9
|
+
|
10
|
+
ExtensionsProject.implement(Module, :deep_const_get) do
|
11
|
+
class Module
|
12
|
+
#
|
13
|
+
# Recursively dereferences constants separated by "<tt>::</tt>".
|
14
|
+
#
|
15
|
+
# _const_ is a Symbol or responds to #to_str, for compatibility with the builtin method
|
16
|
+
# <tt>Module#const_get</tt>.
|
17
|
+
#
|
18
|
+
# Object.const_get("String") # -> String
|
19
|
+
# Object.const_get(:String) # -> String
|
20
|
+
#
|
21
|
+
# Object.deep_const_get("String") # -> String
|
22
|
+
# Object.deep_const_get(:String) # -> String
|
23
|
+
#
|
24
|
+
# Object.deep_const_get("Process::Sys") # -> Process::Sys
|
25
|
+
# Object.deep_const_get("Regexp::IGNORECASE") # -> 1
|
26
|
+
# Object.deep_const_get("Regexp::MULTILINE") # -> 4
|
27
|
+
#
|
28
|
+
# require 'test/unit'
|
29
|
+
# Test.deep_const_get("Unit::Assertions") # -> Test::Unit::Assertions
|
30
|
+
# Test.deep_const_get("::Test::Unit") # -> Test::Unit
|
31
|
+
#
|
32
|
+
# For resolving classes or modules based on their name, see Module.by_name, which uses this
|
33
|
+
# method.
|
34
|
+
#
|
35
|
+
def deep_const_get(const)
|
36
|
+
if Symbol === const
|
37
|
+
const = const.to_s
|
38
|
+
else
|
39
|
+
const = const.to_str.dup
|
40
|
+
end
|
41
|
+
if const.sub!(/^::/, '')
|
42
|
+
base = Object
|
43
|
+
else
|
44
|
+
base = self
|
45
|
+
end
|
46
|
+
const.split(/::/).inject(base) { |mod, name| mod.const_get(name) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
ExtensionsProject.implement(Module, :by_name, :class) do
|
53
|
+
class Module
|
54
|
+
#
|
55
|
+
# <em>Note: the following documentation uses "class" because it's more common, but it
|
56
|
+
# applies to modules as well.</em>
|
57
|
+
#
|
58
|
+
# Given the _name_ of a class, returns the class itself (i.e. instance of Class). The
|
59
|
+
# dereferencing starts at Object. That is,
|
60
|
+
#
|
61
|
+
# Class.by_name("String")
|
62
|
+
#
|
63
|
+
# is equivalent to
|
64
|
+
#
|
65
|
+
# Object.get_const("String")
|
66
|
+
#
|
67
|
+
# The parameter _name_ is expected to be a Symbol or String, or at least to respond to
|
68
|
+
# <tt>to_str</tt>.
|
69
|
+
#
|
70
|
+
# An ArgumentError is raised if _name_ does not correspond to an existing class. If _name_
|
71
|
+
# is not even a valid class name, the error you'll get is not defined.
|
72
|
+
#
|
73
|
+
# Examples:
|
74
|
+
#
|
75
|
+
# Class.by_name("String") # -> String
|
76
|
+
# Class.by_name("::String") # -> String
|
77
|
+
# Class.by_name("Process::Sys") # -> Process::Sys
|
78
|
+
# Class.by_name("GorillaZ") # -> (ArgumentError)
|
79
|
+
#
|
80
|
+
# Class.by_name("Enumerable") # -> Enumerable
|
81
|
+
# Module.by_name("Enumerable") # -> Enumerable
|
82
|
+
#
|
83
|
+
def Module.by_name(name)
|
84
|
+
if Symbol === name
|
85
|
+
name = name.to_s
|
86
|
+
else
|
87
|
+
name = name.to_str
|
88
|
+
end
|
89
|
+
result = Object.deep_const_get(name)
|
90
|
+
if result.is_a? Module
|
91
|
+
return result
|
92
|
+
else
|
93
|
+
raise ArgumentError, "#{name} is not a class or module"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
ExtensionsProject.implement(Module, :basename) do
|
101
|
+
class Module
|
102
|
+
#
|
103
|
+
# Returns the immediate name of the class/module, stripped of any containing classes or
|
104
|
+
# modules. Compare Ruby's builtin methods <tt>Module#name</tt> and <tt>File.basename</tt>.
|
105
|
+
#
|
106
|
+
# Process::Sys.name # -> "Process::Sys"
|
107
|
+
# Process::Sys.basename # -> "Sys"
|
108
|
+
# String.basename # -> "String"
|
109
|
+
#
|
110
|
+
def basename
|
111
|
+
self.name.sub(/^.*::/, '')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/lib/extensions/numeric.rb
CHANGED
@@ -40,6 +40,32 @@ ExtensionsProject.implement(Integer, :odd?) do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
#
|
44
|
+
# This code arose from discussions with Francis Hwang. Leaving it here for future work.
|
45
|
+
#
|
46
|
+
# class Numeric
|
47
|
+
# def precision_format(nplaces, flag = :pad)
|
48
|
+
# format = "%.#{nplaces}f"
|
49
|
+
# result = sprintf(format, self)
|
50
|
+
# case flag
|
51
|
+
# when :pad
|
52
|
+
# when :nopad
|
53
|
+
# result.sub!(/\.?0*$/, '')
|
54
|
+
# else
|
55
|
+
# raise ArgumentError, "Invalid value for flag: #{flag.inspect}"
|
56
|
+
# end
|
57
|
+
# result
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# 100.precision_format(2) # -> "100.00"
|
62
|
+
# 100.precision_format(2, :nopad) # -> "100"
|
63
|
+
# 100.1.precision_format(2) # -> "100.10"
|
64
|
+
# 100.1.precision_format(2, :nopad) # -> "100.1"
|
65
|
+
# 100.1.precision_format(2, false)
|
66
|
+
# # -> "ArgumentError: Invalid value for flag: false"
|
67
|
+
#
|
68
|
+
|
43
69
|
|
44
70
|
ExtensionsProject.implement(Numeric, :format_s) do
|
45
71
|
#--
|
data/lib/extensions/string.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
$kernel_require_relative_test_variable_1 = 'done'
|
@@ -0,0 +1 @@
|
|
1
|
+
$kernel_require_relative_test_variable_2 = 'done'
|
data/test/tc_array.rb
CHANGED
@@ -23,5 +23,25 @@ class TC_Array < Test::Unit::TestCase
|
|
23
23
|
assert_equal([], a.select! {false}, "select none")
|
24
24
|
assert_equal([], a, "select none")
|
25
25
|
end
|
26
|
+
|
27
|
+
def test_only
|
28
|
+
assert_equal(5, [5].only)
|
29
|
+
assert_equal(nil, [nil].only)
|
30
|
+
assert_raise(IndexError) { [].only }
|
31
|
+
assert_raise(IndexError) { [1,2,3].only }
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_rand
|
35
|
+
array = [1,2,3]
|
36
|
+
seen = Hash.new { |hash, key| hash[key] = 0 }
|
37
|
+
1000.times do
|
38
|
+
n = array.rand
|
39
|
+
seen[n] += 1
|
40
|
+
end
|
41
|
+
assert_equal([1,2,3], seen.keys.sort)
|
42
|
+
assert(seen[1] > 100)
|
43
|
+
assert(seen[2] > 100)
|
44
|
+
assert(seen[3] > 100)
|
45
|
+
end
|
26
46
|
end # class TC_Array
|
27
47
|
|
data/test/tc_enumerable.rb
CHANGED
@@ -83,5 +83,37 @@ class TC_Enumerable < Test::Unit::TestCase
|
|
83
83
|
|
84
84
|
# Object#in? is tested in tc_object.rb.
|
85
85
|
|
86
|
+
def test_none?
|
87
|
+
enum = (5..15)
|
88
|
+
assert_equal(true, [].none?)
|
89
|
+
assert_equal(true, [nil, nil, nil].none? )
|
90
|
+
assert_equal(false, enum.none?)
|
91
|
+
assert_equal(false, enum.none? { |n| n < 10 } )
|
92
|
+
assert_equal(true, enum.none? { |n| n < 0 } )
|
93
|
+
|
94
|
+
# Taken from RDoc documentation.
|
95
|
+
assert_equal(true, [].none?)
|
96
|
+
assert_equal(true, [nil].none?)
|
97
|
+
assert_equal(false, [5,8,9].none?)
|
98
|
+
assert_equal(true, (1...10).none? { |n| n < 0 })
|
99
|
+
assert_equal(false, (1...10).none? { |n| n > 0 })
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_one?
|
103
|
+
enum = (5..15)
|
104
|
+
assert_equal(true, [nil, 5, nil].one?)
|
105
|
+
assert_equal(false, enum.one?)
|
106
|
+
assert_equal(false, enum.one? { |n| n < 10 } )
|
107
|
+
assert_equal(true, enum.one? { |n| n == 9 } )
|
108
|
+
|
109
|
+
# Taken from RDoc documentation.
|
110
|
+
assert_equal(false, [].one?)
|
111
|
+
assert_equal(false, [nil].one?)
|
112
|
+
assert_equal(true, [5].one?)
|
113
|
+
assert_equal(false, [5,8,9].one?)
|
114
|
+
assert_equal(true, (1...10).one? { |n| n == 5 } )
|
115
|
+
assert_equal(false, (1...10).one? { |n| n < 5 } )
|
116
|
+
end
|
117
|
+
|
86
118
|
end # class TC_Enumerable
|
87
119
|
|
data/test/tc_kernel.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
require 'test/unit'
|
3
|
+
require 'extensions/kernel'
|
4
|
+
|
5
|
+
class TC_Kernel < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# We test this method by loading a file relative to the test directory (that's the idea,
|
8
|
+
# after all), and confirming that a global variable defined therein has the expected value.
|
9
|
+
# We check first that the variable is nil.
|
10
|
+
def test_require_relative_1
|
11
|
+
assert_nil($kernel_require_relative_test_variable_1)
|
12
|
+
require_relative 'data/kernel_test/global_var_1'
|
13
|
+
assert_equal('done', $kernel_require_relative_test_variable_1)
|
14
|
+
end
|
15
|
+
|
16
|
+
# This is to see what happens when we give the .rb extension. It should work.
|
17
|
+
def test_require_relative_2
|
18
|
+
assert_nil($kernel_require_relative_test_variable_2)
|
19
|
+
require_relative 'data/kernel_test/global_var_2.rb'
|
20
|
+
assert_equal('done', $kernel_require_relative_test_variable_2)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Ensuring we get a LoadError when the file not found. We want this method to behave like
|
24
|
+
# <tt>require</tt> as much as possible.
|
25
|
+
def test_require_relative_3
|
26
|
+
assert_raise(LoadError) do
|
27
|
+
require_relative 'data/kernel_test/non-existent-file'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# If we pass a non-string-like object to require_relative, we should get a NoMethodError.
|
32
|
+
def test_require_relative_4
|
33
|
+
assert_raise(NoMethodError) do
|
34
|
+
require_relative 123.456
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
data/test/tc_module.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
require 'test/unit'
|
3
|
+
require 'extensions/module'
|
4
|
+
|
5
|
+
class TC_Module < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_deep_const_get
|
8
|
+
assert_equal(String, Object.const_get("String"))
|
9
|
+
assert_equal(String, Object.const_get(:String))
|
10
|
+
assert_equal(String, Object.deep_const_get("String"))
|
11
|
+
assert_equal(String, Object.deep_const_get(:String))
|
12
|
+
assert_equal(Process::Sys, Object.deep_const_get("Process::Sys"))
|
13
|
+
assert_equal(Regexp::IGNORECASE, Object.deep_const_get("Regexp::IGNORECASE"))
|
14
|
+
assert_equal(Regexp::MULTILINE, Object.deep_const_get("Regexp::MULTILINE"))
|
15
|
+
assert_equal(Test::Unit::Assertions, Test.deep_const_get("Unit::Assertions"))
|
16
|
+
assert_equal(Test::Unit, Test.deep_const_get("::Test::Unit"))
|
17
|
+
assert_equal(Regexp::MULTILINE, Test.deep_const_get("::Regexp::MULTILINE"))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_class_by_name
|
21
|
+
assert_equal(Test::Unit, Class.by_name("Test::Unit"))
|
22
|
+
assert_equal(Test::Unit, Class.by_name("::Test::Unit"))
|
23
|
+
assert_equal(Test::Unit::Assertions, Class.by_name("Test::Unit::Assertions"))
|
24
|
+
assert_equal(Test::Unit::Assertions, Class.by_name("::Test::Unit::Assertions"))
|
25
|
+
assert_equal(String, Class.by_name("String"))
|
26
|
+
assert_equal(String, Class.by_name("::String"))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_class_by_name_bad_input
|
30
|
+
assert_raise(ArgumentError) { Class.by_name("Regexp::IGNORECASE") }
|
31
|
+
assert_raise(NameError) { Class.by_name("not-a-class") }
|
32
|
+
assert_raise(NoMethodError) { Class.by_name(1) } # We call to_str on argument.
|
33
|
+
assert_raise(NameError) { Class.by_name("StrauPlunkt123") }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_basename
|
37
|
+
assert_equal("String", String.basename)
|
38
|
+
assert_equal("Unit", Test::Unit.basename)
|
39
|
+
assert_equal("Assertions", Test::Unit::Assertions.basename)
|
40
|
+
end
|
41
|
+
|
42
|
+
end # class TC_Class
|
43
|
+
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
|
|
3
3
|
specification_version: 1
|
4
4
|
name: extensions
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2004-
|
6
|
+
version: 0.6.0
|
7
|
+
date: 2004-12-09
|
8
8
|
summary: "'extensions' is a set of extensions to Ruby's built-in classes. It gathers common idioms, useful additions, and aliases, complete with unit testing and documentation, so they are suitable for production code."
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -28,7 +28,6 @@ platform: ruby
|
|
28
28
|
files:
|
29
29
|
- ChangeLog
|
30
30
|
- HISTORY
|
31
|
-
- InstalledFiles
|
32
31
|
- Rakefile
|
33
32
|
- README
|
34
33
|
- README.1st
|
@@ -46,6 +45,8 @@ files:
|
|
46
45
|
- lib/extensions/enumerable.rb
|
47
46
|
- lib/extensions/hash.rb
|
48
47
|
- lib/extensions/io.rb
|
48
|
+
- lib/extensions/kernel.rb
|
49
|
+
- lib/extensions/module.rb
|
49
50
|
- lib/extensions/numeric.rb
|
50
51
|
- lib/extensions/object.rb
|
51
52
|
- lib/extensions/ostruct.rb
|
@@ -53,6 +54,7 @@ files:
|
|
53
54
|
- lib/extensions/symbol.rb
|
54
55
|
- lib/extensions/_base.rb
|
55
56
|
- lib/extensions/_template.rb
|
57
|
+
- "./test/data"
|
56
58
|
- "./test/tc_array.rb"
|
57
59
|
- "./test/tc_binding.rb"
|
58
60
|
- "./test/tc_class.rb"
|
@@ -60,12 +62,17 @@ files:
|
|
60
62
|
- "./test/tc_enumerable.rb"
|
61
63
|
- "./test/tc_hash.rb"
|
62
64
|
- "./test/tc_io.rb"
|
65
|
+
- "./test/tc_kernel.rb"
|
66
|
+
- "./test/tc_module.rb"
|
63
67
|
- "./test/tc_numeric.rb"
|
64
68
|
- "./test/tc_object.rb"
|
65
69
|
- "./test/tc_ostruct.rb"
|
66
70
|
- "./test/tc_string.rb"
|
67
71
|
- "./test/tc_symbol.rb"
|
68
72
|
- "./test/TEST.rb"
|
73
|
+
- "./test/data/kernel_test"
|
74
|
+
- "./test/data/kernel_test/global_var_1.rb"
|
75
|
+
- "./test/data/kernel_test/global_var_2.rb"
|
69
76
|
- etc/checklist
|
70
77
|
- etc/website
|
71
78
|
- etc/website/index.html
|
@@ -78,6 +85,8 @@ test_files:
|
|
78
85
|
- test/tc_enumerable.rb
|
79
86
|
- test/tc_hash.rb
|
80
87
|
- test/tc_io.rb
|
88
|
+
- test/tc_kernel.rb
|
89
|
+
- test/tc_module.rb
|
81
90
|
- test/tc_numeric.rb
|
82
91
|
- test/tc_object.rb
|
83
92
|
- test/tc_ostruct.rb
|
data/InstalledFiles
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/all.rb
|
2
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/class.rb
|
3
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/enumerable.rb
|
4
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/io.rb
|
5
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/numeric.rb
|
6
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/object.rb
|
7
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/string.rb
|
8
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/symbol.rb
|
9
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_base.rb
|
10
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_template.rb
|
11
|
-
/usr/local/bin//rbxtm
|
12
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/all.rb
|
13
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/array.rb
|
14
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/hash.rb
|
15
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/io.rb
|
16
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/object.rb
|
17
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/ostruct.rb
|
18
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/string.rb
|
19
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_base.rb
|
20
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_template.rb
|
21
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/all.rb
|
22
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/object.rb
|
23
|
-
/usr/local/bin//rbxtm
|
24
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/all.rb
|
25
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/array.rb
|
26
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/class.rb
|
27
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/enumerable.rb
|
28
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/hash.rb
|
29
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/io.rb
|
30
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/numeric.rb
|
31
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/object.rb
|
32
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/ostruct.rb
|
33
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/string.rb
|
34
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/symbol.rb
|
35
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_base.rb
|
36
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_template.rb
|
37
|
-
/usr/local/bin//rbxtm
|
38
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/all.rb
|
39
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/array.rb
|
40
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/class.rb
|
41
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/enumerable.rb
|
42
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/hash.rb
|
43
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/io.rb
|
44
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/numeric.rb
|
45
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/object.rb
|
46
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/ostruct.rb
|
47
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/string.rb
|
48
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/symbol.rb
|
49
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_base.rb
|
50
|
-
/usr/local/lib/ruby/site_ruby/1.8/extensions/_template.rb
|