core_ex 0.1.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/AUTHORS ADDED
@@ -0,0 +1,26 @@
1
+ ---
2
+ CoreEx was written by and with the assistance of:
3
+
4
+ - Nicolas Despr�s <nicolas.despres@gmail.com>:
5
+ - Maintenance
6
+ - Module#attr_once
7
+ - DTime
8
+ - Exception extension
9
+ - Version
10
+
11
+ - Nicolas Pouillard <ertai@feydakins.org>:
12
+ - Maintenance
13
+ - Current repository administration
14
+
15
+ - Enumerable extension
16
+ - FileList extension
17
+ - FileUtils extension
18
+ - Pathname extension
19
+ - String extension
20
+ - Time extension
21
+ - YAML extension
22
+
23
+ - embedded_tests
24
+ - rakefile_base.rf
25
+ - RequireSystem
26
+ - TempPath
data/NEWS ADDED
@@ -0,0 +1,8 @@
1
+ = New in 0.1, 2005-05-31:
2
+
3
+ CoreEx is designed to provides a simple but quite useful extension of the
4
+ standard library of Ruby. So some classes and modules like Pathname, Time,
5
+ Enumerable, Exception, FileUtils, String, and YAML are extended. There is
6
+ also some new features like attr_once, DTime, TempPath, Version,
7
+ embedded_tests, filelist (almost from rake), a common Rakefile, and an
8
+ extension of the require system.
data/README ADDED
@@ -0,0 +1 @@
1
+ Coming Soon.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # Author:: The TTK Team. -*- ruby -*-
2
+ # Copyright:: Copyright (c) 2005 TTK team. All rights reserved.
3
+ # License:: Ruby License
4
+ # Revision:: $Id$
5
+
6
+ $: << 'lib'
7
+ load 'core_ex/rakefile_base.rf'
data/SPEC.dyn.yml ADDED
@@ -0,0 +1,8 @@
1
+ --- !ruby/object:OpenStruct
2
+ table:
3
+ :date: "Tue, 31 May 2005"
4
+ :version: !ruby/object:Version
5
+ build: 0
6
+ major: 0
7
+ minor: 1
8
+ revision: 250
data/SPEC.yml ADDED
@@ -0,0 +1,37 @@
1
+ ---
2
+ Author: The TTK Team.
3
+ License: Ruby License
4
+ Revision: '$Id$'
5
+
6
+ name: core_ex
7
+
8
+ title: CoreEx -- A proposal for a standard library extension.
9
+ summary: CoreEx is a proposal for a standard library extension.
10
+ description: >
11
+ CoreEx is designed to provides a simple but quite useful extension of the
12
+ standard library of Ruby. So some classes and modules like Pathname, Time,
13
+ Enumerable, Exception, FileUtils, String, and YAML are extended. There is
14
+ also some new features like attr_once, DTime, TempPath, Version,
15
+ embedded_tests, filelist (almost from rake), a common Rakefile, and an
16
+ extension of the require system.
17
+ homepage: http://api.feydakins.org/core_ex
18
+ rubyforge_project: ttk
19
+
20
+ root_test_suite: test/check-pkg-core_ex.yml
21
+ rdoc_dir: doc/html
22
+ tags_dir: ../../tags
23
+
24
+ rdoc_files: !filelist
25
+ - README
26
+ - AUTHORS
27
+ - NEWS
28
+ - lib/core_ex.rb
29
+ - lib/core_ex/**/*.rb
30
+
31
+ pkg_files: !filelist
32
+ - lib/**/*.rb
33
+ - test/**/*
34
+ - '[A-Z]*'
35
+
36
+ #dependencies:
37
+ # ttk: ~> 0.2.0
@@ -0,0 +1,36 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: attr_once.rb 249 2005-05-31 13:23:42Z ertai $
7
+
8
+
9
+ class Module
10
+
11
+ # You can use this method as you use attr_reader. Must be used only with
12
+ # immutable object.
13
+ #
14
+ # This method is imported from the Date implementation. It provides a way
15
+ # to compute only once the value of getter. Basically, the first time, the
16
+ # getter is used, its result is computed and stored in an attribute with the
17
+ # same name. The second times only the attribute is returned.
18
+ def attr_once(*ids)
19
+ for id in ids
20
+ module_eval <<-"end;", __FILE__, __LINE__
21
+ alias_method :__#{id.to_i}__, :#{id.to_s}
22
+ private :__#{id.to_i}__
23
+ def #{id.to_s}(*args, &block)
24
+ if defined? @__#{id.to_i}__
25
+ @__#{id.to_i}__
26
+ elsif ! self.frozen?
27
+ @__#{id.to_i}__ ||= __#{id.to_i}__(*args, &block)
28
+ else
29
+ __#{id.to_i}__(*args, &block)
30
+ end
31
+ end
32
+ end;
33
+ end
34
+ end
35
+
36
+ end # class Module
@@ -0,0 +1,169 @@
1
+ # Copyright: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License: Gnu General Public License.
4
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: dtime.rb 252 2005-05-31 23:41:42Z ertai $
7
+
8
+
9
+ require 'core_ex'
10
+ require 'core_ex/time'
11
+
12
+
13
+ class DTime
14
+
15
+ def initialize(delta)
16
+ @delta = delta.to_f.abs
17
+ @min, @sec = @delta.divmod(60)
18
+ @hour, @min = @min.floor.divmod(60)
19
+ @day, @hour = @hour.divmod(24)
20
+ end
21
+
22
+ attr_reader :delta, :sec, :min, :hour, :day
23
+
24
+ def to_s
25
+ @delta.to_s.freeze
26
+ end
27
+
28
+ def inspect
29
+ "#@day days #@hour hours #@min mins #@sec secs".freeze
30
+ end
31
+
32
+ def to_a
33
+ [ @day, @hour, @min, @sec ].freeze
34
+ end
35
+
36
+ def to_i
37
+ @delta.to_i
38
+ end
39
+
40
+ alias to_f delta
41
+
42
+ def hash
43
+ @delta.hash
44
+ end
45
+
46
+ def to_hash
47
+ { :days => @day, :hours => @hour, :mins => @min, :secs => @sec }.freeze
48
+ end
49
+
50
+ def floor
51
+ @delta.floor
52
+ end
53
+
54
+ def round
55
+ @delta.round
56
+ end
57
+
58
+ def to_yaml(opts={})
59
+ ((@day != 0 ? "#@day days " : '') +
60
+ (@hour != 0 ? "#@hour hours " : '') +
61
+ (@min != 0 ? "#@min mins " : '') +
62
+ "#@sec secs").to_yaml(opts)
63
+ end
64
+
65
+ def self.diff ( *args, &block )
66
+ start = Time.now
67
+ block[*args]
68
+ return Time.now - start
69
+ end
70
+
71
+ attr_once :to_s, :inspect, :to_a, :to_i, :hash, :to_hash, :floor,
72
+ :round, :to_yaml
73
+
74
+ end # class DTime
75
+
76
+
77
+ test_section __FILE__ do
78
+
79
+ class DTimeTest < Test::Unit::TestCase
80
+
81
+ def test_simple
82
+ d = DTime.new(260.33)
83
+ assert_equal(260, d.delta.floor)
84
+ assert_equal(20, d.sec.floor)
85
+ assert_equal(4, d.min)
86
+ assert_equal(0, d.hour)
87
+ assert_equal(0, d.day)
88
+ end
89
+
90
+ def test_complex
91
+ d = DTime.new(265678.42000)
92
+ assert_equal(265678, d.delta.floor)
93
+ assert_equal(58, d.sec.floor)
94
+ assert_equal(47, d.min)
95
+ assert_equal(1, d.hour)
96
+ assert_equal(3, d.day)
97
+ assert_equal([ 3, 1, 47 ], d.to_a[0..2])
98
+ assert_equal(58, d.to_a[3].floor)
99
+ h = d.to_hash
100
+ assert_equal(3, h[:days])
101
+ assert_equal(1, h[:hours])
102
+ assert_equal(47, h[:mins])
103
+ assert_equal(58, h[:secs].floor)
104
+ end
105
+
106
+ def test_conversion
107
+ d = DTime.new(260.75)
108
+ assert_equal('260.75', d.to_s)
109
+ assert_equal('0 days 0 hours 4 mins 20.75 secs', d.inspect)
110
+ assert_equal(260, d.to_f.floor)
111
+ assert_equal(260, d.to_i)
112
+ assert_equal(260, d.floor)
113
+ assert_equal(261, d.round)
114
+ end
115
+
116
+ def test_negative
117
+ d = DTime.new(-265678.42000)
118
+ assert_equal(265678, d.delta.floor)
119
+ assert_equal(58, d.sec.floor)
120
+ assert_equal(47, d.min)
121
+ assert_equal(1, d.hour)
122
+ assert_equal(3, d.day)
123
+ end
124
+
125
+ def test_marshal
126
+ d = DTime.new(-265678.42000)
127
+ assert_equal(d.floor, Marshal.load(Marshal.dump(d)).floor)
128
+ end
129
+
130
+ def test_to_yaml
131
+ d = DTime.new(265678.42000)
132
+ assert_equal('--- 3 days 1 hours 47 mins 58.4199999999837 secs', d.to_yaml)
133
+ d = DTime.new(5678.42000)
134
+ assert_equal('--- 1 hours 34 mins 38.4200000000001 secs', d.to_yaml)
135
+ d = DTime.new(158.42000)
136
+ assert_equal('--- 2 mins 38.42 secs', d.to_yaml)
137
+ d = DTime.new(58.42000)
138
+ assert_equal('--- 58.42 secs', d.to_yaml)
139
+ end
140
+
141
+ def test_diff
142
+ @foo = 1
143
+ d = DTime.diff(42, 43) do |*args|
144
+ assert_equal([42, 43], args)
145
+ @foo = 2
146
+ sleep 0.2
147
+ @foo = 3
148
+ end
149
+ assert_kind_of(DTime, d)
150
+ assert_equal(3, @foo)
151
+ assert(d.to_f > 0.2)
152
+ assert(d.sec > 0.2)
153
+ end
154
+
155
+ def test_init_from_dtime
156
+ c = DTime.new(260.33)
157
+ d = DTime.new(c)
158
+ assert_equal(260, d.delta.floor)
159
+ assert_equal(20, d.sec.floor)
160
+ assert_equal(4, d.min)
161
+ assert_equal(0, d.hour)
162
+ assert_equal(0, d.day)
163
+ end
164
+
165
+ end # class DTimeTest
166
+
167
+
168
+ end
169
+
@@ -0,0 +1,33 @@
1
+ # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: embedded_tests.rb 249 2005-05-31 13:23:42Z ertai $
5
+
6
+ require 'set'
7
+
8
+ module Kernel
9
+
10
+ @@test_mode = nil
11
+ @@files = Set.new
12
+
13
+ def test_mode ( aRegexp )
14
+ raise 'No block needed' if block_given?
15
+ require 'test/unit'
16
+ @@test_mode = aRegexp
17
+ end
18
+
19
+ def test_mode?
20
+ !! @@test_mode
21
+ end
22
+
23
+ def test_section ( __file__, &block )
24
+ __file__ = Pathname.new(__file__).expand_path
25
+ if block_given? and __file__.to_s =~ @@test_mode
26
+ unless @@files.include?(__file__)
27
+ Kernel.instance_eval(&block)
28
+ @@files << __file__
29
+ end
30
+ end
31
+ end
32
+
33
+ end # module Kernel
@@ -0,0 +1,173 @@
1
+ # Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
2
+ # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: enumerable.rb 252 2005-05-31 23:41:42Z ertai $
5
+
6
+ require 'core_ex'
7
+
8
+ module Enumerable
9
+
10
+ # See also Enumerable#inject
11
+ def fold ( init, each_method=:each, &block )
12
+ accu = init
13
+ send(each_method) do |*a|
14
+ accu = block[accu, *a]
15
+ end
16
+ accu
17
+ end
18
+
19
+
20
+ def foldi ( init, &block )
21
+ fold(init, :each_with_index, &block)
22
+ end
23
+
24
+
25
+ def sum
26
+ fold(0) { |accu, x| accu + x }
27
+ end
28
+
29
+
30
+ def prod
31
+ fold(1) { |accu, x| accu * x }
32
+ end
33
+
34
+
35
+ # Basically, do the same as each_with_index but yield the arguments in
36
+ # reverse order. Thus an array can be assume in certain cases to a hash.
37
+ def each_pair ( &block )
38
+ each_with_index { |x, i| block[i, x] }
39
+ end
40
+
41
+
42
+ def rec_fold ( init, &block )
43
+ fold(init) do |accu1, *a1|
44
+ a1.fold(accu1) { |accu2, a2| a2.rec_fold(accu2, &block) }
45
+ end
46
+ end
47
+
48
+ end # module Enumerable
49
+
50
+
51
+
52
+ class Object
53
+
54
+ def rec_fold ( init, &block )
55
+ block[init, self]
56
+ end
57
+
58
+ end # class Object
59
+
60
+
61
+
62
+ class String
63
+
64
+ def rec_fold ( init, &block )
65
+ if include? "\n"
66
+ super
67
+ else
68
+ block[init, self]
69
+ end
70
+ end
71
+
72
+ end # class String
73
+
74
+
75
+
76
+ test_section __FILE__ do
77
+
78
+ require 'set'
79
+ require 'generator'
80
+
81
+ class EnumerableTest < Test::Unit::TestCase
82
+
83
+ def setup
84
+ @r = 0 .. 10
85
+ @a = @r.to_a
86
+ @s = @a.to_set
87
+ @ak = 'a' .. 'k'
88
+ @h = Hash[*(@r.zip(@ak).flatten)]
89
+ end
90
+
91
+ def test_fold
92
+ [[@r, @a], [@a, @a], [@s, @s.to_a]].each do |enum, ref|
93
+ y = enum.fold([]) { |accu, x| accu << x }
94
+ assert_equal(ref, y, enum.class)
95
+ y = enum.fold([]) { |accu, x| [x] + accu }
96
+ assert_equal(ref.reverse, y, enum.class)
97
+ y = enum.fold([]) { |accu, x| accu << x ; 42 }
98
+ assert_equal(42, y, enum.class)
99
+ end
100
+
101
+ y = @h.fold([[], []]) do |accu, x|
102
+ [ [x.first] + accu.first, [x.last] + accu.last ]
103
+ end
104
+ assert_equal(@h.keys.reverse, y.first)
105
+ assert_equal(@h.values.reverse, y.last)
106
+ end
107
+
108
+ def test_foldi
109
+ [@r, @a, @s, @h].each do |enum|
110
+ ref = @a
111
+ y = enum.foldi([]) { |accu, x, i| accu << i }
112
+ assert_equal(ref, y, enum.class)
113
+ y = enum.foldi([]) { |accu, x, i| [i] + accu }
114
+ assert_equal(ref.reverse, y, enum.class)
115
+ y = enum.foldi([]) { |accu, x, i| accu << i ; 42 }
116
+ assert_equal(42, y, enum.class)
117
+ end
118
+ end
119
+
120
+ def test_sum
121
+ assert_equal(55, @a.sum)
122
+ assert_equal(55, @r.sum)
123
+ assert_equal(55, @s.sum)
124
+ assert_equal(55, @h.keys.sum)
125
+ end
126
+
127
+ def test_prod
128
+ @r = 1 .. 10
129
+ @a = @r.to_a
130
+ @s = @a.to_set
131
+ @ak = 'a' .. 'k'
132
+ @h = Hash[*(@r.zip(@ak).flatten)]
133
+ assert_equal(3628800, @a.prod)
134
+ assert_equal(3628800, @r.prod)
135
+ assert_equal(3628800, @s.prod)
136
+ assert_equal(3628800, @h.keys.prod)
137
+ end
138
+
139
+ def each_pair_checker ( enum, ref )
140
+ j = 0
141
+ enum.each_pair do |i, x|
142
+ assert_equal(j, i)
143
+ j += 1
144
+ assert_equal(ref.next, x)
145
+ end
146
+ end
147
+
148
+ def test_each_pair
149
+ each_pair_checker(@a, Generator.new(@a))
150
+ each_pair_checker(@s, Generator.new(@s))
151
+ each_pair_checker(@r, Generator.new(@r))
152
+ y = @h.fold([], :each_pair) { |accu, x| [x] + accu }
153
+ assert_equal(@h.keys.reverse, y)
154
+ end
155
+
156
+ def test_rec_fold
157
+ h = { 1 => { 2 => 3 }, 4 => { 5 => 6 } }
158
+ y = h.rec_fold([]) { |accu, *a| [a] + accu }
159
+ assert_equal([[6], [5], [4], [3], [2], [1]], y)
160
+ y = @h.rec_fold([]) { |accu, *a| [a] + accu }
161
+ assert_equal([["k"], [10], ["e"], [4], ["j"], [9], ["d"], [3], ["i"],
162
+ [8], ["c"], [2], ["h"], [7], ["b"], [1], ["g"], [6], ["a"],
163
+ [0], ["f"], [5]], y)
164
+
165
+ a = [1, [2,3], [[[4], 5]]]
166
+ y = a.rec_fold(0) { |accu, x| accu + x }
167
+ assert_equal(a.flatten.sum, y)
168
+ end
169
+
170
+
171
+ end # EnumerableTest
172
+
173
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright:: Copyright (c) 2004 Nicolas Despres. All rights reserved.
2
+ # Author:: Nicolas Despres <polrop@lrde.epita.fr>.
3
+ # License:: Gnu General Public License.
4
+ # Revision:: $Id: exception.rb 249 2005-05-31 13:23:42Z ertai $
5
+
6
+
7
+ class Exception
8
+
9
+ #FIXME: write a ruby_pp which print exception backtrace exactly as ruby does
10
+ # by cuting long and repetitive backtrace.
11
+
12
+ def long_pp
13
+ str = short_pp + "\n"
14
+ backtrace[1..-1].each { |x| str += " from #{x}\n" } if backtrace
15
+ str.chomp!
16
+ str
17
+ end
18
+
19
+ def short_pp
20
+ if backtrace.nil?
21
+ tiny_pp
22
+ else
23
+ "#{backtrace[0]}: #{tiny_pp}"
24
+ end
25
+ end
26
+
27
+ def tiny_pp
28
+ exc_name = inspect.sub(/^#<(\w+):.+$/, '\1')
29
+ "#{self} (#{exc_name})"
30
+ end
31
+
32
+ end # class Exception