blackwinter-ruby-nuggets 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +676 -0
- data/ChangeLog +5 -0
- data/README +60 -0
- data/Rakefile +20 -0
- data/lib/nuggets/all.rb +34 -0
- data/lib/nuggets/array/combination.rb +86 -0
- data/lib/nuggets/array/flatten_once.rb +68 -0
- data/lib/nuggets/array/format.rb +124 -0
- data/lib/nuggets/array/in_order.rb +62 -0
- data/lib/nuggets/array/monotone.rb +101 -0
- data/lib/nuggets/array/only.rb +56 -0
- data/lib/nuggets/array/rand.rb +45 -0
- data/lib/nuggets/array/shuffle.rb +133 -0
- data/lib/nuggets/array/to_hash.rb +86 -0
- data/lib/nuggets/enumerable/agrep.rb +82 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +99 -0
- data/lib/nuggets/enumerable/minmax.rb +119 -0
- data/lib/nuggets/env/user_encoding.rb +53 -0
- data/lib/nuggets/env/user_home.rb +54 -0
- data/lib/nuggets/file/which.rb +74 -0
- data/lib/nuggets/hash/at.rb +87 -0
- data/lib/nuggets/hash/in_order.rb +52 -0
- data/lib/nuggets/hash/insert.rb +65 -0
- data/lib/nuggets/hash/only.rb +68 -0
- data/lib/nuggets/integer/factorial.rb +74 -0
- data/lib/nuggets/integer/to_binary_s.rb +47 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/modes.rb +133 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +109 -0
- data/lib/nuggets/numeric/limit.rb +70 -0
- data/lib/nuggets/numeric/signum.rb +60 -0
- data/lib/nuggets/numeric/to_multiple.rb +68 -0
- data/lib/nuggets/object/blank.rb +119 -0
- data/lib/nuggets/object/boolean.rb +69 -0
- data/lib/nuggets/object/eigenclass.rb +2 -0
- data/lib/nuggets/object/ghost_class.rb +2 -0
- data/lib/nuggets/object/metaclass.rb +2 -0
- data/lib/nuggets/object/msend.rb +55 -0
- data/lib/nuggets/object/singleton_class.rb +150 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/proc/bind.rb +68 -0
- data/lib/nuggets/string/capitalize_first.rb +63 -0
- data/lib/nuggets/string/case.rb +104 -0
- data/lib/nuggets/string/evaluate.rb +53 -0
- data/lib/nuggets/string/msub.rb +82 -0
- data/lib/nuggets/string/nsub.rb +80 -0
- data/lib/nuggets/string/sub_with_md.rb +131 -0
- data/lib/nuggets/string/word_wrap.rb +111 -0
- data/lib/nuggets/tempfile/open.rb +54 -0
- data/lib/nuggets/uri/content_type.rb +65 -0
- data/lib/nuggets/uri/exist.rb +63 -0
- data/lib/nuggets/util/added_methods/init.rb +3 -0
- data/lib/nuggets/util/added_methods.rb +407 -0
- data/lib/nuggets/util/ansicolor2css.rb +90 -0
- data/lib/nuggets/util/content_type.rb +104 -0
- data/lib/nuggets/util/dotted_decimal.rb +66 -0
- data/lib/nuggets/util/i18n.rb +143 -0
- data/lib/nuggets/version.rb +27 -0
- data/lib/nuggets.rb +73 -0
- metadata +124 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class File
|
29
|
+
|
30
|
+
class << self
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# File.which(executable) => aString or nil
|
34
|
+
#
|
35
|
+
# Returns the full path to +executable+, or +nil+ if not found in PATH.
|
36
|
+
# Inspired by Gnuplot.which -- thx, Gordon!
|
37
|
+
def which(executable)
|
38
|
+
return executable if executable?(executable)
|
39
|
+
|
40
|
+
if path = ENV['PATH']
|
41
|
+
path.split(PATH_SEPARATOR).each { |dir|
|
42
|
+
candidate = join(dir, executable)
|
43
|
+
return candidate if executable?(candidate)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
# call-seq:
|
51
|
+
# File.which_command(commands) => aString or nil
|
52
|
+
#
|
53
|
+
# Returns the first of +commands+ that is executable.
|
54
|
+
def which_command(commands)
|
55
|
+
commands.find { |command| which(command[/\S+/]) }
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
if $0 == __FILE__
|
63
|
+
%w[cat dog rat gcc /usr/bin/X11/gcc].each { |e|
|
64
|
+
p [e, File.which(e)]
|
65
|
+
}
|
66
|
+
|
67
|
+
c = [
|
68
|
+
'unison --args source target',
|
69
|
+
'rsync --args source target',
|
70
|
+
'scp --args source target'
|
71
|
+
]
|
72
|
+
p c
|
73
|
+
p File.which_command(c)
|
74
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'nuggets/array/rand'
|
29
|
+
|
30
|
+
class Hash
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# hash.at(what) => aHash
|
34
|
+
#
|
35
|
+
# Returns the key/value pair of _hash_ at key position +what+. Remember that
|
36
|
+
# hashes might not have the intended (or expected) order in pre-1.9 Ruby.
|
37
|
+
def at(what)
|
38
|
+
return {} if empty?
|
39
|
+
|
40
|
+
key = case what
|
41
|
+
when Integer
|
42
|
+
keys[what]
|
43
|
+
else
|
44
|
+
block_given? ? keys.send(*what) { |*a| yield(*a) } : keys.send(*what)
|
45
|
+
end
|
46
|
+
|
47
|
+
{ key => self[key] }
|
48
|
+
end
|
49
|
+
|
50
|
+
# call-seq:
|
51
|
+
# hash.first => aHash
|
52
|
+
#
|
53
|
+
# Returns the "first" key/value pair of _hash_.
|
54
|
+
def first
|
55
|
+
at(:first)
|
56
|
+
end
|
57
|
+
|
58
|
+
# call-seq:
|
59
|
+
# hash.last => aHash
|
60
|
+
#
|
61
|
+
# Returns the "last" key/value pair of _hash_.
|
62
|
+
def last
|
63
|
+
at(:last)
|
64
|
+
end
|
65
|
+
|
66
|
+
# call-seq:
|
67
|
+
# hash.rand => aHash
|
68
|
+
#
|
69
|
+
# Returns a random key/value pair of _hash_.
|
70
|
+
def rand
|
71
|
+
at(:rand)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
if $0 == __FILE__
|
77
|
+
h = { :a => 1, 2 => 3, nil => nil, 'foo' => %w[b a r]}
|
78
|
+
p h
|
79
|
+
|
80
|
+
p h.first
|
81
|
+
p h.last
|
82
|
+
p h.rand
|
83
|
+
|
84
|
+
p h.at(0)
|
85
|
+
p h.at(1)
|
86
|
+
p h.at(-1)
|
87
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'nuggets/array/in_order'
|
29
|
+
|
30
|
+
class Hash
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# hash.in_order(*ordered) => anArray
|
34
|
+
#
|
35
|
+
# Returns <tt>hash#to_a</tt>, in forced order (cf. Array#in_order).
|
36
|
+
#
|
37
|
+
# Examples:
|
38
|
+
# { :a => 1, :b => 2, :c => 3 }.in_order(:b, :c) #=> [[:b, 2], [:c, 3], [:a, 1]]
|
39
|
+
# { :a => 1, :b => 2, :c => 3 }.in_order(:b, :d) #=> [[:b, 2], [:a, 1], [:c, 3]]
|
40
|
+
def in_order(*ordered)
|
41
|
+
keys.in_order(*ordered).map { |key| [key, self[key]] }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
if $0 == __FILE__
|
47
|
+
a = { :a => 1, :b => 2, :c => 3 }
|
48
|
+
p a
|
49
|
+
|
50
|
+
p a.in_order(:b, :c)
|
51
|
+
p a.in_order(:b, :d)
|
52
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Hash
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# hash.insert(other) => new_hash
|
32
|
+
# hash.insert(other) { |key, old_value, new_value| ... } => new_hash
|
33
|
+
#
|
34
|
+
# Inserts +other+ into _hash_, while merging existing values instead of just
|
35
|
+
# overwriting. Uses default Hash#merge or block for merging.
|
36
|
+
def insert(other, &block)
|
37
|
+
block ||= lambda { |key, old_val, new_val|
|
38
|
+
old_val.is_a?(Hash) && new_val.is_a?(Hash) ?
|
39
|
+
old_val.merge(new_val, &block) : new_val
|
40
|
+
}
|
41
|
+
|
42
|
+
merge(other, &block)
|
43
|
+
end
|
44
|
+
|
45
|
+
# call-seq:
|
46
|
+
# hash.insert!(other) => hash
|
47
|
+
# hash.insert!(other) { |key, old_value, new_value| ... } => hash
|
48
|
+
#
|
49
|
+
# Destructive version of #insert.
|
50
|
+
def insert!(other)
|
51
|
+
replace block_given? ? insert(other) { |*a| yield(*a) } : insert(other)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
if $0 == __FILE__
|
57
|
+
h = { :a => 0, :b => { :b1 => 1, :b2 => 2 } }
|
58
|
+
p h
|
59
|
+
|
60
|
+
p h.insert(:a => -1)
|
61
|
+
p h.insert(:b => { :b3 => 3 })
|
62
|
+
|
63
|
+
h.insert!(:b => { :b0 => 0 })
|
64
|
+
p h
|
65
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'nuggets/hash/at'
|
29
|
+
|
30
|
+
class Hash
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# hash.only(relax = true or false) => aHash
|
34
|
+
#
|
35
|
+
# Returns the only key/value pair of _hash_. Raises an IndexError if _hash_'s
|
36
|
+
# size is not 1, unless +relax+ is true.
|
37
|
+
def only(relax = size == 1, split = false)
|
38
|
+
raise IndexError, 'not a single-element hash' unless relax
|
39
|
+
|
40
|
+
return *first if split
|
41
|
+
first
|
42
|
+
end
|
43
|
+
|
44
|
+
# call-seq:
|
45
|
+
# hash.only_pair(relax = true or false) => anArray
|
46
|
+
#
|
47
|
+
# Returns the only key/value pair of _hash_ as an array. Raises an IndexError
|
48
|
+
# if _hash_'s size is not 1, unless +relax+ is true.
|
49
|
+
def only_pair(relax = size == 1)
|
50
|
+
only(relax, true)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
if $0 == __FILE__
|
56
|
+
[{ :a => 5 }, { 1 => 2, 3 => 4 }, {}].each { |h|
|
57
|
+
p h
|
58
|
+
|
59
|
+
begin
|
60
|
+
p h.only
|
61
|
+
p h.only_pair
|
62
|
+
rescue IndexError => err
|
63
|
+
warn err
|
64
|
+
end
|
65
|
+
|
66
|
+
p h.only(true)
|
67
|
+
}
|
68
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Integer
|
29
|
+
|
30
|
+
# Memoization container: integer => factorial(integer)
|
31
|
+
FACTORIAL = { 0 => 1 }
|
32
|
+
|
33
|
+
# call-seq:
|
34
|
+
# int.factorial => anInteger
|
35
|
+
#
|
36
|
+
# Calculate the factorial of _int_. To use the memoized version:
|
37
|
+
# <tt>Integer.send(:alias_method, :factorial, :factorial_memoized)</tt>
|
38
|
+
def factorial
|
39
|
+
(1..self).inject { |f, i| f * i }
|
40
|
+
end
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# int.factorial_memoized => anInteger
|
44
|
+
#
|
45
|
+
# Calculate the factorial of _int_ with the help of memoization (Which gives
|
46
|
+
# a considerable speedup for repeated calculations -- at the cost of memory).
|
47
|
+
#
|
48
|
+
# WARNING: Don't try to calculate the factorial this way for "large"
|
49
|
+
# integers! This might well bring your system down to its knees... ;-)
|
50
|
+
def factorial_memoized
|
51
|
+
FACTORIAL[self] ||= (1..self).inject { |f, i| FACTORIAL[i] ||= f * i }
|
52
|
+
end
|
53
|
+
|
54
|
+
alias_method :fac, :factorial
|
55
|
+
alias_method :f!, :factorial
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
if $0 == __FILE__
|
60
|
+
1.upto(8) { |i|
|
61
|
+
puts "#{i}: #{i.factorial}"
|
62
|
+
}
|
63
|
+
|
64
|
+
require 'benchmark'
|
65
|
+
|
66
|
+
Benchmark.bm(19) { |x|
|
67
|
+
[20000, 800, 300, 700, 130, 480, 9999, 9999, 25000].each { |i|
|
68
|
+
puts "#{i}:"
|
69
|
+
|
70
|
+
x.report('factorial') { i.factorial }
|
71
|
+
x.report('factorial_memoized') { i.factorial_memoized }
|
72
|
+
}
|
73
|
+
}
|
74
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Integer
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# int.to_binary_s => aString
|
32
|
+
# int.to_binary_s(length) => aString
|
33
|
+
#
|
34
|
+
# Returns _int_ as binary number string; optionally zero-padded to +length+.
|
35
|
+
def to_binary_s(length = nil)
|
36
|
+
"%0#{length}d" % to_s(2)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
if $0 == __FILE__
|
42
|
+
[20000, 800, 300, 700, 130, 480, 9999, 9999, 25000].each { |i|
|
43
|
+
p i
|
44
|
+
p i.to_binary_s
|
45
|
+
p i.to_binary_s(32)
|
46
|
+
}
|
47
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
require 'nuggets/enumerable/agrep'
|
29
|
+
|
30
|
+
class IO
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# IO.agrep(fd, pattern[, distance]) => anArray
|
34
|
+
#
|
35
|
+
def self.agrep(fd, pattern, distance)
|
36
|
+
open(fd) { |io| io.agrep(pattern, distance) }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
if $0 == __FILE__
|
42
|
+
puts File.agrep(__FILE__, /calls/, 2)
|
43
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class IO
|
29
|
+
|
30
|
+
# Inspired by Martin DeMello, [ruby-talk:307782] -- thx ;-)
|
31
|
+
|
32
|
+
class << self
|
33
|
+
|
34
|
+
alias_method :_nuggets_original_read, :read
|
35
|
+
|
36
|
+
# call-seq:
|
37
|
+
# IO.read(name, [length [, offset]]) => aString
|
38
|
+
# IO.read(name, binary = false) { |io| ... } => anObject
|
39
|
+
#
|
40
|
+
# Opens +name+ with mode +r+. NOTE: With no associated block,
|
41
|
+
# acts like the original IO::read, not like IO::new.
|
42
|
+
def read(name, *args)
|
43
|
+
return _nuggets_original_read(name, *args) unless block_given?
|
44
|
+
|
45
|
+
case args.size
|
46
|
+
when 0
|
47
|
+
# ok
|
48
|
+
when 1
|
49
|
+
case binary = args.first
|
50
|
+
when true, false, nil
|
51
|
+
# ok
|
52
|
+
else
|
53
|
+
raise TypeError, "wrong argument type #{binary.class} (expected boolean)"
|
54
|
+
end
|
55
|
+
else
|
56
|
+
raise ArgumentError, "wrong number of arguments (#{args.size + 1} for 1-2)"
|
57
|
+
end
|
58
|
+
|
59
|
+
open_with_mode(name, 'r', binary) { |*a| yield(*a) }
|
60
|
+
end
|
61
|
+
|
62
|
+
# call-seq:
|
63
|
+
# IO.write(name, binary = false) => anIO
|
64
|
+
# IO.write(name, binary = false) { |io| ... } => anObject
|
65
|
+
#
|
66
|
+
# Opens +name+ with mode +w+.
|
67
|
+
def write(name, binary = false)
|
68
|
+
block_given? ?
|
69
|
+
open_with_mode(name, 'w', binary) { |*a| yield(*a) } :
|
70
|
+
open_with_mode(name, 'w', binary)
|
71
|
+
end
|
72
|
+
|
73
|
+
# call-seq:
|
74
|
+
# IO.append(name, binary = false) => anIO
|
75
|
+
# IO.append(name, binary = false) { |io| ... } => anObject
|
76
|
+
#
|
77
|
+
# Opens +name+ with mode +a+.
|
78
|
+
def append(name, binary = false)
|
79
|
+
block_given? ?
|
80
|
+
open_with_mode(name, 'a', binary) { |*a| yield(*a) } :
|
81
|
+
open_with_mode(name, 'a', binary)
|
82
|
+
end
|
83
|
+
|
84
|
+
# call-seq:
|
85
|
+
# IO.read_write(name, binary = false) => anIO
|
86
|
+
# IO.read_write(name, binary = false) { |io| ... } => anObject
|
87
|
+
#
|
88
|
+
# Opens +name+ with mode <tt>r+</tt>.
|
89
|
+
def read_write(name, binary = false)
|
90
|
+
block_given? ?
|
91
|
+
open_with_mode(name, 'r+', binary) { |*a| yield(*a) } :
|
92
|
+
open_with_mode(name, 'r+', binary)
|
93
|
+
end
|
94
|
+
|
95
|
+
# call-seq:
|
96
|
+
# IO.write_read(name, binary = false) => anIO
|
97
|
+
# IO.write_read(name, binary = false) { |io| ... } => anObject
|
98
|
+
#
|
99
|
+
# Opens +name+ with mode <tt>w+</tt>.
|
100
|
+
def write_read(name, binary = false)
|
101
|
+
block_given? ?
|
102
|
+
open_with_mode(name, 'w+', binary) { |*a| yield(*a) } :
|
103
|
+
open_with_mode(name, 'w+', binary)
|
104
|
+
end
|
105
|
+
|
106
|
+
# call-seq:
|
107
|
+
# IO.append_read(name, binary = false) => anIO
|
108
|
+
# IO.append_read(name, binary = false) { |io| ... } => anObject
|
109
|
+
#
|
110
|
+
# Opens +name+ with mode <tt>a+</tt>.
|
111
|
+
def append_read(name, binary = false)
|
112
|
+
block_given? ?
|
113
|
+
open_with_mode(name, 'a+', binary) { |*a| yield(*a) } :
|
114
|
+
open_with_mode(name, 'a+', binary)
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
# Just a helper to DRY things up.
|
120
|
+
def open_with_mode(name, mode, binary = false)
|
121
|
+
mode << 'b' if binary
|
122
|
+
block_given? ? open(name, mode) { |io| yield io } : open(name, mode)
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
if $0 == __FILE__
|
130
|
+
# File.read(__FILE__) { |f| ... }
|
131
|
+
# File.write(__FILE__) { |f| ... }
|
132
|
+
# File.append(__FILE__) { |f| ... }
|
133
|
+
end
|