Skip to content

Instantly share code, notes, and snippets.

@nakajima
Created February 1, 2009 21:16
Show Gist options
  • Save nakajima/56661 to your computer and use it in GitHub Desktop.
Save nakajima/56661 to your computer and use it in GitHub Desktop.
Measuring monkey-patches.
# Measure the amount bloat you're adding by requiring libraries.
#
# Usage:
#
# Bloat.measure do
# require 'rubygems'
# require 'activesupport'
# end
#
# A report will be printed that tells you how many methods were
# added, along with a list of names.
class Bloat
def self.measure(klass=Object)
new(klass).measure { yield }
end
def initialize(klass)
@klass = klass
end
def delta(m)
added(m).length
end
def added(m)
@new_methods[m].reject { |sym| @old_methods[m].include?(sym) }.sort
end
def measure
@old_methods = count
yield
@new_methods = count
report
end
private
def report
puts "#{delta(:total)} total methods added to #{@klass}:"
puts "- #{delta(:instance)} instance methods."
added(:instance).each { |m| puts " - #{m}" }
puts "- #{delta(:class)} class methods."
added(:class).each { |m| puts " - #{m}" }
end
def count
[:total, :instance, :class].inject({ }) do |memo, m|
memo[m] = send("#{m}_methods")
memo
end
end
def total_methods
class_methods + instance_methods
end
def class_methods
@klass.methods
end
def instance_methods
@klass.instance_methods
end
end
Bloat.measure(Object) do
require 'rubygems'
require 'activesupport'
end
147 total methods added to Object:
- 47 instance methods.
  - `
  - acts_like?
  - b64encode
  - blank?
  - breakpoint
  - class_eval
  - copy_instance_variables_from
  - daemonize
  - dclone
  - debugger
  - decode64
  - decode_b
  - duplicable?
  - enable_warnings
  - encode64
  - enum_for
  - extend_with_included_modules_from
  - extended_by
  - instance_exec
  - instance_values
  - instance_variable_names
  - load_with_new_constant_marking
  - metaclass
  - present?
  - remove_subclasses_of
  - require
  - require_association
  - require_dependency
  - require_library_or_gem
  - require_or_load
  - returning
  - silence_stderr
  - silence_stream
  - silence_warnings
  - subclasses_of
  - suppress
  - taguri
  - taguri=
  - to_enum
  - to_json
  - to_param
  - to_query
  - to_yaml
  - to_yaml_properties
  - to_yaml_style
  - unloadable
  - with_options
- 101 class methods.
  - `
  - acts_like?
  - alias_attribute
  - alias_method_chain
  - as_load_path
  - attr_accessor_with_default
  - attr_internal
  - attr_internal_accessor
  - attr_internal_naming_format
  - attr_internal_naming_format=
  - attr_internal_reader
  - attr_internal_writer
  - b64encode
  - blank?
  - breakpoint
  - cattr_accessor
  - cattr_reader
  - cattr_writer
  - class_inheritable_accessor
  - class_inheritable_array
  - class_inheritable_array_writer
  - class_inheritable_hash
  - class_inheritable_hash_writer
  - class_inheritable_reader
  - class_inheritable_writer
  - const_missing_with_dependencies
  - const_missing_without_dependencies
  - copy_instance_variables_from
  - daemonize
  - dclone
  - debugger
  - decode64
  - decode_b
  - delegate
  - deprecate
  - deprecated_method_warning
  - deprecation_horizon
  - duplicable?
  - enable_warnings
  - encode64
  - enum_for
  - extend_with_included_modules_from
  - extended_by
  - find_hidden_method
  - included_in_classes
  - inheritable_attributes
  - instance_exec
  - instance_values
  - instance_variable_names
  - load_with_new_constant_marking
  - local_constant_names
  - local_constants
  - mattr_accessor
  - mattr_reader
  - mattr_writer
  - metaclass
  - method_added
  - model_name
  - parent
  - parent_name
  - parents
  - present?
  - read_inheritable_attribute
  - remove_class
  - remove_subclasses
  - remove_subclasses_of
  - require
  - require_association
  - require_dependency
  - require_library_or_gem
  - require_or_load
  - reset_inheritable_attributes
  - returning
  - silence_stderr
  - silence_stream
  - silence_warnings
  - subclasses
  - subclasses_of
  - superclass_delegating_accessor
  - superclass_delegating_reader
  - superclass_delegating_writer
  - suppress
  - synchronize
  - taguri
  - taguri=
  - to_enum
  - to_json
  - to_param
  - to_query
  - to_yaml
  - to_yaml_properties
  - to_yaml_style
  - unloadable
  - with_options
  - write_inheritable_array
  - write_inheritable_attribute
  - write_inheritable_hash
  - yaml_as
  - yaml_tag_class_name
  - yaml_tag_read_class
  - yaml_tag_subclasses?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment