nakajima (owner)

Forks

Revisions

gist: 56661 Download_button fork
public
Description:
Measuring monkey-patches.
Public Clone URL: git://gist.github.com/56661.git
Embed All Files: show embed
bloat.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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
example.rb #
1
2
3
4
Bloat.measure(Object) do
  require 'rubygems'
  require 'activesupport'
end
output.txt #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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?