ruby-nuggets 0.2.2.247 → 0.2.3.253
Sign up to get free protection for your applications and to get access to all the features.
- data/HEADER +27 -0
- data/README +1 -1
- data/lib/nuggets/enumerable/all_any_extended.rb +4 -4
- data/lib/nuggets/enumerable/minmax.rb +6 -6
- data/lib/nuggets/io/modes.rb +2 -2
- data/lib/nuggets/numeric/between.rb +2 -47
- data/lib/nuggets/numeric/limit.rb +70 -0
- data/lib/nuggets/object/msend.rb +57 -0
- data/lib/nuggets/util/added_methods/init.rb +3 -0
- data/lib/nuggets/util/added_methods.rb +302 -0
- data/lib/nuggets/version.rb +1 -1
- metadata +39 -34
data/HEADER
ADDED
@@ -0,0 +1,27 @@
|
|
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
|
+
|
data/README
CHANGED
@@ -27,8 +27,8 @@
|
|
27
27
|
|
28
28
|
module Enumerable
|
29
29
|
|
30
|
-
alias_method :
|
31
|
-
alias_method :
|
30
|
+
alias_method :_nuggets_original_all?, :all?
|
31
|
+
alias_method :_nuggets_original_any?, :any?
|
32
32
|
|
33
33
|
# call-seq:
|
34
34
|
# enum.all?(obj[, operator]) => true or false
|
@@ -38,7 +38,7 @@ module Enumerable
|
|
38
38
|
# be tested against each item in _enum_ according to +operator+, defaulting
|
39
39
|
# to :===.
|
40
40
|
def all?(object = default = Object.new, operator = :===, &block)
|
41
|
-
|
41
|
+
_nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, block))
|
42
42
|
end
|
43
43
|
|
44
44
|
# call-seq:
|
@@ -49,7 +49,7 @@ module Enumerable
|
|
49
49
|
# be tested against each item in _enum_ according to +operator+, defaulting
|
50
50
|
# to :===.
|
51
51
|
def any?(object = default = Object.new, operator = :===, &block)
|
52
|
-
|
52
|
+
_nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, block))
|
53
53
|
end
|
54
54
|
|
55
55
|
private
|
@@ -27,8 +27,8 @@
|
|
27
27
|
|
28
28
|
module Enumerable
|
29
29
|
|
30
|
-
alias_method :
|
31
|
-
alias_method :
|
30
|
+
alias_method :_nuggets_original_max, :max
|
31
|
+
alias_method :_nuggets_original_min, :min
|
32
32
|
|
33
33
|
# call-seq:
|
34
34
|
# enum.minmax_by(meth, by) => aValue
|
@@ -81,18 +81,18 @@ module Enumerable
|
|
81
81
|
# enum.max(what) => aValue
|
82
82
|
#
|
83
83
|
# Maximum #minmax. If +what+ is omitted, or nil, the original Enumerable#max
|
84
|
-
# is called
|
84
|
+
# is called.
|
85
85
|
def max(what = nil, &block)
|
86
|
-
what ? minmax(:max, what) :
|
86
|
+
what ? minmax(:max, what) : _nuggets_original_max(&block)
|
87
87
|
end
|
88
88
|
|
89
89
|
# call-seq:
|
90
90
|
# enum.min(what) => aValue
|
91
91
|
#
|
92
92
|
# Minimum #minmax. If +what+ is omitted, or nil, the original Enumerable#min
|
93
|
-
# is called
|
93
|
+
# is called.
|
94
94
|
def min(what = nil, &block)
|
95
|
-
what ? minmax(:min, what) :
|
95
|
+
what ? minmax(:min, what) : _nuggets_original_min(&block)
|
96
96
|
end
|
97
97
|
|
98
98
|
end
|
data/lib/nuggets/io/modes.rb
CHANGED
@@ -31,7 +31,7 @@ class IO
|
|
31
31
|
|
32
32
|
class << self
|
33
33
|
|
34
|
-
alias_method :
|
34
|
+
alias_method :_nuggets_original_read, :read
|
35
35
|
|
36
36
|
# call-seq:
|
37
37
|
# IO.read(name, [length [, offset]]) => aString
|
@@ -40,7 +40,7 @@ class IO
|
|
40
40
|
# Opens +name+ with mode +r+. NOTE: With no associated block,
|
41
41
|
# acts like the original IO::read, not like IO::new.
|
42
42
|
def read(name, *args, &block)
|
43
|
-
return
|
43
|
+
return _nuggets_original_read(name, *args) unless block
|
44
44
|
|
45
45
|
case args.size
|
46
46
|
when 0
|
@@ -1,47 +1,2 @@
|
|
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 Numeric
|
29
|
-
|
30
|
-
# call-seq:
|
31
|
-
# num.between(min, max) => aNumeric
|
32
|
-
#
|
33
|
-
# Cuts _num_ to the (inclusive) range of +min+ to +max+.
|
34
|
-
def between(min, max)
|
35
|
-
min, max = max, min if max < min
|
36
|
-
|
37
|
-
self < min ? min : self > max ? max : self
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
if $0 == __FILE__
|
43
|
-
[123, -123, 0, 0.001, 1.23, -12.3].each { |n|
|
44
|
-
p n
|
45
|
-
p n.between(0, 10)
|
46
|
-
}
|
47
|
-
end
|
1
|
+
# backwards compatibility
|
2
|
+
require File.join(File.dirname(__FILE__), 'limit')
|
@@ -0,0 +1,70 @@
|
|
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 Numeric
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# num.limit(min, max) => aNumeric
|
32
|
+
#
|
33
|
+
# Returns +min+ if that's larger than _num_, or +max+ if that's smaller than
|
34
|
+
# _num_. Otherwise returns _num_.
|
35
|
+
def limit(min, max)
|
36
|
+
min, max = max, min if max < min
|
37
|
+
|
38
|
+
self.min(min).max(max)
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :between, :limit
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# num.min(min) => aNumeric
|
45
|
+
#
|
46
|
+
# Returns _num_ or +min+, whatever is larger.
|
47
|
+
def min(min)
|
48
|
+
self < min ? min : self
|
49
|
+
end
|
50
|
+
|
51
|
+
alias_method :at_least, :min
|
52
|
+
|
53
|
+
# call-seq:
|
54
|
+
# num.max(max) => aNumeric
|
55
|
+
#
|
56
|
+
# Returns _num_ or +max+, whatever is smaller.
|
57
|
+
def max(max)
|
58
|
+
self > max ? max : self
|
59
|
+
end
|
60
|
+
|
61
|
+
alias_method :at_most, :max
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
if $0 == __FILE__
|
66
|
+
[123, -123, 0, 0.001, 1.23, -12.3].each { |n|
|
67
|
+
p n
|
68
|
+
p n.between(0, 10)
|
69
|
+
}
|
70
|
+
end
|
@@ -0,0 +1,57 @@
|
|
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 Object
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# object.msend(*messages) => anArray
|
32
|
+
#
|
33
|
+
# Sends _object_ multiple +messages+ and returns an array of the individual
|
34
|
+
# return values.
|
35
|
+
def msend(*messages)
|
36
|
+
messages_with_args = messages.last.is_a?(Hash) ? messages.pop : {}
|
37
|
+
|
38
|
+
(messages + messages_with_args.keys).map { |msg|
|
39
|
+
messages_with_args.has_key?(msg) ? send(msg, *messages_with_args[msg]) : send(msg)
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
if $0 == __FILE__
|
46
|
+
o = 'foo bar'
|
47
|
+
p o
|
48
|
+
p o.msend(:length, :reverse)
|
49
|
+
|
50
|
+
o = 42
|
51
|
+
p o
|
52
|
+
p o.msend(:to_s, :* => 2)
|
53
|
+
|
54
|
+
o = Time.now
|
55
|
+
p o
|
56
|
+
p o.msend(:year, :month, :day)
|
57
|
+
end
|
@@ -0,0 +1,302 @@
|
|
1
|
+
# watch for added methods and record them
|
2
|
+
# cf. <http://unroller.rubyforge.org/classes/Unroller.html#M000034>
|
3
|
+
|
4
|
+
# TODO:
|
5
|
+
# - multi-line statements in irb (extract_source)
|
6
|
+
# - polishing!
|
7
|
+
|
8
|
+
module AddedMethods
|
9
|
+
|
10
|
+
extend self
|
11
|
+
|
12
|
+
HISTFILENAME = '(Readline::HISTORY)'.freeze
|
13
|
+
|
14
|
+
class AddedMethod < Hash
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
update(*args) unless args.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def extract_source(num_lines = nil)
|
21
|
+
return unless Object.const_defined?(:SCRIPT_LINES__)
|
22
|
+
return unless script_lines = SCRIPT_LINES__[file]
|
23
|
+
|
24
|
+
start, from, to = line - 1, line, script_lines.size - 1
|
25
|
+
|
26
|
+
# suppose we're already in a block
|
27
|
+
in_block = 1
|
28
|
+
|
29
|
+
num_lines ||= case definition = script_lines[start]
|
30
|
+
# def ... end, or do ... end style block
|
31
|
+
when /\b(?:def|do)\b/
|
32
|
+
definition =~ /\bend\b/ ? 1 : begin
|
33
|
+
from.upto(to) { |i|
|
34
|
+
case line = script_lines[i]
|
35
|
+
when /[^;\s]\s+(?:if|unless)\b/
|
36
|
+
# probably postfix conditional, ignore
|
37
|
+
when /\b(?:if|unless|while|until|def|do)\b/
|
38
|
+
in_block += 1
|
39
|
+
when /\bend\b/
|
40
|
+
in_block -= 1
|
41
|
+
end
|
42
|
+
|
43
|
+
break i - start + 1 if in_block.zero?
|
44
|
+
}
|
45
|
+
end
|
46
|
+
# { ... } style block
|
47
|
+
when /\bdefine_method\b/
|
48
|
+
from.upto(to) { |i|
|
49
|
+
line = script_lines[i]
|
50
|
+
|
51
|
+
in_block += line.count('{')
|
52
|
+
in_block -= line.count('}')
|
53
|
+
|
54
|
+
break i - start + 1 if in_block.zero?
|
55
|
+
}
|
56
|
+
else
|
57
|
+
1
|
58
|
+
end
|
59
|
+
|
60
|
+
script_lines[start, num_lines].map { |l| " #{l}" }
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s(num_lines = nil)
|
64
|
+
"# File #{file}, line #{line}\n#{extract_source(num_lines)}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def klass
|
68
|
+
self[:class]
|
69
|
+
end
|
70
|
+
|
71
|
+
def method_missing(method, *args)
|
72
|
+
has_key?(method) ? self[method] : super
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def init(regexp = nil, klasses = [], &block)
|
78
|
+
init_script_lines
|
79
|
+
patch_readline_history
|
80
|
+
|
81
|
+
define_callback(:__init, regexp, klasses, &block) if regexp
|
82
|
+
install_callbacks
|
83
|
+
end
|
84
|
+
|
85
|
+
def callbacks
|
86
|
+
init_callbacks
|
87
|
+
CALLBACKS
|
88
|
+
end
|
89
|
+
|
90
|
+
def callback(*args, &inner_block)
|
91
|
+
callback_args = [identify_added_method(*args << caller), caller, inner_block]
|
92
|
+
callbacks.each { |name, callback| callback[*callback_args] }
|
93
|
+
end
|
94
|
+
|
95
|
+
def define_callback(name, regexp = //, klasses = [], &outer_block)
|
96
|
+
raise TypeError, "wrong argument type #{name.class} (expected Symbol)" unless name.is_a?(Symbol)
|
97
|
+
raise "callback with name #{name} already exists" if callbacks.any? { |n, _| n == name }
|
98
|
+
|
99
|
+
raise TypeError, "wrong argument type #{regexp.class} (expected Regexp)" unless regexp.is_a?(Regexp)
|
100
|
+
raise TypeError, "wrong argument type #{klasses.class} (expected container object)" unless klasses.respond_to?(:empty?) && klasses.respond_to?(:include?)
|
101
|
+
|
102
|
+
callbacks << [name, lambda { |am, callstack, inner_block|
|
103
|
+
method, klass = am.values_at(:name, :class)
|
104
|
+
|
105
|
+
return if %w[method_added singleton_method_added].include?(method)
|
106
|
+
|
107
|
+
return unless klasses.empty? || klasses.include?(klass.to_s)
|
108
|
+
return unless method =~ regexp
|
109
|
+
|
110
|
+
if outer_block || inner_block
|
111
|
+
outer_block[am] if outer_block
|
112
|
+
inner_block[am] if inner_block
|
113
|
+
else
|
114
|
+
msg = "[#{am.base}] Adding #{'singleton ' if am.singleton}method #{klass}##{method}"
|
115
|
+
|
116
|
+
msg << if irb?(callstack)
|
117
|
+
" in (irb:#{IRB.conf[:MAIN_CONTEXT].instance_variable_get(:@line_no)})"
|
118
|
+
else
|
119
|
+
" at #{where(callstack)}"
|
120
|
+
end
|
121
|
+
|
122
|
+
puts msg
|
123
|
+
end
|
124
|
+
}]
|
125
|
+
end
|
126
|
+
|
127
|
+
def remove_callback(name)
|
128
|
+
callbacks.delete_if { |n, _| n == name }
|
129
|
+
end
|
130
|
+
|
131
|
+
def replace_callback(name, regexp = nil, klasses = [], &outer_block)
|
132
|
+
remove_callback(name)
|
133
|
+
define_callback(name, regexp, klasses, &outer_block)
|
134
|
+
end
|
135
|
+
|
136
|
+
def install_callbacks(bases = [Object, Class, Module, Kernel])
|
137
|
+
bases.each { |base|
|
138
|
+
[base, singleton_class(base)].each { |b|
|
139
|
+
b.send(:define_method, :method_added) { |id| AddedMethods.callback(b, self, id, false) }
|
140
|
+
b.send(:define_method, :singleton_method_added) { |id| AddedMethods.callback(b, self, id, true) }
|
141
|
+
}
|
142
|
+
}
|
143
|
+
end
|
144
|
+
|
145
|
+
def all_methods
|
146
|
+
init_all_methods
|
147
|
+
ALL_METHODS
|
148
|
+
end
|
149
|
+
|
150
|
+
def find(conditions = {})
|
151
|
+
conditions = conditions.dup
|
152
|
+
|
153
|
+
class_condition = conditions.delete(:class)
|
154
|
+
file_condition = conditions.delete(:file)
|
155
|
+
|
156
|
+
results = []
|
157
|
+
|
158
|
+
all_methods.each { |klass, files|
|
159
|
+
if class_condition
|
160
|
+
next unless class_condition.is_a?(Array) ? class_condition.include?(klass) : klass == class_condition
|
161
|
+
end
|
162
|
+
|
163
|
+
files.each { |file, entries|
|
164
|
+
if file_condition
|
165
|
+
next unless file_condition.is_a?(Regexp) ? file =~ file_condition : file == file_condition
|
166
|
+
end
|
167
|
+
|
168
|
+
entries.each { |entry|
|
169
|
+
results << entry.merge(
|
170
|
+
:class => klass,
|
171
|
+
:file => file
|
172
|
+
) if conditions.all? { |key, value|
|
173
|
+
case value
|
174
|
+
when Array: value.include?(entry[key])
|
175
|
+
when Regexp: entry[key].to_s =~ value
|
176
|
+
else entry[key] == value
|
177
|
+
end
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
results
|
184
|
+
end
|
185
|
+
|
186
|
+
def find_by_class(*classes)
|
187
|
+
conditions = classes.last.is_a?(Hash) ? classes.pop : {}
|
188
|
+
find(conditions.merge(:class => classes))
|
189
|
+
end
|
190
|
+
|
191
|
+
def find_by_name(*names)
|
192
|
+
conditions = names.last.is_a?(Hash) ? names.pop : {}
|
193
|
+
names.inject([]) { |memo, name|
|
194
|
+
memo += find(conditions.merge(:name => name.to_s))
|
195
|
+
}
|
196
|
+
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def singleton_class(klass = self)
|
201
|
+
class << klass; self; end
|
202
|
+
end
|
203
|
+
|
204
|
+
def init_script_lines
|
205
|
+
unless Object.const_defined?(:SCRIPT_LINES__)
|
206
|
+
Object.const_set(:SCRIPT_LINES__, {})
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def init_callbacks
|
211
|
+
unless const_defined?(:CALLBACKS)
|
212
|
+
const_set(:CALLBACKS, [])
|
213
|
+
define_callback(:__default, //, [], &added_method_callback)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def init_all_methods
|
218
|
+
unless const_defined?(:ALL_METHODS)
|
219
|
+
const_set(:ALL_METHODS, Hash.new { |h, k|
|
220
|
+
h[k] = Hash.new { |i, j|
|
221
|
+
i[j] = []
|
222
|
+
}
|
223
|
+
})
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
def patch_readline_history
|
228
|
+
return unless have_readline_history?
|
229
|
+
return if Readline::HISTORY.respond_to?(:_added_methods_original_push)
|
230
|
+
|
231
|
+
class << Readline::HISTORY
|
232
|
+
alias_method :_added_methods_original_push, :push
|
233
|
+
|
234
|
+
def push(l)
|
235
|
+
(SCRIPT_LINES__[HISTFILENAME] ||= Readline::HISTORY.to_a) << l
|
236
|
+
_added_methods_original_push(l)
|
237
|
+
end
|
238
|
+
|
239
|
+
alias_method :<<, :push
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def have_readline_history?
|
244
|
+
Object.const_defined?(:Readline) && Readline.const_defined?(:HISTORY)
|
245
|
+
end
|
246
|
+
|
247
|
+
def defined_in_irb?(callstack)
|
248
|
+
callstack = callstack.dup
|
249
|
+
|
250
|
+
callstack.shift # ignore immediate caller
|
251
|
+
callstack.reject! { |c| c =~ /\(irb\):|:in `irb_binding'/ }
|
252
|
+
callstack.pop if callstack.last =~ %r{/irb/workspace\.rb:}
|
253
|
+
|
254
|
+
callstack.empty?
|
255
|
+
end
|
256
|
+
|
257
|
+
def irb?(callstack)
|
258
|
+
have_readline_history? && defined_in_irb?(callstack)
|
259
|
+
end
|
260
|
+
|
261
|
+
def where(callstack, default = '(none):0')
|
262
|
+
callstack.find { |i| i !~ /:in `.*'/ } || callstack[1] || default
|
263
|
+
end
|
264
|
+
|
265
|
+
def added_method_callback
|
266
|
+
lambda { |am| add_method(am) }
|
267
|
+
end
|
268
|
+
|
269
|
+
def add_method(am)
|
270
|
+
am = AddedMethod.new(am) unless am.is_a?(AddedMethod)
|
271
|
+
all_methods[am.klass][am.file] << am
|
272
|
+
end
|
273
|
+
|
274
|
+
def identify_added_method(base, klass, id, singleton, callstack)
|
275
|
+
am = {
|
276
|
+
:base => base,
|
277
|
+
:class => klass,
|
278
|
+
:name => id.id2name,
|
279
|
+
:singleton => singleton
|
280
|
+
}
|
281
|
+
|
282
|
+
if irb?(callstack)
|
283
|
+
am.update(
|
284
|
+
:file => HISTFILENAME,
|
285
|
+
:line => Readline::HISTORY.size,
|
286
|
+
:source => Readline::HISTORY[-1]
|
287
|
+
)
|
288
|
+
else
|
289
|
+
file, line, _ = where(callstack).split(':')
|
290
|
+
line = line.to_i
|
291
|
+
|
292
|
+
am.update(
|
293
|
+
:file => file,
|
294
|
+
:line => line,
|
295
|
+
:source => (SCRIPT_LINES__[file] || [])[line - 1]
|
296
|
+
)
|
297
|
+
end
|
298
|
+
|
299
|
+
AddedMethod.new(am)
|
300
|
+
end
|
301
|
+
|
302
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3.253
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-11 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,60 +24,65 @@ extra_rdoc_files:
|
|
24
24
|
- ChangeLog
|
25
25
|
- README
|
26
26
|
files:
|
27
|
-
- lib/nuggets/all.rb
|
28
|
-
- lib/nuggets/version.rb
|
29
|
-
- lib/nuggets/io/agrep.rb
|
30
|
-
- lib/nuggets/io/modes.rb
|
31
|
-
- lib/nuggets/file/which.rb
|
32
|
-
- lib/nuggets/integer/factorial.rb
|
33
27
|
- lib/nuggets/integer/to_binary_s.rb
|
34
|
-
- lib/nuggets/
|
35
|
-
- lib/nuggets/
|
36
|
-
- lib/nuggets/
|
37
|
-
- lib/nuggets/
|
38
|
-
- lib/nuggets/
|
39
|
-
- lib/nuggets/
|
40
|
-
- lib/nuggets/
|
41
|
-
- lib/nuggets/
|
42
|
-
- lib/nuggets/
|
43
|
-
- lib/nuggets/
|
44
|
-
- lib/nuggets/proc/bind.rb
|
45
|
-
- lib/nuggets/string/word_wrap.rb
|
28
|
+
- lib/nuggets/integer/factorial.rb
|
29
|
+
- lib/nuggets/version.rb
|
30
|
+
- lib/nuggets/object/singleton_class.rb
|
31
|
+
- lib/nuggets/object/blank.rb
|
32
|
+
- lib/nuggets/object/msend.rb
|
33
|
+
- lib/nuggets/enumerable/agrep.rb
|
34
|
+
- lib/nuggets/enumerable/all_any_extended.rb
|
35
|
+
- lib/nuggets/enumerable/minmax.rb
|
36
|
+
- lib/nuggets/all.rb
|
37
|
+
- lib/nuggets/string/sub_with_md.rb
|
46
38
|
- lib/nuggets/string/msub.rb
|
47
39
|
- lib/nuggets/string/case.rb
|
48
|
-
- lib/nuggets/string/sub_with_md.rb
|
49
40
|
- lib/nuggets/string/evaluate.rb
|
41
|
+
- lib/nuggets/string/word_wrap.rb
|
50
42
|
- lib/nuggets/string/nsub.rb
|
51
43
|
- lib/nuggets/string/capitalize_first.rb
|
52
|
-
- lib/nuggets/
|
53
|
-
- lib/nuggets/
|
44
|
+
- lib/nuggets/hash/in_order.rb
|
45
|
+
- lib/nuggets/hash/insert.rb
|
46
|
+
- lib/nuggets/proc/bind.rb
|
47
|
+
- lib/nuggets/array/rand.rb
|
48
|
+
- lib/nuggets/array/to_hash.rb
|
49
|
+
- lib/nuggets/array/flatten_once.rb
|
50
|
+
- lib/nuggets/array/in_order.rb
|
51
|
+
- lib/nuggets/array/shuffle.rb
|
52
|
+
- lib/nuggets/array/monotone.rb
|
53
|
+
- lib/nuggets/array/format.rb
|
54
|
+
- lib/nuggets/array/combination.rb
|
54
55
|
- lib/nuggets/numeric/between.rb
|
55
56
|
- lib/nuggets/numeric/signum.rb
|
57
|
+
- lib/nuggets/numeric/limit.rb
|
56
58
|
- lib/nuggets/numeric/to_multiple.rb
|
57
|
-
- lib/nuggets/util/
|
58
|
-
- lib/nuggets/util/
|
59
|
+
- lib/nuggets/util/added_methods/init.rb
|
60
|
+
- lib/nuggets/util/added_methods.rb
|
59
61
|
- lib/nuggets/util/ansicolor2css.rb
|
60
62
|
- lib/nuggets/util/content_type.rb
|
61
|
-
- lib/nuggets/
|
62
|
-
- lib/nuggets/
|
63
|
-
- lib/nuggets/
|
64
|
-
- lib/nuggets/
|
65
|
-
- lib/nuggets/
|
63
|
+
- lib/nuggets/util/dotted_decimal.rb
|
64
|
+
- lib/nuggets/util/i18n.rb
|
65
|
+
- lib/nuggets/file/which.rb
|
66
|
+
- lib/nuggets/uri/content_type.rb
|
67
|
+
- lib/nuggets/uri/exist.rb
|
68
|
+
- lib/nuggets/io/modes.rb
|
69
|
+
- lib/nuggets/io/agrep.rb
|
66
70
|
- COPYING
|
67
|
-
-
|
71
|
+
- HEADER
|
68
72
|
- README
|
69
73
|
- ChangeLog
|
74
|
+
- Rakefile
|
70
75
|
has_rdoc: true
|
71
76
|
homepage: http://prometheus.rubyforge.org/ruby-nuggets
|
72
77
|
post_install_message:
|
73
78
|
rdoc_options:
|
74
|
-
- --all
|
75
|
-
- --main
|
76
|
-
- README
|
77
79
|
- --line-numbers
|
78
80
|
- --inline-source
|
79
81
|
- --title
|
80
82
|
- ruby-nuggets Application documentation
|
83
|
+
- --main
|
84
|
+
- README
|
85
|
+
- --all
|
81
86
|
- --charset
|
82
87
|
- UTF-8
|
83
88
|
require_paths:
|