webget_ruby_ramp 1.7.3 → 1.7.4
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +1 -1
- data/LICENSE.txt +12 -0
- data/README.rdoc +226 -0
- data/lib/webget_ruby_ramp.rb +10 -2
- data/lib/webget_ruby_ramp/array.rb +2 -0
- data/lib/webget_ruby_ramp/class.rb +36 -0
- data/lib/webget_ruby_ramp/csv.rb +8 -3
- data/lib/webget_ruby_ramp/date.rb +17 -13
- data/lib/webget_ruby_ramp/enumerable.rb +2 -0
- data/lib/webget_ruby_ramp/file.rb +2 -0
- data/lib/webget_ruby_ramp/hash.rb +13 -1
- data/lib/webget_ruby_ramp/integer.rb +24 -0
- data/lib/webget_ruby_ramp/io.rb +2 -0
- data/lib/webget_ruby_ramp/kernel.rb +2 -0
- data/lib/webget_ruby_ramp/math.rb +2 -0
- data/lib/webget_ruby_ramp/nil.rb +2 -0
- data/lib/webget_ruby_ramp/numeric.rb +5 -6
- data/lib/webget_ruby_ramp/object.rb +2 -0
- data/lib/webget_ruby_ramp/process.rb +3 -1
- data/lib/webget_ruby_ramp/string.rb +2 -0
- data/lib/webget_ruby_ramp/symbol.rb +2 -0
- data/lib/webget_ruby_ramp/time.rb +2 -0
- data/lib/webget_ruby_ramp/xml.rb +2 -0
- data/lib/webget_ruby_ramp/yaml.rb +2 -0
- data/test/webget_ruby_ramp/array_test.rb +218 -75
- data/test/webget_ruby_ramp/class_test.rb +76 -0
- data/test/webget_ruby_ramp/csv_test.rb +35 -8
- data/test/webget_ruby_ramp/date_test.rb +14 -2
- data/test/webget_ruby_ramp/enumerable_test.rb +2 -2
- data/test/webget_ruby_ramp/hash_test.rb +91 -4
- data/test/webget_ruby_ramp/integer_test.rb +16 -0
- data/test/webget_ruby_ramp/math_test.rb +1 -0
- data/test/webget_ruby_ramp/numeric_test.rb +60 -0
- metadata +6 -2
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
#� ��m��h�5�*�֠bx�_�a�����q����Bռ���]��0�\*�8���1���<�8����q����1_���
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
LICENSE
|
2
|
+
|
3
|
+
You may choose any of these licenses:
|
4
|
+
|
5
|
+
- CreativeCommons License, Non-commercial Share Alike
|
6
|
+
- LGPL, GNU Lesser General Public License
|
7
|
+
- MIT License
|
8
|
+
- Ruby License
|
9
|
+
|
10
|
+
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
11
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
12
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
|
2
|
+
= WebGet Ruby Gem: Ramp is a toolkit of Ruby base class extensions
|
3
|
+
|
4
|
+
Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
|
5
|
+
Copyright:: Copyright (c) 2006-2010 Joel Parker Henderson
|
6
|
+
License:: CreativeCommons License, Non-commercial Share Alike
|
7
|
+
License:: LGPL, GNU Lesser General Public License
|
8
|
+
|
9
|
+
Ramp is a library of extensions to Ruby base classes, including Array, Date, Enumerable, Hash, Kernel, Numeric, Object, Process, String, Time, and YAML.
|
10
|
+
|
11
|
+
Testing:
|
12
|
+
<ul>
|
13
|
+
<li>Each has an associated test class, e.g., ArrayTest, DateTest, etc.
|
14
|
+
<li>The easy way to run the tests: gem install webget_ruby_ramp --test
|
15
|
+
</ul>
|
16
|
+
|
17
|
+
|
18
|
+
== Array
|
19
|
+
|
20
|
+
* car, cdr: aka first, rest (see shifted)
|
21
|
+
* choice, choices: one or more random elements from an array
|
22
|
+
* cross: return the cross pairings of an array with another array
|
23
|
+
* divvy: divides an array, like a pie, into a specified number of slices
|
24
|
+
* join: same as Array#join with some improvments
|
25
|
+
* onto: return a hash that maps an array's keys on to another array's values
|
26
|
+
* rotate: moves the first element of an array to the end
|
27
|
+
* rest: return the rest of the items of the array (aka cdr, aka shifted)
|
28
|
+
* shifted, shifted!: return an array with the first n items shifted (aka cdr, aka rest)
|
29
|
+
* shuffle, shuffle!: randomly sort an array efficiently; each of these methods are loaded only if needed (Ruby 1.8.7+ already defines shuffle)
|
30
|
+
* slices: divide an array into specified number of equal size sub-arrays ([1,2,3,4,5,6]slices(3) => [[1,2],[3,4],[5,6]])
|
31
|
+
* to_csv: join a multidimensional array into a string in CSV (Comma Separated Values), with each subarray becoming one 'line' in the output; typically for viewing in a spreadsheet such as Excel.
|
32
|
+
* to_tdf: join a multidimensional array into a string in TDF (Tab Delimited Format); this is an alias for #to_tsv
|
33
|
+
* to_tsv: join a multidimensional array into a string in TSV (Tab Separated Values), with each subarray becoming one 'line' in the output; typically for viewing in a spreadsheet such as Excel.
|
34
|
+
* union: builds an array containing each of the unique elements of sub-arrays ([[1,2,3,4],[2,3,4,5],[3,4,5,6]].union => [1,2,3,4,5,6])
|
35
|
+
|
36
|
+
|
37
|
+
== CSV
|
38
|
+
|
39
|
+
* http_headers: provides web file download headers for text/csv content type and disposition.
|
40
|
+
|
41
|
+
|
42
|
+
== Date
|
43
|
+
|
44
|
+
* age_days, age_years
|
45
|
+
* between: a random date between two specified dates
|
46
|
+
* to_sql: date as a string formatted as expected for MySQL
|
47
|
+
* weekday?, weekend?: is date a weekday or on the weekend
|
48
|
+
|
49
|
+
|
50
|
+
== Enumerable
|
51
|
+
|
52
|
+
* cartesian_product: return an array of all possible ordered tuples from arrays.
|
53
|
+
* hash_by: convert the array to a hash by mapping each item to a key=>value pair.
|
54
|
+
* index_by: convert the array to a hash by mapping each ite to a key=>item pair.
|
55
|
+
* join: forwards to self.to_a.join
|
56
|
+
* map_id: returns the id of an Enumerable object; *requires* that the object respond to an 'id' message
|
57
|
+
* map_to_a, map_to_f, map_to_i, map_to_s, map_to_sym: convert each object to a specific type by calling its respective method to_a, to_i, to_f, to_s, to_sym
|
58
|
+
* map_with_index: for each item, yield to a block with the item and its incrementing index
|
59
|
+
* nitems_until, select_until: returns the number of, or an array containing, the leading elements for which block is false or nil.
|
60
|
+
* nitems_while, select_while: returns the number of items, or an array containing the leading elements, for which block is not false or nil.
|
61
|
+
* nitems_with_index, select_with_index: calls block with two arguments, the item and its index, for each item in enum. Returns the number of, or an array containing, the leading elements for which block is not false or nil.
|
62
|
+
* power_set: return an array with all subsets of the enum's elements
|
63
|
+
|
64
|
+
|
65
|
+
== File
|
66
|
+
|
67
|
+
* File.joindir: wrapper for File.join(File.dirname(...),string,...)
|
68
|
+
|
69
|
+
|
70
|
+
== Hash
|
71
|
+
|
72
|
+
* size?: return true if hash has any keys
|
73
|
+
* each_sort: sort the keys then call each
|
74
|
+
* each_key!: passes each key to a specified block and updates hash in place if the key changes
|
75
|
+
* each_pair!: passes each key value pair to a specified block and updates the hash in place if the key or value change.
|
76
|
+
* each_value!: passes each value to a specified block and updates the hash in place if the value changes.
|
77
|
+
* map_pair: map each key-value pair by calling a a block
|
78
|
+
* pivot: aggregates subtotals by keys and values, such as a rollup and rolldown
|
79
|
+
* to_yaml_sort: returns a YAML object, sorted by field name
|
80
|
+
|
81
|
+
|
82
|
+
== Integer
|
83
|
+
|
84
|
+
* maps: syntactic sugar to yield n times to a block, returning an array of any results (e.g. 3.maps{rand} => [0.4351325,0.7778625,0.158613534])
|
85
|
+
|
86
|
+
|
87
|
+
== IO
|
88
|
+
|
89
|
+
* readrow: reads a row line as with IO#readline, and return the row split it into fields
|
90
|
+
* IO.readrows: reads the entire file specified by name as individual row lines, and returns those rows split into fields, in an array of arrays.
|
91
|
+
|
92
|
+
|
93
|
+
== Kernel
|
94
|
+
|
95
|
+
* method_name:
|
96
|
+
* method_name_of_caller: returns the name of the method which called the current method, or the Nth parent up the call stack if the optional caller_index parameter is passed.
|
97
|
+
|
98
|
+
|
99
|
+
== Math
|
100
|
+
|
101
|
+
* ln(x): natural log of x
|
102
|
+
* logn(x,b): log of x in base b
|
103
|
+
|
104
|
+
|
105
|
+
== NilClass
|
106
|
+
|
107
|
+
* blank?: return true (same as Rails)
|
108
|
+
|
109
|
+
|
110
|
+
== Numeric
|
111
|
+
|
112
|
+
* if: returns 0 if the passed flag is any of: nil, false, 0, [], {} and otherwise returns self
|
113
|
+
* unless: returns 0 unless the passed flag is any of: nil, false, 0, [], {} and otherwise returns self
|
114
|
+
* peta, tera, giga, mega, kilo, hecto, deka, deci, centi, milli, micro, nano: multiply/divide by powers of ten
|
115
|
+
|
116
|
+
|
117
|
+
== Object
|
118
|
+
|
119
|
+
* in?: returns boolean indicating whether the object is a member of the specified array parameter
|
120
|
+
|
121
|
+
|
122
|
+
== Process
|
123
|
+
|
124
|
+
Extensions that help debug Ruby programs.
|
125
|
+
|
126
|
+
* (class) ps: output of the system 'ps' command, also including aliases, as raw plain text.
|
127
|
+
* (class) pss: output of the system 'ps' command as a hash with each value set to the right type, e.g., integer, float, etc..
|
128
|
+
|
129
|
+
|
130
|
+
== REXML::Attributes
|
131
|
+
|
132
|
+
* hash: flattens the attributes hash set into a more useful ruby hash, e.g. {:height => 100, :width => 400 }
|
133
|
+
|
134
|
+
|
135
|
+
== REXML::Document
|
136
|
+
|
137
|
+
* remove_attributes: remove all the attributes from the document's elements
|
138
|
+
|
139
|
+
|
140
|
+
== REXML::Element
|
141
|
+
|
142
|
+
* remove_attributes: remove all the attributes from the element
|
143
|
+
|
144
|
+
|
145
|
+
== String
|
146
|
+
|
147
|
+
* capitalize_words (alias to titleize/titlecase): ensures the first character of each word is uppercase.
|
148
|
+
* decrement: decrease the rightmost natural number, defaults to one value lower or by the optional step parameter value.
|
149
|
+
* increment: increase the rightmost natural number, defaults to one value higher or by the optional step parameter value.
|
150
|
+
* lorem: return a short random string, good for use in "lorem ipsum" sample text.
|
151
|
+
* lowcase: translate a string to lowercase, digits and single underscores (e.g. to a method name)
|
152
|
+
* prev/pred: previous string ("b" => "a", "bbc" => "bbb", "a" => "z", "880" => "879")
|
153
|
+
* prev!/pred!: updates variable to the previous string in place (astring = "bbc", astring.prev!, puts astring => "bbb")
|
154
|
+
* (class) prev_char/pred_char: returns the previous character, with a changed flag and carry flag; that is, there are three returned values, a string and two booleans.
|
155
|
+
* split_tab: split the string into an array at each embedded tab ("Last\tFirst\tMiddle" => ["last","first","middle"])
|
156
|
+
* split_tsv: split the string into an array at each embedded newline and embedded tab ("A\tB\t\C\nD\tE\tF" => [["A","B","C"],["D","E","F"]])
|
157
|
+
* to_class: the global class reference of a given string
|
158
|
+
* words: split the string into an array of words
|
159
|
+
|
160
|
+
|
161
|
+
== Symbol
|
162
|
+
|
163
|
+
* <=> and include the comparable mixin to compare symbols as strings
|
164
|
+
|
165
|
+
|
166
|
+
== Time
|
167
|
+
|
168
|
+
* (class) stamp: current time in UTC as a timestamp string ("YYYYMMDDHHMMSS")
|
169
|
+
* to_sql: time as a string formatted as expected for MySQL
|
170
|
+
|
171
|
+
|
172
|
+
== XML
|
173
|
+
|
174
|
+
* (class) load_dir: specify a one or more directory patterns and pass each XML file in the matching directories to a block; see [Dir#glob](http://www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
|
175
|
+
* (class) strip_all: delete exraneous junk from an XML text string, typically for sanitizing input
|
176
|
+
* (class) strip_attributes: delete all attributes from an XML text string
|
177
|
+
* (class) strip_comments: delete all comments from an XML text string
|
178
|
+
* (class) strip_microsoft: delete all proprietary Microsoft code from an XML text string
|
179
|
+
* (class) strip_unprintables: delete all unprintable characters from an XML text string
|
180
|
+
|
181
|
+
|
182
|
+
== YAML
|
183
|
+
|
184
|
+
* (class) load_dir: specify a one or more directory patterns and pass each YAML file in the matching directories to a block; see [Dir#glob](http://www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
|
185
|
+
|
186
|
+
|
187
|
+
== Changes
|
188
|
+
|
189
|
+
- 1.7.3 Refactor Rails classes to their own gem
|
190
|
+
- 1.7.2 Gemcutter update
|
191
|
+
- 1.7.1.8 Add Enumerable#map_with_index
|
192
|
+
- 1.7.1.6 Add ActiveRecord::SaveExtensions#save_false_then_reload!
|
193
|
+
- 1.7.1.3 Add XML#strip_xxx
|
194
|
+
- 1.7.1.2 Update gems: Gemcutter, Ruby 1.9.1, JRuby sqlite3
|
195
|
+
- 1.7.1.0 Add XML attributes methods #
|
196
|
+
- 1.7.0.9 Add Enumerable #hash_by, #index_by
|
197
|
+
- 1.7.0.7 Add Array#to_tsv, String#split_tsv, improve Array#to_csv
|
198
|
+
- 1.7.0.6 Add Array#shuffle, Array#shuffle!
|
199
|
+
- 1.7.0.5 Add Array#shifted, Array#rest, Array#car, Array#cdr
|
200
|
+
- 1.7.0.4 Upgrade IO#readrows and #readrow to use options
|
201
|
+
- 1.7.0.2 Add Array#to_tdf
|
202
|
+
- 1.7.0.1 Remove sqlite3 testing dependency
|
203
|
+
- 1.6.9.6 Add Symbol with comparable and <=>
|
204
|
+
- 1.6.9.5 Add ActiveRecord testing with sqlite3
|
205
|
+
- 1.6.9.4 JRuby install and test successful
|
206
|
+
- 1.6.9.3 Array#join add infix, prefix, suffix
|
207
|
+
- 1.6.9.2 Improve ri docs
|
208
|
+
- 1.6.9.1 Add Array#onto, Enumerable#intersect?
|
209
|
+
- 1.6.9.0 Add IO, File, ActiveRecord#seed
|
210
|
+
- 1.6.8.9 Add Enumerable#to_h, Array#hash, Hash#each_key!, Hash#each_pair, Hash#each_value!
|
211
|
+
- 1.6.8.8 Add Hash#map_pair, Hash#size?, Hash#pivot
|
212
|
+
- 1.6.8.7 Add Math
|
213
|
+
- 1.6.8.6 Add ActiveRecord SchemaStatements
|
214
|
+
- 1.6.8.5 Add Integer#maps, String#ACCENTS
|
215
|
+
- 1.6.8.4 Add XML
|
216
|
+
- 1.6.8.3 Add ActiveRecord
|
217
|
+
- 1.6.8.2 Add String#lowcase
|
218
|
+
- 1.6.8 Add map_to_xxx methods
|
219
|
+
- 1.6.7 Add CSV
|
220
|
+
- 1.6.6 Add Array#to_csv, Integer, String#lorem, etc., improve tests
|
221
|
+
- 1.6.4 Bug fixes: String characters and YAML test files
|
222
|
+
- 1.6.2 Improve organizaiton of class files to lib/ramp
|
223
|
+
- 1.6.0 Upgraded to work with Ruby 1.9.1
|
224
|
+
- 1.5.0 Combined all Ruby extension files into one gem
|
225
|
+
- 1.0.0 Original
|
226
|
+
|
data/lib/webget_ruby_ramp.rb
CHANGED
@@ -35,6 +35,11 @@ Testing:
|
|
35
35
|
* union: builds an array containing each of the unique elements of sub-arrays ([[1,2,3,4],[2,3,4,5],[3,4,5,6]].union => [1,2,3,4,5,6])
|
36
36
|
|
37
37
|
|
38
|
+
== Class
|
39
|
+
|
40
|
+
* publicize_methods: make all methods public for a block, e.g. to unit test private methods
|
41
|
+
|
42
|
+
|
38
43
|
== CSV
|
39
44
|
|
40
45
|
* http_headers: provides web file download headers for text/csv content type and disposition.
|
@@ -82,7 +87,9 @@ Testing:
|
|
82
87
|
|
83
88
|
== Integer
|
84
89
|
|
90
|
+
* even?: is the number even?
|
85
91
|
* maps: syntactic sugar to yield n times to a block, returning an array of any results (e.g. 3.maps{rand} => [0.4351325,0.7778625,0.158613534])
|
92
|
+
* odd?: is the number odd?
|
86
93
|
|
87
94
|
|
88
95
|
== IO
|
@@ -187,7 +194,8 @@ Extensions that help debug Ruby programs.
|
|
187
194
|
|
188
195
|
== Changes
|
189
196
|
|
190
|
-
- 1.7.
|
197
|
+
- 1.7.4 Add Class#publicize_methods, Integer#even?, Integer#odd?
|
198
|
+
- 1.7.3 Refactor Rails classes to their own gem, add README, LICENSE
|
191
199
|
- 1.7.2 Gemcutter update
|
192
200
|
- 1.7.1.8 Add Enumerable#map_with_index
|
193
201
|
- 1.7.1.6 Add ActiveRecord::SaveExtensions#save_false_then_reload!
|
@@ -227,7 +235,7 @@ Extensions that help debug Ruby programs.
|
|
227
235
|
|
228
236
|
=end
|
229
237
|
|
230
|
-
|
238
|
+
['array','class','csv','date','enumerable','file','hash','integer','io','kernel','math','nil','numeric','object','process','string','symbol','time','xml','yaml'].map{|x|
|
231
239
|
require File.dirname(__FILE__) + "/webget_ruby_ramp/#{x}.rb"
|
232
240
|
}
|
233
241
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# Class extensions
|
4
|
+
|
5
|
+
|
6
|
+
class Class
|
7
|
+
|
8
|
+
# Make all the methods public for a block.
|
9
|
+
#
|
10
|
+
# This is especially useful for unit testing
|
11
|
+
# a class's private and protected methods
|
12
|
+
#
|
13
|
+
# From http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html
|
14
|
+
#
|
15
|
+
# ==Example
|
16
|
+
# MyClass.publicize_methods do
|
17
|
+
# ...call some method that was private or protected...
|
18
|
+
# end
|
19
|
+
|
20
|
+
def publicize_methods
|
21
|
+
saved_private_instance_methods = self.private_instance_methods
|
22
|
+
saved_protected_instance_methods = self.protected_instance_methods
|
23
|
+
self.class_eval {
|
24
|
+
public *saved_private_instance_methods
|
25
|
+
public *saved_protected_instance_methods
|
26
|
+
}
|
27
|
+
yield
|
28
|
+
self.class_eval {
|
29
|
+
private *saved_private_instance_methods
|
30
|
+
protected *saved_protected_instance_methods
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
|
data/lib/webget_ruby_ramp/csv.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
# Comma Separated Values extensions
|
2
4
|
|
3
5
|
class CSV
|
@@ -34,19 +36,22 @@ class CSV
|
|
34
36
|
}
|
35
37
|
end
|
36
38
|
|
39
|
+
|
37
40
|
# Helper to try to "do the right thing" for the common case of Rails & MS IE.
|
38
41
|
#
|
39
42
|
# Rails automatically defines a _request_ object,
|
40
43
|
# that has an env HTTP_USER_AGENT.
|
41
|
-
|
44
|
+
#
|
45
|
+
# @return [Hash] options
|
46
|
+
|
42
47
|
def self.http_headers_adjust_for_broken_msie(options={})
|
43
48
|
request = options[:request] || request
|
44
49
|
msie = (request and request.env['HTTP_USER_AGENT'] =~ /msie/i)
|
45
50
|
if msie
|
46
|
-
options[:content_type]||='text/plain'
|
51
|
+
options[:content_type]||='text/plain'
|
47
52
|
options[:cache]||=false
|
48
53
|
end
|
49
|
-
options
|
54
|
+
return options
|
50
55
|
end
|
51
56
|
|
52
57
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
|
3
5
|
# Date extensions
|
@@ -5,17 +7,6 @@ require 'date'
|
|
5
7
|
class Date
|
6
8
|
|
7
9
|
|
8
|
-
# Return date in a sql format: YYYY-MM-DD
|
9
|
-
#
|
10
|
-
# ==Example
|
11
|
-
# d=Date.today
|
12
|
-
# d.to_sql => "2007-12-31"
|
13
|
-
|
14
|
-
def to_sql
|
15
|
-
return to_time.strftime("%Y-%m-%d")
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
10
|
# Return true if the date is a weekday: Mon, Tue, Wed, Thu, Fri
|
20
11
|
#
|
21
12
|
# ==Example
|
@@ -52,6 +43,17 @@ class Date
|
|
52
43
|
end
|
53
44
|
|
54
45
|
|
46
|
+
# Return date in a sql format: YYYY-MM-DD
|
47
|
+
#
|
48
|
+
# ==Example
|
49
|
+
# d=Date.today
|
50
|
+
# d.to_sql => "2007-12-31"
|
51
|
+
|
52
|
+
def to_sql
|
53
|
+
return to_time.strftime("%Y-%m-%d")
|
54
|
+
end
|
55
|
+
|
56
|
+
|
55
57
|
# Return the age in years for a given date.
|
56
58
|
#
|
57
59
|
# ==Example
|
@@ -73,6 +75,7 @@ class Date
|
|
73
75
|
# birthdate.age_years(new_years_eve) => 28 # after the birthday
|
74
76
|
|
75
77
|
def age_years(compare_date=Date.today)
|
78
|
+
(compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date"
|
76
79
|
age=compare_date.year-year
|
77
80
|
compare_month = compare_date.month
|
78
81
|
age-=1 if compare_month < month or (compare_month==month and compare_date.day < day)
|
@@ -82,8 +85,9 @@ class Date
|
|
82
85
|
|
83
86
|
# Return the age in days for a given date.
|
84
87
|
|
85
|
-
def age_days(
|
86
|
-
(
|
88
|
+
def age_days(compare_date=Date.today)
|
89
|
+
(compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date"
|
90
|
+
(compare_date-self).to_i
|
87
91
|
end
|
88
92
|
|
89
93
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require 'yaml'
|
2
4
|
|
3
5
|
# Hash extensions
|
@@ -14,6 +16,11 @@ class Hash
|
|
14
16
|
# Calls block once for each key in hsh, passing the key and value to the block as a two-element array.
|
15
17
|
#
|
16
18
|
# The keys are sorted.
|
19
|
+
#
|
20
|
+
# ==Example
|
21
|
+
# h = { "xyz" => "123", "abc" => "789" }
|
22
|
+
# h.each_sort {|key, val| ... }
|
23
|
+
# => calls the block with "abc" => "789", then with "xyz" => "123"
|
17
24
|
|
18
25
|
def each_sort
|
19
26
|
keys.sort.each{|key| yield key,self[key] }
|
@@ -113,6 +120,11 @@ class Hash
|
|
113
120
|
# Hash#to_yaml with sort
|
114
121
|
#
|
115
122
|
# From http://snippets.dzone.com/tag/yaml
|
123
|
+
#
|
124
|
+
# ==Example
|
125
|
+
# h = {"a"=>"b", "c"=>"d", "e"=>"f" }
|
126
|
+
# h.to_yaml_sort
|
127
|
+
# => "--- \na: b\nc: d\ne: f\n"
|
116
128
|
|
117
129
|
def to_yaml_sort( opts = {} )
|
118
130
|
YAML::quick_emit( object_id, opts ) do |out|
|
@@ -216,7 +228,7 @@ class Hash
|
|
216
228
|
case direction_name
|
217
229
|
when 'key','keys','up','left','out' then return true
|
218
230
|
when 'val','vals','down','right','in' then return false
|
219
|
-
else raise 'Pivot direction must be either: up/left/out or down/right/in'
|
231
|
+
else raise ArgumentError, 'Pivot direction must be either: key/keys/up/left/out or val/vals/down/right/in'
|
220
232
|
end
|
221
233
|
end
|
222
234
|
|