hyper-max-tool 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2f7d03f112146f6a7ff0096c0b9844017a36a63de34050f9b6451d355d16a12
4
+ data.tar.gz: 3caeecd27bfd870481165e89b8d44942097969f32c436310a6169bd95c39737f
5
+ SHA512:
6
+ metadata.gz: 4c81857f24db5b14528f883671853359dcceaacf5546c928d4760e208cc7c7183dc2ab79573c3ba6eb5523ed50c3d828596a968ba117200fcc7a7dd0ae9dabb4
7
+ data.tar.gz: 1b81c786899bd05e7e628a9fbc1ab78a64c2e941c2dd32119882f3f9b3facb7d5f0748397af145eb09978196932962c5d5e63a50d25d5811b73d88cfa2f82add
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "hyper-max-tool"
3
+ s.version = "0.0.1"
4
+ s.summary = "Research test"
5
+ s.description = "University research based on method_source"
6
+ s.authors = ["Andrey78"]
7
+ s.email = ["cakoc614@gmail.com"]
8
+ s.files = Dir.glob("**/*").reject { |f| f.end_with?('.gem') }
9
+ s.homepage = "https://rubygems.org/profiles/Andrey78"
10
+ s.license = "MIT"
11
+ end
@@ -0,0 +1,15 @@
1
+ # method_source changelog
2
+
3
+ ### master
4
+
5
+ ### [v1.1.0][v1.1.0] (April 15, 2024)
6
+
7
+ - Added `MethodSource.clear_cache`
8
+ - Added support for `RUBYOPT="--enable-frozen-string-literal"`
9
+
10
+ ### [v1.0.0][v1.0.0] (March 19, 2020)
11
+
12
+ - Added Ruby 2.7 support
13
+
14
+ [v1.0.0]: https://github.com/banister/method_source/releases/tag/v1.0.0
15
+ [v1.1.0]: https://github.com/banister/method_source/releases/tag/v1.1.0
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rake"
6
+ gem "rspec"
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2011 John Mair (banisterfiend)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,101 @@
1
+ method_source
2
+ =============
3
+
4
+ [![Circle Build Status](https://circleci.com/gh/banister/method_source.svg?style=shield)](https://circleci.com/gh/banister/method_source)
5
+
6
+ (C) John Mair (banisterfiend) 2011
7
+
8
+ _retrieve the sourcecode for a method_
9
+
10
+ *NOTE:* This simply utilizes `Method#source_location`; it
11
+ does not access the live AST.
12
+
13
+ `method_source` is a utility to return a method's sourcecode as a
14
+ Ruby string. Also returns `Proc` and `Lambda` sourcecode.
15
+
16
+ Method comments can also be extracted using the `comment` method.
17
+
18
+ It is written in pure Ruby (no C).
19
+
20
+ * Some Ruby 1.8 support now available.
21
+ * Support for MRI, RBX, JRuby, REE
22
+
23
+ `method_source` provides the `source` and `comment` methods to the `Method` and
24
+ `UnboundMethod` and `Proc` classes.
25
+
26
+ * Install the [gem](https://rubygems.org/gems/method_source): `gem install method_source`
27
+ * Read the [documentation](https://www.rubydoc.info/github/banister/method_source/master)
28
+ * See the [source code](http://github.com/banister/method_source)
29
+
30
+ Example: display method source
31
+ ------------------------------
32
+
33
+ Set.instance_method(:merge).source.display
34
+ # =>
35
+ def merge(enum)
36
+ if enum.instance_of?(self.class)
37
+ @hash.update(enum.instance_variable_get(:@hash))
38
+ else
39
+ do_with_enum(enum) { |o| add(o) }
40
+ end
41
+
42
+ self
43
+ end
44
+
45
+ Example: display method comments
46
+ --------------------------------
47
+
48
+ Set.instance_method(:merge).comment.display
49
+ # =>
50
+ # Merges the elements of the given enumerable object to the set and
51
+ # returns self.
52
+
53
+ Example: display module/class comments
54
+ --------------------------------------
55
+
56
+ MethodSource::MethodExtensions.method(:included).module_comment
57
+ # =>
58
+ # This module is to be included by `Method` and `UnboundMethod` and
59
+ # provides the `#source` functionality
60
+
61
+ Limitations:
62
+ ------------
63
+
64
+ * Occasional strange behaviour in Ruby 1.8
65
+ * Cannot return source for C methods.
66
+ * Cannot return source for dynamically defined methods.
67
+
68
+ Special Thanks
69
+ --------------
70
+
71
+ [Adam Sanderson](https://github.com/adamsanderson) for `comment` functionality.
72
+
73
+ [Dmitry Elastic](https://github.com/dmitryelastic) for the brilliant Ruby 1.8 `source_location` hack.
74
+
75
+ [Samuel Kadolph](https://github.com/samuelkadolph) for the JRuby 1.8 `source_location`.
76
+
77
+ License
78
+ -------
79
+
80
+ (The MIT License)
81
+
82
+ Copyright (c) 2011 John Mair (banisterfiend)
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining
85
+ a copy of this software and associated documentation files (the
86
+ 'Software'), to deal in the Software without restriction, including
87
+ without limitation the rights to use, copy, modify, merge, publish,
88
+ distribute, sublicense, and/or sell copies of the Software, and to
89
+ permit persons to whom the Software is furnished to do so, subject to
90
+ the following conditions:
91
+
92
+ The above copyright notice and this permission notice shall be
93
+ included in all copies or substantial portions of the Software.
94
+
95
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
98
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
99
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
100
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
101
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ dlext = RbConfig::CONFIG['DLEXT']
2
+ direc = File.dirname(__FILE__)
3
+
4
+ require 'rake/clean'
5
+ require 'rubygems/package_task'
6
+ require "#{direc}/lib/method_source/version"
7
+
8
+ CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
9
+ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
10
+ "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*.rbc",
11
+ "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
12
+
13
+ def apply_spec_defaults(s)
14
+ s.name = "method_source"
15
+ s.summary = "retrieve the sourcecode for a method"
16
+ s.version = MethodSource::VERSION
17
+ s.date = Time.now.strftime '%Y-%m-%d'
18
+ s.author = "John Mair (banisterfiend)"
19
+ s.email = 'jrmair@gmail.com'
20
+ s.description = s.summary
21
+ s.require_path = 'lib'
22
+ s.license = 'MIT'
23
+
24
+ s.add_development_dependency("rspec","~>3.6")
25
+ s.add_development_dependency("rake", "~>0.9")
26
+ s.homepage = "http://banisterfiend.wordpress.com"
27
+ s.has_rdoc = 'yard'
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- spec/*`.split("\n")
30
+ end
31
+
32
+ require "rspec/core/rake_task"
33
+ RSpec::Core::RakeTask.new(:spec) do |t|
34
+ t.ruby_opts = %w[-w]
35
+ end
36
+
37
+ desc "reinstall gem"
38
+ task :reinstall => :gems do
39
+ sh "gem uninstall method_source" rescue nil
40
+ sh "gem install #{direc}/pkg/method_source-#{MethodSource::VERSION}.gem"
41
+ end
42
+
43
+ desc "Set up and run tests"
44
+ task :default => [:spec]
45
+
46
+ desc "Build the gemspec file"
47
+ task :gemspec => "ruby:gemspec"
48
+
49
+ namespace :ruby do
50
+ spec = Gem::Specification.new do |s|
51
+ apply_spec_defaults(s)
52
+ s.platform = Gem::Platform::RUBY
53
+ end
54
+
55
+ Gem::PackageTask.new(spec) do |pkg|
56
+ pkg.need_zip = false
57
+ pkg.need_tar = false
58
+ end
59
+
60
+ desc "Generate gemspec file"
61
+ task :gemspec do
62
+ File.open("#{spec.name}.gemspec", "w") do |f|
63
+ f << spec.to_ruby
64
+ end
65
+ end
66
+ end
67
+
68
+ desc "build all platform gems at once"
69
+ task :gems => [:rmgems, "ruby:gem"]
70
+
71
+ desc "remove all platform gems"
72
+ task :rmgems => ["ruby:clobber_package"]
73
+
74
+ desc "build and push latest gems"
75
+ task :pushgems => :gems do
76
+ chdir("#{direc}/pkg") do
77
+ Dir["*.gem"].each do |gemfile|
78
+ sh "gem push #{gemfile}"
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,154 @@
1
+ module MethodSource
2
+
3
+ module CodeHelpers
4
+ # Retrieve the first expression starting on the given line of the given file.
5
+ #
6
+ # This is useful to get module or method source code.
7
+ #
8
+ # @param [Array<String>, File, String] file The file to parse, either as a File or as
9
+ # @param [Integer] line_number The line number at which to look.
10
+ # NOTE: The first line in a file is
11
+ # line 1!
12
+ # @param [Hash] options The optional configuration parameters.
13
+ # @option options [Boolean] :strict If set to true, then only completely
14
+ # valid expressions are returned. Otherwise heuristics are used to extract
15
+ # expressions that may have been valid inside an eval.
16
+ # @option options [Integer] :consume A number of lines to automatically
17
+ # consume (add to the expression buffer) without checking for validity.
18
+ # @return [String] The first complete expression
19
+ # @raise [SyntaxError] If the first complete expression can't be identified
20
+ def expression_at(file, line_number, options={})
21
+ options = {
22
+ :strict => false,
23
+ :consume => 0
24
+ }.merge!(options)
25
+
26
+ lines = file.is_a?(Array) ? file : file.each_line.to_a
27
+
28
+ relevant_lines = lines[(line_number - 1)..-1] || []
29
+
30
+ extract_first_expression(relevant_lines, options[:consume])
31
+ rescue SyntaxError => e
32
+ raise if options[:strict]
33
+
34
+ begin
35
+ extract_first_expression(relevant_lines) do |code|
36
+ code.gsub(/\#\{.*?\}/, "temp")
37
+ end
38
+ rescue SyntaxError
39
+ raise e
40
+ end
41
+ end
42
+
43
+ # Retrieve the comment describing the expression on the given line of the given file.
44
+ #
45
+ # This is useful to get module or method documentation.
46
+ #
47
+ # @param [Array<String>, File, String] file The file to parse, either as a File or as
48
+ # a String or an Array of lines.
49
+ # @param [Integer] line_number The line number at which to look.
50
+ # NOTE: The first line in a file is line 1!
51
+ # @return [String] The comment
52
+ def comment_describing(file, line_number)
53
+ lines = file.is_a?(Array) ? file : file.each_line.to_a
54
+
55
+ extract_last_comment(lines[0..(line_number - 2)])
56
+ end
57
+
58
+ # Determine if a string of code is a complete Ruby expression.
59
+ # @param [String] code The code to validate.
60
+ # @return [Boolean] Whether or not the code is a complete Ruby expression.
61
+ # @raise [SyntaxError] Any SyntaxError that does not represent incompleteness.
62
+ # @example
63
+ # complete_expression?("class Hello") #=> false
64
+ # complete_expression?("class Hello; end") #=> true
65
+ # complete_expression?("class 123") #=> SyntaxError: unexpected tINTEGER
66
+ def complete_expression?(str)
67
+ old_verbose = $VERBOSE
68
+ $VERBOSE = nil
69
+
70
+ catch(:valid) do
71
+ eval("BEGIN{throw :valid}\n#{str}")
72
+ end
73
+
74
+ # Assert that a line which ends with a , or \ is incomplete.
75
+ str !~ /[,\\]\s*\z/
76
+ rescue IncompleteExpression
77
+ false
78
+ ensure
79
+ $VERBOSE = old_verbose
80
+ end
81
+
82
+ private
83
+
84
+ # Get the first expression from the input.
85
+ #
86
+ # @param [Array<String>] lines
87
+ # @param [Integer] consume A number of lines to automatically
88
+ # consume (add to the expression buffer) without checking for validity.
89
+ # @yield a clean-up function to run before checking for complete_expression
90
+ # @return [String] a valid ruby expression
91
+ # @raise [SyntaxError]
92
+ def extract_first_expression(lines, consume=0, &block)
93
+ code = consume.zero? ? +"" : lines.slice!(0..(consume - 1)).join
94
+
95
+ lines.each do |v|
96
+ code << v
97
+ return code if complete_expression?(block ? block.call(code) : code)
98
+ end
99
+ raise SyntaxError, "unexpected $end"
100
+ end
101
+
102
+ # Get the last comment from the input.
103
+ #
104
+ # @param [Array<String>] lines
105
+ # @return [String]
106
+ def extract_last_comment(lines)
107
+ buffer = +""
108
+
109
+ lines.each do |line|
110
+ # Add any line that is a valid ruby comment,
111
+ # but clear as soon as we hit a non comment line.
112
+ if (line =~ /^\s*#/) || (line =~ /^\s*$/)
113
+ buffer << line.lstrip
114
+ else
115
+ buffer.clear
116
+ end
117
+ end
118
+
119
+ buffer
120
+ end
121
+
122
+ # An exception matcher that matches only subsets of SyntaxErrors that can be
123
+ # fixed by adding more input to the buffer.
124
+ module IncompleteExpression
125
+ GENERIC_REGEXPS = [
126
+ /unexpected (\$end|end-of-file|end-of-input|END_OF_FILE)/, # mri, jruby, ruby-2.0, ironruby
127
+ /embedded document meets end of file/, # =begin
128
+ /unterminated (quoted string|string|regexp|list) meets end of file/, # "quoted string" is ironruby
129
+ /can't find string ".*" anywhere before EOF/, # rbx and jruby
130
+ /missing 'end' for/, /expecting kWHEN/ # rbx
131
+ ]
132
+
133
+ RBX_ONLY_REGEXPS = [
134
+ /expecting '[})\]]'(?:$|:)/, /expecting keyword_end/
135
+ ]
136
+
137
+ def self.===(ex)
138
+ return false unless SyntaxError === ex
139
+ case ex.message
140
+ when *GENERIC_REGEXPS
141
+ true
142
+ when *RBX_ONLY_REGEXPS
143
+ rbx?
144
+ else
145
+ false
146
+ end
147
+ end
148
+
149
+ def self.rbx?
150
+ RbConfig::CONFIG['ruby_install_name'] == 'rbx'
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,138 @@
1
+ module MethodSource
2
+ module ReeSourceLocation
3
+ # Ruby enterprise edition provides all the information that's
4
+ # needed, in a slightly different way.
5
+ def source_location
6
+ [__file__, __line__] rescue nil
7
+ end
8
+ end
9
+
10
+ module SourceLocation
11
+ module MethodExtensions
12
+ if Proc.method_defined? :__file__
13
+ include ReeSourceLocation
14
+
15
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
16
+ require 'java'
17
+
18
+ # JRuby version source_location hack
19
+ # @return [Array] A two element array containing the source location of the method
20
+ def source_location
21
+ to_java.source_location(Thread.current.to_java.getContext())
22
+ end
23
+ else
24
+
25
+
26
+ def trace_func(event, file, line, id, binding, classname)
27
+ return unless event == 'call'
28
+ set_trace_func nil
29
+
30
+ @file, @line = file, line
31
+ raise :found
32
+ end
33
+
34
+ private :trace_func
35
+
36
+ # Return the source location of a method for Ruby 1.8.
37
+ # @return [Array] A two element array. First element is the
38
+ # file, second element is the line in the file where the
39
+ # method definition is found.
40
+ def source_location
41
+ if @file.nil?
42
+ args =[*(1..(arity<-1 ? -arity-1 : arity ))]
43
+
44
+ set_trace_func method(:trace_func).to_proc
45
+ call(*args) rescue nil
46
+ set_trace_func nil
47
+ @file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
48
+ end
49
+ [@file, @line] if @file
50
+ end
51
+ end
52
+ end
53
+
54
+ module ProcExtensions
55
+ if Proc.method_defined? :__file__
56
+ include ReeSourceLocation
57
+
58
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
59
+
60
+ # Return the source location for a Proc (Rubinius only)
61
+ # @return [Array] A two element array. First element is the
62
+ # file, second element is the line in the file where the
63
+ # proc definition is found.
64
+ def source_location
65
+ [block.file.to_s, block.line]
66
+ end
67
+ else
68
+
69
+ # Return the source location for a Proc (in implementations
70
+ # without Proc#source_location)
71
+ # @return [Array] A two element array. First element is the
72
+ # file, second element is the line in the file where the
73
+ # proc definition is found.
74
+ def source_location
75
+ self.to_s =~ /@(.*):(\d+)/
76
+ [$1, $2.to_i]
77
+ end
78
+ end
79
+ end
80
+
81
+ module UnboundMethodExtensions
82
+ if Proc.method_defined? :__file__
83
+ include ReeSourceLocation
84
+
85
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
86
+ require 'java'
87
+
88
+ # JRuby version source_location hack
89
+ # @return [Array] A two element array containing the source location of the method
90
+ def source_location
91
+ to_java.source_location(Thread.current.to_java.getContext())
92
+ end
93
+
94
+ else
95
+
96
+
97
+ # Return the source location of an instance method for Ruby 1.8.
98
+ # @return [Array] A two element array. First element is the
99
+ # file, second element is the line in the file where the
100
+ # method definition is found.
101
+ def source_location
102
+ klass = case owner
103
+ when Class
104
+ owner
105
+ when Module
106
+ method_owner = owner
107
+ Class.new { include(method_owner) }
108
+ end
109
+
110
+ # deal with immediate values
111
+ case
112
+ when klass == Symbol
113
+ return :a.method(name).source_location
114
+ when klass == Integer
115
+ return 0.method(name).source_location
116
+ when klass == TrueClass
117
+ return true.method(name).source_location
118
+ when klass == FalseClass
119
+ return false.method(name).source_location
120
+ when klass == NilClass
121
+ return nil.method(name).source_location
122
+ end
123
+
124
+ begin
125
+ Object.instance_method(:method).bind(klass.allocate).call(name).source_location
126
+ rescue TypeError
127
+
128
+ # Assume we are dealing with a Singleton Class:
129
+ # 1. Get the instance object
130
+ # 2. Forward the source_location lookup to the instance
131
+ instance ||= ObjectSpace.each_object(owner).first
132
+ Object.instance_method(:method).bind(instance).call(name).source_location
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,3 @@
1
+ module MethodSource
2
+ VERSION = '1.1.0'.freeze
3
+ end
@@ -0,0 +1,177 @@
1
+ # (C) John Mair (banisterfiend) 2011
2
+ # MIT License
3
+
4
+ direc = File.dirname(__FILE__)
5
+
6
+ require "#{direc}/method_source/version"
7
+ require "#{direc}/method_source/source_location"
8
+ require "#{direc}/method_source/code_helpers"
9
+
10
+ module MethodSource
11
+ extend MethodSource::CodeHelpers
12
+
13
+ # An Exception to mark errors that were raised trying to find the source from
14
+ # a given source_location.
15
+ #
16
+ class SourceNotFoundError < StandardError; end
17
+
18
+ # Helper method responsible for extracting method body.
19
+ # Defined here to avoid polluting `Method` class.
20
+ # @param [Array] source_location The array returned by Method#source_location
21
+ # @param [String] method_name
22
+ # @return [String] The method body
23
+ def self.source_helper(source_location, name=nil)
24
+ raise SourceNotFoundError, "Could not locate source for #{name}!" unless source_location
25
+ file, line = *source_location
26
+
27
+ expression_at(lines_for(file), line)
28
+ rescue SyntaxError => e
29
+ raise SourceNotFoundError, "Could not parse source for #{name}: #{e.message}"
30
+ end
31
+
32
+ # Helper method responsible for opening source file and buffering up
33
+ # the comments for a specified method. Defined here to avoid polluting
34
+ # `Method` class.
35
+ # @param [Array] source_location The array returned by Method#source_location
36
+ # @param [String] method_name
37
+ # @return [String] The comments up to the point of the method.
38
+ def self.comment_helper(source_location, name=nil)
39
+ raise SourceNotFoundError, "Could not locate source for #{name}!" unless source_location
40
+ file, line = *source_location
41
+
42
+ comment_describing(lines_for(file), line)
43
+ end
44
+
45
+ # Load a memoized copy of the lines in a file.
46
+ #
47
+ # @param [String] file_name
48
+ # @param [String] method_name
49
+ # @return [Array<String>] the contents of the file
50
+ # @raise [SourceNotFoundError]
51
+ def self.lines_for(file_name, name=nil)
52
+ @lines_for_file ||= {}
53
+ @lines_for_file[file_name] ||= File.readlines(file_name)
54
+ rescue Errno::ENOENT => e
55
+ raise SourceNotFoundError, "Could not load source for #{name}: #{e.message}"
56
+ end
57
+
58
+ # Clear cache.
59
+ def self.clear_cache
60
+ @lines_for_file = {}
61
+ end
62
+
63
+ # @deprecated — use MethodSource::CodeHelpers#complete_expression?
64
+ def self.valid_expression?(str)
65
+ complete_expression?(str)
66
+ rescue SyntaxError
67
+ false
68
+ end
69
+
70
+ # @deprecated — use MethodSource::CodeHelpers#expression_at
71
+ def self.extract_code(source_location)
72
+ source_helper(source_location)
73
+ end
74
+
75
+ # This module is to be included by `Method` and `UnboundMethod` and
76
+ # provides the `#source` functionality
77
+ module MethodExtensions
78
+
79
+ # We use the included hook to patch Method#source on rubinius.
80
+ # We need to use the included hook as Rubinius defines a `source`
81
+ # on Method so including a module will have no effect (as it's
82
+ # higher up the MRO).
83
+ # @param [Class] klass The class that includes the module.
84
+ def self.included(klass)
85
+ if klass.method_defined?(:source) && Object.const_defined?(:RUBY_ENGINE) &&
86
+ RUBY_ENGINE =~ /rbx/
87
+
88
+ klass.class_eval do
89
+ orig_source = instance_method(:source)
90
+
91
+ define_method(:source) do
92
+ begin
93
+ super
94
+ rescue
95
+ orig_source.bind(self).call
96
+ end
97
+ end
98
+
99
+ end
100
+ end
101
+ end
102
+
103
+ # Return the sourcecode for the method as a string
104
+ # @return [String] The method sourcecode as a string
105
+ # @raise SourceNotFoundException
106
+ #
107
+ # @example
108
+ # Set.instance_method(:clear).source.display
109
+ # =>
110
+ # def clear
111
+ # @hash.clear
112
+ # self
113
+ # end
114
+ def source
115
+ MethodSource.source_helper(source_location, defined?(name) ? name : inspect)
116
+ end
117
+
118
+ # Return the comments associated with the method as a string.
119
+ # @return [String] The method's comments as a string
120
+ # @raise SourceNotFoundException
121
+ #
122
+ # @example
123
+ # Set.instance_method(:clear).comment.display
124
+ # =>
125
+ # # Removes all elements and returns self.
126
+ def comment
127
+ MethodSource.comment_helper(source_location, defined?(name) ? name : inspect)
128
+ end
129
+
130
+ # Return the comments associated with the method class/module.
131
+ # @return [String] The method's comments as a string
132
+ # @raise SourceNotFoundException
133
+ #
134
+ # @example
135
+ # MethodSource::MethodExtensions.method(:included).module_comment
136
+ # =>
137
+ # # This module is to be included by `Method` and `UnboundMethod` and
138
+ # # provides the `#source` functionality
139
+ def class_comment
140
+ if self.respond_to?(:receiver)
141
+ class_inst_or_module = self.receiver
142
+ elsif self.respond_to?(:owner)
143
+ class_inst_or_module = self.owner
144
+ else
145
+ return comment
146
+ end
147
+
148
+ if class_inst_or_module.respond_to?(:name)
149
+ const_name = class_inst_or_module.name
150
+ else
151
+ const_name = class_inst_or_module.class.name
152
+ class_inst_or_module = class_inst_or_module.class
153
+ end
154
+
155
+ location = class_inst_or_module.const_source_location(const_name)
156
+
157
+ MethodSource.comment_helper(location, defined?(name) ? name : inspect)
158
+ end
159
+ alias module_comment class_comment
160
+ end
161
+ end
162
+
163
+ class Method
164
+ include MethodSource::SourceLocation::MethodExtensions
165
+ include MethodSource::MethodExtensions
166
+ end
167
+
168
+ class UnboundMethod
169
+ include MethodSource::SourceLocation::UnboundMethodExtensions
170
+ include MethodSource::MethodExtensions
171
+ end
172
+
173
+ class Proc
174
+ include MethodSource::SourceLocation::ProcExtensions
175
+ include MethodSource::MethodExtensions
176
+ end
177
+
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "method_source".freeze
5
+ s.version = "1.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
8
+ s.require_paths = ["lib".freeze]
9
+ s.authors = ["John Mair (banisterfiend)".freeze]
10
+ s.date = "2024-04-15"
11
+ s.description = "retrieve the sourcecode for a method".freeze
12
+ s.email = "jrmair@gmail.com".freeze
13
+ s.files = ["CHANGELOG.md".freeze, ".gemtest".freeze, ".yardopts".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.markdown".freeze, "Rakefile".freeze, "lib/method_source.rb".freeze, "lib/method_source/code_helpers.rb".freeze, "lib/method_source/source_location.rb".freeze, "lib/method_source/version.rb".freeze, "method_source.gemspec".freeze, "spec/method_source/code_helpers_spec.rb".freeze, "spec/method_source_spec.rb".freeze, "spec/spec_helper.rb".freeze]
14
+ s.homepage = "http://banisterfiend.wordpress.com".freeze
15
+ s.metadata["changelog_uri"] = "https://github.com/banister/method_source/blob/master/CHANGELOG.md".freeze
16
+ s.licenses = ["MIT".freeze]
17
+ s.summary = "retrieve the sourcecode for a method".freeze
18
+ s.test_files = ["spec/method_source/code_helpers_spec.rb".freeze, "spec/method_source_spec.rb".freeze, "spec/spec_helper.rb".freeze]
19
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe MethodSource::CodeHelpers do
4
+ before do
5
+ @tester = Object.new.extend(MethodSource::CodeHelpers)
6
+ end
7
+
8
+ [
9
+ ["p = '", "'"],
10
+ ["def", "a", "(); end"],
11
+ ["p = <<FOO", "lots", "and", "lots of", "foo", "FOO"],
12
+ ["[", ":lets,", "'list',", "[/nested/", "], things ]"],
13
+ ["abc =~ /hello", "/"],
14
+ ["issue = %W/", "343/"],
15
+ ["pouts(<<HI, 'foo", "bar", "HI", "baz')"],
16
+ ["=begin", "no-one uses this syntax anymore...", "=end"],
17
+ ["puts 1, 2,", "3"],
18
+ ["puts 'hello'\\", "'world'"]
19
+ ].each do |lines|
20
+ it "should not raise an error on broken lines: #{lines.join("\\n")}" do
21
+ 1.upto(lines.size - 1) do |i|
22
+ expect(@tester.complete_expression?(lines[0...i].join("\n") + "\n")).to be_falsy
23
+ end
24
+ expect(@tester.complete_expression?(lines.join("\n"))).to be_truthy
25
+ end
26
+ end
27
+
28
+ [
29
+ ["end"],
30
+ ["puts )("],
31
+ ["1 1"],
32
+ ["puts :"]
33
+ ] + (RbConfig::CONFIG['ruby_install_name'] == 'rbx' ? [] : [
34
+ ["def", "method(1"], # in this case the syntax error is "expecting ')'".
35
+ ["o = Object.new.tap{ def o.render;","'MEH'", "}"] # in this case the syntax error is "expecting keyword_end".
36
+ ]).compact.each do |foo|
37
+ it "should raise an error on invalid syntax like #{foo.inspect}" do
38
+ expect {
39
+ @tester.complete_expression?(foo.join("\n"))
40
+ }.to raise_error(SyntaxError)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+
3
+ describe MethodSource do
4
+
5
+ describe "source_location (testing 1.8 implementation)" do
6
+ it 'should return correct source_location for a method' do
7
+ expect(method(:hello).source_location.first).to match(/spec_helper/)
8
+ end
9
+
10
+ it 'should not raise for immediate instance methods' do
11
+ [Symbol, Integer, TrueClass, FalseClass, NilClass].each do |immediate_class|
12
+ expect do
13
+ immediate_class.instance_method(:to_s).source_location
14
+ end.not_to raise_error
15
+ end
16
+ end
17
+
18
+ it 'should not raise for immediate methods' do
19
+ [:a, 1, true, false, nil].each do |immediate|
20
+ expect do
21
+ immediate.method(:to_s).source_location
22
+ end.not_to raise_error
23
+ end
24
+ end
25
+ end
26
+
27
+ before do
28
+ @hello_module_source = " def hello; :hello_module; end\n"
29
+ @hello_singleton_source = "def $o.hello; :hello_singleton; end\n"
30
+ @hello_source = "def hello; :hello; end\n"
31
+ @hello_comment = "# A comment for hello\n# It spans two lines and is indented by 2 spaces\n"
32
+ @lambda_comment = "# This is a comment for MyLambda\n"
33
+ @module_comment = "# This is a comment for module\n"
34
+ @class_comment = "# This is a comment for class\n"
35
+ @lambda_source = "MyLambda = lambda { :lambda }\n"
36
+ @proc_source = "MyProc = Proc.new { :proc }\n"
37
+ @hello_instance_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
38
+ @hello_instance_evaled_source_2 = " def \#{name}_two()\n if 40 + 4\n 45\n end\n end\n"
39
+ @hello_class_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
40
+ @hi_module_evaled_source = " def hi_\#{name}\n @var = \#{name}\n end\n"
41
+ end
42
+
43
+ it 'should define methods on Method and UnboundMethod and Proc' do
44
+ expect(Method.method_defined?(:source)).to be_truthy
45
+ expect(UnboundMethod.method_defined?(:source)).to be_truthy
46
+ expect(Proc.method_defined?(:source)).to be_truthy
47
+ end
48
+
49
+ describe "Methods" do
50
+ it 'should return source for method' do
51
+ expect(method(:hello).source).to eq(@hello_source)
52
+ end
53
+
54
+ it 'should return source for a method defined in a module' do
55
+ expect(M.instance_method(:hello).source).to eq(@hello_module_source)
56
+ end
57
+
58
+ it 'should return source for a singleton method as an instance method' do
59
+ expect(class << $o
60
+ self
61
+ end.instance_method(:hello).source).to eq(@hello_singleton_source)
62
+ end
63
+
64
+ it 'should return source for a singleton method' do
65
+ expect($o.method(:hello).source).to eq(@hello_singleton_source)
66
+ end
67
+
68
+ it 'should return a comment for method' do
69
+ expect(method(:hello).comment).to eq(@hello_comment)
70
+ end
71
+
72
+ # These tests fail because of http://jira.codehaus.org/browse/JRUBY-4576
73
+ unless defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
74
+ it 'should return source for an *_evaled method' do
75
+ expect(M.method(:hello_name).source).to eq(@hello_instance_evaled_source)
76
+ expect(M.method(:name_two).source).to eq(@hello_instance_evaled_source_2)
77
+ expect(M.instance_method(:hello_name).source).to eq(@hello_class_evaled_source)
78
+ expect(M.instance_method(:hi_name).source).to eq(@hi_module_evaled_source)
79
+ end
80
+ end
81
+
82
+ it "should raise error for evaled methods that do not pass __FILE__ and __LINE__ + 1 as its arguments" do
83
+ expect do
84
+ M.instance_method(:name_three).source
85
+ end.to raise_error(MethodSource::SourceNotFoundError)
86
+ end
87
+
88
+ if !is_rbx?
89
+ it 'should raise for C methods' do
90
+ expect do
91
+ method(:puts).source
92
+ end.to raise_error(MethodSource::SourceNotFoundError)
93
+ end
94
+ end
95
+ end
96
+
97
+ # if RUBY_VERSION =~ /1.9/ || is_rbx?
98
+ describe "Lambdas and Procs" do
99
+ it 'should return source for proc' do
100
+ expect(MyProc.source).to eq(@proc_source)
101
+ end
102
+
103
+ it 'should return an empty string if there is no comment' do
104
+ expect(MyProc.comment).to eq('')
105
+ end
106
+
107
+ it 'should return source for lambda' do
108
+ expect(MyLambda.source).to eq(@lambda_source)
109
+ end
110
+
111
+ it 'should return comment for lambda' do
112
+ expect(MyLambda.comment).to eq(@lambda_comment)
113
+ end
114
+
115
+ it 'should return comment for module' do
116
+ expect(M.instance_method(:hello).module_comment).to eq(@module_comment)
117
+ end
118
+
119
+ it 'should return comment for class' do
120
+ expect(C.method(:hello).class_comment).to eq(@class_comment)
121
+ end
122
+
123
+ it 'should return comment for class instance' do
124
+ expect(C.new.method(:hello).class_comment).to eq(@class_comment)
125
+ end
126
+ end
127
+ # end
128
+ describe "Comment tests" do
129
+ before do
130
+ @comment1 = "# a\n# b\n"
131
+ @comment2 = "# a\n# b\n"
132
+ @comment3 = "# a\n#\n# b\n"
133
+ @comment4 = "# a\n# b\n"
134
+ @comment5 = "# a\n# b\n# c\n# d\n"
135
+ end
136
+
137
+ it "should correctly extract multi-line comments" do
138
+ expect(method(:comment_test1).comment).to eq(@comment1)
139
+ end
140
+
141
+ it "should correctly strip leading whitespace before comments" do
142
+ expect(method(:comment_test2).comment).to eq(@comment2)
143
+ end
144
+
145
+ it "should keep empty comment lines" do
146
+ expect(method(:comment_test3).comment).to eq(@comment3)
147
+ end
148
+
149
+ it "should ignore blank lines between comments" do
150
+ expect(method(:comment_test4).comment).to eq(@comment4)
151
+ end
152
+
153
+ it "should align all comments to same indent level" do
154
+ expect(method(:comment_test5).comment).to eq(@comment5)
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,107 @@
1
+ require 'method_source'
2
+ require 'rspec'
3
+
4
+ def is_rbx?
5
+ defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
6
+ end
7
+
8
+ def jruby?
9
+ defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
10
+ end
11
+
12
+
13
+ # This is a comment for module
14
+ module M
15
+ def hello; :hello_module; end
16
+ end
17
+
18
+ # This is a comment for class
19
+ class C
20
+ include M
21
+ end
22
+
23
+ $o = Object.new
24
+ def $o.hello; :hello_singleton; end
25
+
26
+ # A comment for hello
27
+
28
+ # It spans two lines and is indented by 2 spaces
29
+ def hello; :hello; end
30
+
31
+ # a
32
+ # b
33
+ def comment_test1; end
34
+
35
+ # a
36
+ # b
37
+ def comment_test2; end
38
+
39
+ # a
40
+ #
41
+ # b
42
+ def comment_test3; end
43
+
44
+ # a
45
+
46
+ # b
47
+ def comment_test4; end
48
+
49
+
50
+ # a
51
+ # b
52
+ # c
53
+ # d
54
+ def comment_test5; end
55
+
56
+ # This is a comment for MyLambda
57
+ MyLambda = lambda { :lambda }
58
+ MyProc = Proc.new { :proc }
59
+
60
+
61
+ name = "name"
62
+
63
+ M.instance_eval <<-METHOD, __FILE__, __LINE__ + 1
64
+ def hello_#{name}(*args)
65
+ send_mesg(:#{name}, *args)
66
+ end
67
+ METHOD
68
+
69
+ M.class_eval <<-METHOD, __FILE__, __LINE__ + 1
70
+ def hello_#{name}(*args)
71
+ send_mesg(:#{name}, *args)
72
+ end
73
+ METHOD
74
+
75
+ # module_eval to DRY code up
76
+ #
77
+ M.module_eval <<-METHOD, __FILE__, __LINE__ + 1
78
+
79
+ # module_eval is used here
80
+ #
81
+ def hi_#{name}
82
+ @var = #{name}
83
+ end
84
+ METHOD
85
+
86
+ # case where 2 methods are defined inside an _eval block
87
+ #
88
+ M.instance_eval <<EOF, __FILE__, __LINE__ + 1
89
+
90
+ def #{name}_one()
91
+ if 40 + 3
92
+ 44
93
+ end
94
+ end
95
+
96
+
97
+ def #{name}_two()
98
+ if 40 + 4
99
+ 45
100
+ end
101
+ end
102
+ EOF
103
+
104
+ # class_eval without filename and lineno + 1 parameter
105
+
106
+ M.class_eval "def #{name}_three; @tempfile.#{name}; end"
107
+
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyper-max-tool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrey78
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-07-06 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: University research based on method_source
13
+ email:
14
+ - cakoc614@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - hyper-max-tool.gemspec
20
+ - method_source-1.1.0/CHANGELOG.md
21
+ - method_source-1.1.0/Gemfile
22
+ - method_source-1.1.0/LICENSE
23
+ - method_source-1.1.0/README.markdown
24
+ - method_source-1.1.0/Rakefile
25
+ - method_source-1.1.0/lib/method_source.rb
26
+ - method_source-1.1.0/lib/method_source/code_helpers.rb
27
+ - method_source-1.1.0/lib/method_source/source_location.rb
28
+ - method_source-1.1.0/lib/method_source/version.rb
29
+ - method_source-1.1.0/method_source.gemspec
30
+ - method_source-1.1.0/spec/method_source/code_helpers_spec.rb
31
+ - method_source-1.1.0/spec/method_source_spec.rb
32
+ - method_source-1.1.0/spec/spec_helper.rb
33
+ homepage: https://rubygems.org/profiles/Andrey78
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.6.2
52
+ specification_version: 4
53
+ summary: Research test
54
+ test_files: []