libaaron 1.1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/libaaron.rb +67 -7
- metadata +4 -10
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Version 1.
|
1
|
+
Version 1.2
|
data/lib/libaaron.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
|
3
|
-
# Version 1.
|
3
|
+
# Version 1.2
|
4
4
|
|
5
5
|
# Copyright 2008, 2009, 2010 Aaron Lynch
|
6
6
|
|
@@ -19,6 +19,8 @@ You should have received a copy of the GNU General Public License
|
|
19
19
|
along with this library. If not, see <http://www.gnu.org/licenses/>.
|
20
20
|
=end
|
21
21
|
|
22
|
+
LIBAARON_VERSION = "1.2"
|
23
|
+
|
22
24
|
class Dir
|
23
25
|
def self.real_entries(directory=".")
|
24
26
|
entries(directory)-[".",".."]
|
@@ -103,18 +105,41 @@ class Array
|
|
103
105
|
end
|
104
106
|
return out_arr
|
105
107
|
end
|
106
|
-
def
|
108
|
+
def modify(index)
|
107
109
|
self[index] = yield self[index]
|
108
110
|
end
|
109
111
|
def rand
|
110
112
|
self[Kernel.rand(self.length)]
|
111
113
|
end
|
114
|
+
def smart_join(common_separator=", ", end_separator = " and ")
|
115
|
+
if self.length == 1
|
116
|
+
return self.join(common_separator)
|
117
|
+
elsif self.length == 2
|
118
|
+
return %[#{self.first}#{end_separator}#{self.last}]
|
119
|
+
elsif self.length >= 3
|
120
|
+
return (self[0..-2]).join(common_separator) + %[#{end_separator}#{self.last}]
|
121
|
+
else
|
122
|
+
raise "Some funky length for array: #{self.length}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
def all_true ###TODO rewrite with all?
|
126
|
+
self.select{|e|e}
|
127
|
+
end
|
128
|
+
def all_false
|
129
|
+
self.select{|e|!e}
|
130
|
+
end
|
131
|
+
def all_true?
|
132
|
+
self.all_true == self
|
133
|
+
end
|
134
|
+
def all_false?
|
135
|
+
self.all_false == self
|
136
|
+
end
|
112
137
|
end
|
113
138
|
|
114
139
|
class Numeric
|
115
140
|
#Taken from a code snippet by tthorley at http://codesnippets.joyent.com/posts/show/1812
|
116
|
-
def
|
117
|
-
pre,post =
|
141
|
+
def format(value_separator = ",", trailing_zeroes = 2, after = 3, decimal_separator = ".")
|
142
|
+
pre,post = self.to_s.split(".")
|
118
143
|
pre.gsub(/(\d)(?=(\d{#{after}})+$)/, '\0' + value_separator)+(post ? "#{decimal_separator}#{post == "0" ? "0"*trailing_zeroes : post}" : "")
|
119
144
|
end
|
120
145
|
def thousand
|
@@ -131,6 +156,30 @@ class Numeric
|
|
131
156
|
end
|
132
157
|
end
|
133
158
|
|
159
|
+
class Integer
|
160
|
+
def f!
|
161
|
+
optional_require 'rubygems'
|
162
|
+
gmp_bindings = optional_require 'gmp'
|
163
|
+
|
164
|
+
unless gmp_bindings == false
|
165
|
+
return GMP::Z.fac(self).to_i
|
166
|
+
else
|
167
|
+
# do it the slow, ruby way
|
168
|
+
out = 1
|
169
|
+
i = 1
|
170
|
+
while (i<=self)
|
171
|
+
out *= i
|
172
|
+
i += 1
|
173
|
+
end
|
174
|
+
return out
|
175
|
+
end
|
176
|
+
end
|
177
|
+
def choose(number_of_elements)
|
178
|
+
ne = number_of_elements
|
179
|
+
return (self.f!)/(ne.f!*(self-ne).f!)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
134
183
|
class Struct
|
135
184
|
def to_hash
|
136
185
|
hash = Hash.new
|
@@ -144,7 +193,18 @@ class Struct
|
|
144
193
|
end
|
145
194
|
|
146
195
|
module Kernel
|
147
|
-
def numform(*args)
|
148
|
-
|
196
|
+
def numform(number, *args)
|
197
|
+
number.format(*args)
|
149
198
|
end
|
150
|
-
|
199
|
+
def optional_require(library)
|
200
|
+
begin
|
201
|
+
if require library
|
202
|
+
return true
|
203
|
+
else
|
204
|
+
return nil # the library was already loaded; we return nil instead of something more obvious because the standard require method returns false if the library is already loaded, and we want to maintain boolean compatibility
|
205
|
+
end
|
206
|
+
rescue LoadError
|
207
|
+
return false
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libaaron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 17
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 1
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 1.1.1
|
7
|
+
- 2
|
8
|
+
version: "1.2"
|
11
9
|
platform: ruby
|
12
10
|
authors:
|
13
11
|
- Aaron Lynch
|
@@ -15,7 +13,7 @@ autorequire:
|
|
15
13
|
bindir: bin
|
16
14
|
cert_chain: []
|
17
15
|
|
18
|
-
date:
|
16
|
+
date: 2011-11-14 00:00:00 -05:00
|
19
17
|
default_executable:
|
20
18
|
dependencies: []
|
21
19
|
|
@@ -42,27 +40,23 @@ rdoc_options: []
|
|
42
40
|
require_paths:
|
43
41
|
- lib
|
44
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
43
|
requirements:
|
47
44
|
- - ">="
|
48
45
|
- !ruby/object:Gem::Version
|
49
|
-
hash: 3
|
50
46
|
segments:
|
51
47
|
- 0
|
52
48
|
version: "0"
|
53
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
50
|
requirements:
|
56
51
|
- - ">="
|
57
52
|
- !ruby/object:Gem::Version
|
58
|
-
hash: 3
|
59
53
|
segments:
|
60
54
|
- 0
|
61
55
|
version: "0"
|
62
56
|
requirements: []
|
63
57
|
|
64
58
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.3.
|
59
|
+
rubygems_version: 1.3.6
|
66
60
|
signing_key:
|
67
61
|
specification_version: 3
|
68
62
|
summary: libaaron is a Ruby convenince library of common operations.
|