why_chain 0.1.0

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: bc9a06ec23c45c166f5f1ee5ef100e2a611b4a221800bf6f5b947a026c357e8a
4
+ data.tar.gz: 542eca20fb80f277e8bdfac100af52f01e781ebe7f4c3a1621f2bf3cadb4e370
5
+ SHA512:
6
+ metadata.gz: ea34de73851a224c13f468019c23cc9379edc1ac85b75e0975dc3419a67dc3a3a47bca4e19d12591f3e1335a54ad788e0bb0feafcd1838bc4522c965010efbae
7
+ data.tar.gz: 0cd9ad56a88c26c350c9f9d35918c2eef28223d0a751b31a584292c9051e44cdde069f12eb1307850f064fee0f39b7a0c34b82c37c1d3cc2ab226adf9890c019
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-05-10
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in why_chain.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ why_chain (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.3)
10
+ diff-lcs (1.6.2)
11
+ json (2.19.5)
12
+ language_server-protocol (3.17.0.5)
13
+ lint_roller (1.1.0)
14
+ parallel (1.28.0)
15
+ parser (3.3.11.1)
16
+ ast (~> 2.4.1)
17
+ racc
18
+ prism (1.9.0)
19
+ racc (1.8.1)
20
+ rainbow (3.1.1)
21
+ rake (13.4.2)
22
+ regexp_parser (2.12.0)
23
+ rspec (3.13.2)
24
+ rspec-core (~> 3.13.0)
25
+ rspec-expectations (~> 3.13.0)
26
+ rspec-mocks (~> 3.13.0)
27
+ rspec-core (3.13.6)
28
+ rspec-support (~> 3.13.0)
29
+ rspec-expectations (3.13.5)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.13.0)
32
+ rspec-mocks (3.13.8)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.13.0)
35
+ rspec-support (3.13.7)
36
+ rubocop (1.86.1)
37
+ json (~> 2.3)
38
+ language_server-protocol (~> 3.17.0.2)
39
+ lint_roller (~> 1.1.0)
40
+ parallel (>= 1.10)
41
+ parser (>= 3.3.0.2)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 2.9.3, < 3.0)
44
+ rubocop-ast (>= 1.49.0, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 2.4.0, < 4.0)
47
+ rubocop-ast (1.49.1)
48
+ parser (>= 3.3.7.2)
49
+ prism (~> 1.7)
50
+ ruby-progressbar (1.13.0)
51
+ unicode-display_width (3.2.0)
52
+ unicode-emoji (~> 4.1)
53
+ unicode-emoji (4.2.0)
54
+
55
+ PLATFORMS
56
+ arm64-darwin-24
57
+
58
+ DEPENDENCIES
59
+ rake (~> 13.0)
60
+ rspec (~> 3.0)
61
+ rubocop (~> 1.21)
62
+ why_chain!
63
+
64
+ BUNDLED WITH
65
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 alessio salati
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # WhyChain
2
+
3
+ A tiny gem to inspect Ruby method dispatch at runtime.
4
+
5
+ WhyChain helps you see:
6
+ - method lookup chain (`ancestors`)
7
+ - method owner resolution
8
+ - where `super` would resolve next
9
+
10
+ Built for learning and debugging Ruby internals. Not a production framework.
11
+
12
+ You can inspect this manually in Ruby console, but WhyChain gives you a consistent, teachable trace in one call.
13
+
14
+ ## Installation
15
+
16
+ Add the gem to your `Gemfile`:
17
+
18
+ ```ruby
19
+ gem "why_chain"
20
+ ```
21
+
22
+ Then run:
23
+
24
+ ```bash
25
+ bundle install
26
+ ```
27
+
28
+ ## Quick start
29
+
30
+ ```ruby
31
+ trace = WhyChain.trace(object, :method_name)
32
+ pp trace.to_h
33
+ ```
34
+
35
+ `trace` is a `WhyChain::DispatchTrace` object with readers:
36
+ - `lookup_chain`
37
+ - `owner`
38
+ - `next_super_owner`
39
+
40
+ As hash:
41
+
42
+ ```ruby
43
+ trace.to_h
44
+ # {
45
+ lookup_chain: [...],
46
+ owner: SomeClassOrModule,
47
+ next_super_owner: AnotherClassOrModule
48
+ # }
49
+ ```
50
+
51
+ ## Usage examples
52
+
53
+ ### 1) `prepend` vs `include` in runtime lookup
54
+
55
+ ```ruby
56
+ module P
57
+ def foo = :p
58
+ end
59
+
60
+ module I
61
+ def foo = :i
62
+ end
63
+
64
+ class A
65
+ def foo = :a
66
+ end
67
+
68
+ class B < A
69
+ include I
70
+ prepend P
71
+ end
72
+
73
+ trace = WhyChain.trace(B.new, :foo)
74
+ pp trace.to_h
75
+ # {
76
+ # lookup_chain: [P, B, I, A, Object, Kernel, BasicObject],
77
+ # owner: P,
78
+ # next_super_owner: I
79
+ # }
80
+ ```
81
+
82
+ ### 2) Singleton method takes precedence
83
+
84
+ ```ruby
85
+ obj = Object.new
86
+ def obj.single_foo = :singleton
87
+
88
+ trace = WhyChain.trace(obj, :single_foo)
89
+ pp trace.to_h
90
+ # {
91
+ # lookup_chain: [#<Class:#<Object:...>>, Object, Kernel, BasicObject],
92
+ # owner: #<Class:#<Object:...>>,
93
+ # next_super_owner: nil
94
+ # }
95
+ ```
96
+
97
+ ### 3) Missing method fails fast
98
+
99
+ ```ruby
100
+ WhyChain.trace(Object.new, :not_existing_method)
101
+ # raises NameError
102
+ ```
103
+
104
+ Note: some entries in `lookup_chain` can appear as anonymous classes/modules (for example singleton classes). This is expected and reflects Ruby runtime internals.
105
+
106
+ ## Development
107
+
108
+ Run tests:
109
+
110
+ ```bash
111
+ bundle exec rspec
112
+ ```
113
+
114
+ Run lint:
115
+
116
+ ```bash
117
+ bundle exec rubocop
118
+ ```
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub:
123
+ [https://github.com/alessio-salati/why_chain](https://github.com/alessio-salati/why_chain)
124
+
125
+ ## License
126
+
127
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhyChain
4
+ # Immutable value object for traced dispatch data.
5
+ class DispatchTrace
6
+ attr_reader :lookup_chain, :owner, :next_super_owner
7
+
8
+ def initialize(
9
+ lookup_chain:,
10
+ owner:,
11
+ next_super_owner:
12
+ )
13
+ @lookup_chain = lookup_chain
14
+ @owner = owner
15
+ @next_super_owner = next_super_owner
16
+ end
17
+
18
+ def to_h
19
+ {
20
+ lookup_chain: lookup_chain,
21
+ owner: owner,
22
+ next_super_owner: next_super_owner
23
+ }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhyChain
4
+ # Finds where super resolves after a method owner.
5
+ class MethodLocator
6
+ def initialize(lookup_chain, owner, method_name)
7
+ @lookup_chain = lookup_chain
8
+ @owner = owner
9
+ @method_name = method_name
10
+ end
11
+
12
+ def next_super_owner
13
+ owner_index = @lookup_chain.index(@owner)
14
+
15
+ return nil unless owner_index
16
+
17
+ @lookup_chain[(owner_index + 1)..].find do |mod|
18
+ defines_instance_method?(mod)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def defines_instance_method?(mod)
25
+ mod.method_defined?(@method_name, false) ||
26
+ mod.private_method_defined?(@method_name, false) ||
27
+ mod.protected_method_defined?(@method_name, false)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhyChain
4
+ # Builds a dispatch trace for a receiver and method.
5
+ class Tracer
6
+ def initialize(object, method_name)
7
+ @object = object
8
+ @method_name = method_name
9
+ end
10
+
11
+ def trace
12
+ DispatchTrace.new(
13
+ lookup_chain: lookup_chain,
14
+ owner: owner,
15
+ next_super_owner: next_super_owner
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ def lookup_chain
22
+ @object.singleton_class.ancestors
23
+ end
24
+
25
+ def owner
26
+ @owner ||= @object.method(@method_name).owner
27
+ end
28
+
29
+ def next_super_owner
30
+ MethodLocator.new(lookup_chain, owner, @method_name).next_super_owner
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhyChain
4
+ VERSION = "0.1.0"
5
+ end
data/lib/why_chain.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "why_chain/version"
4
+ require_relative "why_chain/tracer"
5
+ require_relative "why_chain/dispatch_trace"
6
+ require_relative "why_chain/method_locator"
7
+
8
+ # Entry point for WhyChain runtime dispatch introspection.
9
+ module WhyChain
10
+ class Error < StandardError; end
11
+
12
+ def self.trace(object, method_name)
13
+ Tracer.new(object, method_name).trace
14
+ end
15
+ end
data/sig/why_chain.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module WhyChain
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/why_chain.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/why_chain/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "why_chain"
7
+ spec.version = WhyChain::VERSION
8
+ spec.authors = ["alessio salati"]
9
+ spec.email = ["alessio.salati@gmail.com"]
10
+
11
+ spec.summary = "Inspect Ruby method dispatch at runtime."
12
+ spec.description = "WhyChain is a tiny educational gem to inspect lookup chain, " \
13
+ "method owner, and next super target in Ruby."
14
+ spec.homepage = "https://github.com/alessio-salati/why_chain"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.0.0"
17
+
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/alessio-salati/why_chain"
22
+ spec.metadata["changelog_uri"] = "https://github.com/alessio-salati/why_chain/blob/main/CHANGELOG.md"
23
+ spec.metadata["rubygems_mfa_required"] = "true"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(__dir__) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (File.expand_path(f) == __FILE__) ||
30
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor]) ||
31
+ f.end_with?(".gem")
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ # spec.add_dependency "example-gem", "~> 1.0"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: why_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alessio salati
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-05-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: WhyChain is a tiny educational gem to inspect lookup chain, method owner,
14
+ and next super target in Ruby.
15
+ email:
16
+ - alessio.salati@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - lib/why_chain.rb
30
+ - lib/why_chain/dispatch_trace.rb
31
+ - lib/why_chain/method_locator.rb
32
+ - lib/why_chain/tracer.rb
33
+ - lib/why_chain/version.rb
34
+ - sig/why_chain.rbs
35
+ - why_chain.gemspec
36
+ homepage: https://github.com/alessio-salati/why_chain
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org
41
+ homepage_uri: https://github.com/alessio-salati/why_chain
42
+ source_code_uri: https://github.com/alessio-salati/why_chain
43
+ changelog_uri: https://github.com/alessio-salati/why_chain/blob/main/CHANGELOG.md
44
+ rubygems_mfa_required: 'true'
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.4.10
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Inspect Ruby method dispatch at runtime.
64
+ test_files: []