sentry-raven 3.0.2 → 3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.craft.yml +9 -5
- data/.scripts/bump-version.rb +5 -0
- data/{changelog.md → CHANGELOG.md} +165 -8
- data/Gemfile +10 -3
- data/Makefile +3 -0
- data/README.md +42 -15
- data/lib/raven/backtrace.rb +2 -0
- data/lib/raven/base.rb +3 -0
- data/lib/raven/breadcrumbs/active_support_logger.rb +25 -0
- data/lib/raven/breadcrumbs/logger.rb +2 -92
- data/lib/raven/breadcrumbs/sentry_logger.rb +73 -0
- data/lib/raven/cli.rb +8 -19
- data/lib/raven/client.rb +1 -1
- data/lib/raven/configuration.rb +80 -5
- data/lib/raven/context.rb +13 -8
- data/lib/raven/core_ext/object/deep_dup.rb +57 -0
- data/lib/raven/core_ext/object/duplicable.rb +153 -0
- data/lib/raven/event.rb +27 -13
- data/lib/raven/helpers/deprecation_helper.rb +17 -0
- data/lib/raven/instance.rb +17 -2
- data/lib/raven/integrations/delayed_job.rb +2 -1
- data/lib/raven/integrations/rack-timeout.rb +5 -1
- data/lib/raven/integrations/rack.rb +5 -4
- data/lib/raven/integrations/rails.rb +12 -3
- data/lib/raven/integrations/rails/active_job.rb +2 -1
- data/lib/raven/integrations/rails/backtrace_cleaner.rb +29 -0
- data/lib/raven/integrations/sidekiq.rb +4 -78
- data/lib/raven/integrations/sidekiq/cleanup_middleware.rb +13 -0
- data/lib/raven/integrations/sidekiq/error_handler.rb +38 -0
- data/lib/raven/processor/cookies.rb +1 -1
- data/lib/raven/processor/removecircularreferences.rb +2 -1
- data/lib/raven/transports/http.rb +2 -3
- data/lib/raven/utils/context_filter.rb +42 -0
- data/lib/raven/utils/request_id.rb +16 -0
- data/lib/raven/version.rb +1 -1
- data/lib/sentry-raven-without-integrations.rb +6 -1
- data/lib/sentry_raven_without_integrations.rb +1 -0
- data/sentry-raven.gemspec +7 -0
- metadata +21 -11
- data/.github/workflows/test.yml +0 -77
- data/.gitignore +0 -15
- data/.gitmodules +0 -0
- data/.rspec +0 -1
- data/.rubocop.yml +0 -109
- data/.scripts/bump-version.sh +0 -9
- data/lib/raven/breadcrumbs/activesupport.rb +0 -19
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Raven
|
4
|
+
module Breadcrumbs
|
5
|
+
module SentryLogger
|
6
|
+
LEVELS = {
|
7
|
+
::Logger::DEBUG => 'debug',
|
8
|
+
::Logger::INFO => 'info',
|
9
|
+
::Logger::WARN => 'warn',
|
10
|
+
::Logger::ERROR => 'error',
|
11
|
+
::Logger::FATAL => 'fatal'
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def add(*args)
|
15
|
+
add_breadcrumb(*args)
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_breadcrumb(severity, message = nil, progname = nil)
|
20
|
+
message = progname if message.nil? # see Ruby's Logger docs for why
|
21
|
+
return if ignored_logger?(progname)
|
22
|
+
return if message.nil? || message == ""
|
23
|
+
|
24
|
+
# some loggers will add leading/trailing space as they (incorrectly, mind you)
|
25
|
+
# think of logging as a shortcut to std{out,err}
|
26
|
+
message = message.to_s.strip
|
27
|
+
|
28
|
+
last_crumb = Raven.breadcrumbs.peek
|
29
|
+
# try to avoid dupes from logger broadcasts
|
30
|
+
if last_crumb.nil? || last_crumb.message != message
|
31
|
+
Raven.breadcrumbs.record do |crumb|
|
32
|
+
crumb.level = Raven::Breadcrumbs::SentryLogger::LEVELS.fetch(severity, nil)
|
33
|
+
crumb.category = progname || 'logger'
|
34
|
+
crumb.message = message
|
35
|
+
crumb.type =
|
36
|
+
if severity >= 3
|
37
|
+
"error"
|
38
|
+
else
|
39
|
+
crumb.level
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def ignored_logger?(progname)
|
48
|
+
progname == "sentry" ||
|
49
|
+
Raven.configuration.exclude_loggers.include?(progname)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
module OldBreadcrumbsSentryLogger
|
53
|
+
def self.included(base)
|
54
|
+
base.class_eval do
|
55
|
+
include Raven::Breadcrumbs::SentryLogger
|
56
|
+
alias_method :add_without_raven, :add
|
57
|
+
alias_method :add, :add_with_raven
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def add_with_raven(*args)
|
62
|
+
add_breadcrumb(*args)
|
63
|
+
add_without_raven(*args)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
Raven.safely_prepend(
|
70
|
+
"Breadcrumbs::SentryLogger",
|
71
|
+
:from => Raven,
|
72
|
+
:to => ::Logger
|
73
|
+
)
|
data/lib/raven/cli.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Raven
|
2
2
|
class CLI
|
3
|
-
def self.test(dsn = nil, silent = false, config = nil)
|
3
|
+
def self.test(dsn = nil, silent = false, config = nil)
|
4
4
|
config ||= Raven.configuration
|
5
5
|
|
6
6
|
config.logger = if silent
|
@@ -18,7 +18,7 @@ module Raven
|
|
18
18
|
|
19
19
|
# wipe out env settings to ensure we send the event
|
20
20
|
unless config.capture_allowed?
|
21
|
-
env_name = config.environments.
|
21
|
+
env_name = config.environments.last || 'production'
|
22
22
|
config.current_environment = env_name
|
23
23
|
end
|
24
24
|
|
@@ -33,27 +33,16 @@ module Raven
|
|
33
33
|
evt = instance.capture_exception(e)
|
34
34
|
end
|
35
35
|
|
36
|
-
if evt
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
elsif evt # async configuration
|
43
|
-
if evt.value.is_a? Hash
|
44
|
-
instance.logger.debug "-> event ID: #{evt.value[:event_id]}"
|
45
|
-
else
|
46
|
-
instance.logger.debug "-> event ID: #{evt.value.id}"
|
47
|
-
end
|
36
|
+
if evt
|
37
|
+
instance.logger.debug "-> event ID: #{evt.id}"
|
38
|
+
instance.logger.debug ""
|
39
|
+
instance.logger.debug "Done!"
|
40
|
+
evt
|
48
41
|
else
|
49
42
|
instance.logger.debug ""
|
50
43
|
instance.logger.debug "An error occurred while attempting to send the event."
|
51
|
-
|
44
|
+
false
|
52
45
|
end
|
53
|
-
|
54
|
-
instance.logger.debug ""
|
55
|
-
instance.logger.debug "Done!"
|
56
|
-
evt
|
57
46
|
end
|
58
47
|
end
|
59
48
|
end
|
data/lib/raven/client.rb
CHANGED
@@ -125,7 +125,7 @@ module Raven
|
|
125
125
|
configuration.logger.warn("Failed to submit event: #{get_log_message(event)}")
|
126
126
|
|
127
127
|
# configuration.transport_failure_callback can be false & nil
|
128
|
-
configuration.transport_failure_callback.call(event) if configuration.transport_failure_callback # rubocop:disable Style/SafeNavigation
|
128
|
+
configuration.transport_failure_callback.call(event, e) if configuration.transport_failure_callback # rubocop:disable Style/SafeNavigation
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
data/lib/raven/configuration.rb
CHANGED
@@ -12,6 +12,11 @@ module Raven
|
|
12
12
|
attr_reader :async
|
13
13
|
alias async? async
|
14
14
|
|
15
|
+
# An array of breadcrumbs loggers to be used. Available options are:
|
16
|
+
# - :sentry_logger
|
17
|
+
# - :active_support_logger
|
18
|
+
attr_reader :breadcrumbs_logger
|
19
|
+
|
15
20
|
# Number of lines of code context to capture, or nil for none
|
16
21
|
attr_accessor :context_lines
|
17
22
|
|
@@ -83,7 +88,7 @@ module Raven
|
|
83
88
|
attr_accessor :public_key
|
84
89
|
|
85
90
|
# Turns on ActiveSupport breadcrumbs integration
|
86
|
-
|
91
|
+
attr_reader :rails_activesupport_breadcrumbs
|
87
92
|
|
88
93
|
# Rails catches exceptions in the ActionDispatch::ShowExceptions or
|
89
94
|
# ActionDispatch::DebugExceptions middlewares, depending on the environment.
|
@@ -118,6 +123,19 @@ module Raven
|
|
118
123
|
# Otherwise, can be one of "http", "https", or "dummy"
|
119
124
|
attr_accessor :scheme
|
120
125
|
|
126
|
+
# a proc/lambda that takes an array of stack traces
|
127
|
+
# it'll be used to silence (reduce) backtrace of the exception
|
128
|
+
#
|
129
|
+
# for example:
|
130
|
+
#
|
131
|
+
# ```ruby
|
132
|
+
# Raven.configuration.backtrace_cleanup_callback = lambda do |backtrace|
|
133
|
+
# Rails.backtrace_cleaner.clean(backtrace)
|
134
|
+
# end
|
135
|
+
# ```
|
136
|
+
#
|
137
|
+
attr_accessor :backtrace_cleanup_callback
|
138
|
+
|
121
139
|
# Secret key for authentication with the Sentry server
|
122
140
|
# If you provide a DSN, this will be set automatically.
|
123
141
|
#
|
@@ -172,16 +190,34 @@ module Raven
|
|
172
190
|
# Errors object - an Array that contains error messages. See #
|
173
191
|
attr_reader :errors
|
174
192
|
|
193
|
+
# the dsn value, whether it's set via `config.dsn=` or `ENV["SENTRY_DSN"]`
|
194
|
+
attr_reader :dsn
|
195
|
+
|
196
|
+
# Array of rack env parameters to be included in the event sent to sentry.
|
197
|
+
attr_accessor :rack_env_whitelist
|
198
|
+
|
199
|
+
# Most of these errors generate 4XX responses. In general, Sentry clients
|
200
|
+
# only automatically report 5xx responses.
|
175
201
|
IGNORE_DEFAULT = [
|
176
202
|
'AbstractController::ActionNotFound',
|
203
|
+
'ActionController::BadRequest',
|
177
204
|
'ActionController::InvalidAuthenticityToken',
|
205
|
+
'ActionController::InvalidCrossOriginRequest',
|
206
|
+
'ActionController::MethodNotAllowed',
|
207
|
+
'ActionController::NotImplemented',
|
208
|
+
'ActionController::ParameterMissing',
|
178
209
|
'ActionController::RoutingError',
|
179
210
|
'ActionController::UnknownAction',
|
211
|
+
'ActionController::UnknownFormat',
|
212
|
+
'ActionController::UnknownHttpMethod',
|
213
|
+
'ActionDispatch::Http::Parameters::ParseError',
|
214
|
+
'ActiveJob::DeserializationError', # Can cause infinite loops
|
180
215
|
'ActiveRecord::RecordNotFound',
|
181
216
|
'CGI::Session::CookieStore::TamperedWithCookie',
|
182
217
|
'Mongoid::Errors::DocumentNotFound',
|
183
|
-
'
|
184
|
-
'
|
218
|
+
'Rack::QueryParser::InvalidParameterError',
|
219
|
+
'Rack::QueryParser::ParameterTypeError',
|
220
|
+
'Sinatra::NotFound'
|
185
221
|
].freeze
|
186
222
|
|
187
223
|
# Note the order - we have to remove circular references and bad characters
|
@@ -198,11 +234,20 @@ module Raven
|
|
198
234
|
HEROKU_DYNO_METADATA_MESSAGE = "You are running on Heroku but haven't enabled Dyno Metadata. For Sentry's "\
|
199
235
|
"release detection to work correctly, please run `heroku labs:enable runtime-dyno-metadata`".freeze
|
200
236
|
|
237
|
+
RACK_ENV_WHITELIST_DEFAULT = %w(
|
238
|
+
REMOTE_ADDR
|
239
|
+
SERVER_NAME
|
240
|
+
SERVER_PORT
|
241
|
+
).freeze
|
242
|
+
|
201
243
|
LOG_PREFIX = "** [Raven] ".freeze
|
202
244
|
MODULE_SEPARATOR = "::".freeze
|
203
245
|
|
246
|
+
AVAILABLE_BREADCRUMBS_LOGGERS = [:sentry_logger, :active_support_logger].freeze
|
247
|
+
|
204
248
|
def initialize
|
205
249
|
self.async = false
|
250
|
+
self.breadcrumbs_logger = []
|
206
251
|
self.context_lines = 3
|
207
252
|
self.current_environment = current_environment_from_env
|
208
253
|
self.encoding = 'gzip'
|
@@ -215,7 +260,8 @@ module Raven
|
|
215
260
|
self.open_timeout = 1
|
216
261
|
self.processors = DEFAULT_PROCESSORS.dup
|
217
262
|
self.project_root = detect_project_root
|
218
|
-
|
263
|
+
@rails_activesupport_breadcrumbs = false
|
264
|
+
|
219
265
|
self.rails_report_rescued_exceptions = true
|
220
266
|
self.release = detect_release
|
221
267
|
self.sample_rate = 1.0
|
@@ -232,11 +278,14 @@ module Raven
|
|
232
278
|
self.timeout = 2
|
233
279
|
self.transport_failure_callback = false
|
234
280
|
self.before_send = false
|
281
|
+
self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
|
235
282
|
end
|
236
283
|
|
237
284
|
def server=(value)
|
238
285
|
return if value.nil?
|
239
286
|
|
287
|
+
@dsn = value
|
288
|
+
|
240
289
|
uri = URI.parse(value)
|
241
290
|
uri_path = uri.path.split('/')
|
242
291
|
|
@@ -273,6 +322,23 @@ module Raven
|
|
273
322
|
@async = value
|
274
323
|
end
|
275
324
|
|
325
|
+
def breadcrumbs_logger=(logger)
|
326
|
+
loggers =
|
327
|
+
if logger.is_a?(Array)
|
328
|
+
logger
|
329
|
+
else
|
330
|
+
unless AVAILABLE_BREADCRUMBS_LOGGERS.include?(logger)
|
331
|
+
raise Raven::Error, "Unsupported breadcrumbs logger. Supported loggers: #{AVAILABLE_BREADCRUMBS_LOGGERS}"
|
332
|
+
end
|
333
|
+
|
334
|
+
Array(logger)
|
335
|
+
end
|
336
|
+
|
337
|
+
require "raven/breadcrumbs/sentry_logger" if loggers.include?(:sentry_logger)
|
338
|
+
|
339
|
+
@breadcrumbs_logger = logger
|
340
|
+
end
|
341
|
+
|
276
342
|
def transport_failure_callback=(value)
|
277
343
|
unless value == false || value.respond_to?(:call)
|
278
344
|
raise(ArgumentError, "transport_failure_callback must be callable (or false to disable)")
|
@@ -329,6 +395,11 @@ module Raven
|
|
329
395
|
Backtrace::Line.instance_variable_set(:@in_app_pattern, nil) # blow away cache
|
330
396
|
end
|
331
397
|
|
398
|
+
def rails_activesupport_breadcrumbs=(val)
|
399
|
+
DeprecationHelper.deprecate_old_breadcrumbs_configuration(:active_support_logger)
|
400
|
+
@rails_activesupport_breadcrumbs = val
|
401
|
+
end
|
402
|
+
|
332
403
|
def exception_class_allowed?(exc)
|
333
404
|
if exc.is_a?(Raven::Error)
|
334
405
|
# Try to prevent error reporting loops
|
@@ -342,6 +413,10 @@ module Raven
|
|
342
413
|
end
|
343
414
|
end
|
344
415
|
|
416
|
+
def enabled_in_current_env?
|
417
|
+
environments.empty? || environments.include?(current_environment)
|
418
|
+
end
|
419
|
+
|
345
420
|
private
|
346
421
|
|
347
422
|
def detect_project_root
|
@@ -423,7 +498,7 @@ module Raven
|
|
423
498
|
end
|
424
499
|
|
425
500
|
def capture_in_current_environment?
|
426
|
-
return true
|
501
|
+
return true if enabled_in_current_env?
|
427
502
|
|
428
503
|
@errors << "Not configured to send/capture in environment '#{current_environment}'"
|
429
504
|
false
|
data/lib/raven/context.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rbconfig'
|
2
|
+
require 'etc'
|
2
3
|
|
3
4
|
module Raven
|
4
5
|
class Context
|
@@ -24,18 +25,22 @@ module Raven
|
|
24
25
|
|
25
26
|
class << self
|
26
27
|
def os_context
|
27
|
-
@os_context ||=
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
@os_context ||=
|
29
|
+
begin
|
30
|
+
uname = Etc.uname
|
31
|
+
{
|
32
|
+
name: uname[:sysname] || RbConfig::CONFIG["host_os"],
|
33
|
+
version: uname[:version],
|
34
|
+
build: uname[:release],
|
35
|
+
kernel_version: uname[:version]
|
36
|
+
}
|
37
|
+
end
|
33
38
|
end
|
34
39
|
|
35
40
|
def runtime_context
|
36
41
|
@runtime_context ||= {
|
37
|
-
:
|
38
|
-
:
|
42
|
+
name: RbConfig::CONFIG["ruby_install_name"],
|
43
|
+
version: RUBY_DESCRIPTION || Raven.sys_command("ruby -v")
|
39
44
|
}
|
40
45
|
end
|
41
46
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'raven/core_ext/object/duplicable'
|
2
|
+
|
3
|
+
#########################################
|
4
|
+
# This file was copied from Rails 5.2 #
|
5
|
+
#########################################
|
6
|
+
|
7
|
+
class Object
|
8
|
+
# Returns a deep copy of object if it's duplicable. If it's
|
9
|
+
# not duplicable, returns +self+.
|
10
|
+
#
|
11
|
+
# object = Object.new
|
12
|
+
# dup = object.deep_dup
|
13
|
+
# dup.instance_variable_set(:@a, 1)
|
14
|
+
#
|
15
|
+
# object.instance_variable_defined?(:@a) # => false
|
16
|
+
# dup.instance_variable_defined?(:@a) # => true
|
17
|
+
def deep_dup
|
18
|
+
duplicable? ? dup : self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Array
|
23
|
+
# Returns a deep copy of array.
|
24
|
+
#
|
25
|
+
# array = [1, [2, 3]]
|
26
|
+
# dup = array.deep_dup
|
27
|
+
# dup[1][2] = 4
|
28
|
+
#
|
29
|
+
# array[1][2] # => nil
|
30
|
+
# dup[1][2] # => 4
|
31
|
+
def deep_dup
|
32
|
+
map(&:deep_dup)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Hash
|
37
|
+
# Returns a deep copy of hash.
|
38
|
+
#
|
39
|
+
# hash = { a: { b: 'b' } }
|
40
|
+
# dup = hash.deep_dup
|
41
|
+
# dup[:a][:c] = 'c'
|
42
|
+
#
|
43
|
+
# hash[:a][:c] # => nil
|
44
|
+
# dup[:a][:c] # => "c"
|
45
|
+
def deep_dup
|
46
|
+
hash = dup
|
47
|
+
each_pair do |key, value|
|
48
|
+
if key.frozen? && ::String === key
|
49
|
+
hash[key] = value.deep_dup
|
50
|
+
else
|
51
|
+
hash.delete(key)
|
52
|
+
hash[key.deep_dup] = value.deep_dup
|
53
|
+
end
|
54
|
+
end
|
55
|
+
hash
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#########################################
|
4
|
+
# This file was copied from Rails 5.2 #
|
5
|
+
#########################################
|
6
|
+
|
7
|
+
#--
|
8
|
+
# Most objects are cloneable, but not all. For example you can't dup methods:
|
9
|
+
#
|
10
|
+
# method(:puts).dup # => TypeError: allocator undefined for Method
|
11
|
+
#
|
12
|
+
# Classes may signal their instances are not duplicable removing +dup+/+clone+
|
13
|
+
# or raising exceptions from them. So, to dup an arbitrary object you normally
|
14
|
+
# use an optimistic approach and are ready to catch an exception, say:
|
15
|
+
#
|
16
|
+
# arbitrary_object.dup rescue object
|
17
|
+
#
|
18
|
+
# Rails dups objects in a few critical spots where they are not that arbitrary.
|
19
|
+
# That rescue is very expensive (like 40 times slower than a predicate), and it
|
20
|
+
# is often triggered.
|
21
|
+
#
|
22
|
+
# That's why we hardcode the following cases and check duplicable? instead of
|
23
|
+
# using that rescue idiom.
|
24
|
+
#++
|
25
|
+
class Object
|
26
|
+
# Can you safely dup this object?
|
27
|
+
#
|
28
|
+
# False for method objects;
|
29
|
+
# true otherwise.
|
30
|
+
def duplicable?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class NilClass
|
36
|
+
begin
|
37
|
+
nil.dup
|
38
|
+
rescue TypeError
|
39
|
+
# +nil+ is not duplicable:
|
40
|
+
#
|
41
|
+
# nil.duplicable? # => false
|
42
|
+
# nil.dup # => TypeError: can't dup NilClass
|
43
|
+
def duplicable?
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class FalseClass
|
50
|
+
begin
|
51
|
+
false.dup
|
52
|
+
rescue TypeError
|
53
|
+
# +false+ is not duplicable:
|
54
|
+
#
|
55
|
+
# false.duplicable? # => false
|
56
|
+
# false.dup # => TypeError: can't dup FalseClass
|
57
|
+
def duplicable?
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class TrueClass
|
64
|
+
begin
|
65
|
+
true.dup
|
66
|
+
rescue TypeError
|
67
|
+
# +true+ is not duplicable:
|
68
|
+
#
|
69
|
+
# true.duplicable? # => false
|
70
|
+
# true.dup # => TypeError: can't dup TrueClass
|
71
|
+
def duplicable?
|
72
|
+
false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Symbol
|
78
|
+
begin
|
79
|
+
:symbol.dup # Ruby 2.4.x.
|
80
|
+
"symbol_from_string".to_sym.dup # Some symbols can't `dup` in Ruby 2.4.0.
|
81
|
+
rescue TypeError
|
82
|
+
# Symbols are not duplicable:
|
83
|
+
#
|
84
|
+
# :my_symbol.duplicable? # => false
|
85
|
+
# :my_symbol.dup # => TypeError: can't dup Symbol
|
86
|
+
def duplicable?
|
87
|
+
false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Numeric
|
93
|
+
begin
|
94
|
+
1.dup
|
95
|
+
rescue TypeError
|
96
|
+
# Numbers are not duplicable:
|
97
|
+
#
|
98
|
+
# 3.duplicable? # => false
|
99
|
+
# 3.dup # => TypeError: can't dup Integer
|
100
|
+
def duplicable?
|
101
|
+
false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
require "bigdecimal"
|
107
|
+
class BigDecimal
|
108
|
+
# BigDecimals are duplicable:
|
109
|
+
#
|
110
|
+
# BigDecimal("1.2").duplicable? # => true
|
111
|
+
# BigDecimal("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
|
112
|
+
def duplicable?
|
113
|
+
true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class Method
|
118
|
+
# Methods are not duplicable:
|
119
|
+
#
|
120
|
+
# method(:puts).duplicable? # => false
|
121
|
+
# method(:puts).dup # => TypeError: allocator undefined for Method
|
122
|
+
def duplicable?
|
123
|
+
false
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class Complex
|
128
|
+
begin
|
129
|
+
Complex(1).dup
|
130
|
+
rescue TypeError
|
131
|
+
# Complexes are not duplicable:
|
132
|
+
#
|
133
|
+
# Complex(1).duplicable? # => false
|
134
|
+
# Complex(1).dup # => TypeError: can't copy Complex
|
135
|
+
def duplicable?
|
136
|
+
false
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class Rational
|
142
|
+
begin
|
143
|
+
Rational(1).dup
|
144
|
+
rescue TypeError
|
145
|
+
# Rationals are not duplicable:
|
146
|
+
#
|
147
|
+
# Rational(1).duplicable? # => false
|
148
|
+
# Rational(1).dup # => TypeError: can't copy Rational
|
149
|
+
def duplicable?
|
150
|
+
false
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|