Skip to content

Remove duplicate definitions causing method-redefined warnings (#1434)#1677

Open
ryoya1122 wants to merge 1 commit into
activerecord-hackery:v5.0.0from
ryoya1122:fix/issue-1434-method-redefined-warnings
Open

Remove duplicate definitions causing method-redefined warnings (#1434)#1677
ryoya1122 wants to merge 1 commit into
activerecord-hackery:v5.0.0from
ryoya1122:fix/issue-1434-method-redefined-warnings

Conversation

@ryoya1122
Copy link
Copy Markdown
Contributor

Closes #1434.

Problem

Loading the gem with warnings enabled (e.g. ruby -W or RSpec under -w) emits three "method redefined" warnings:

lib/ransack/nodes/condition.rb:291: warning: method redefined; discarding old arel_predicate
lib/ransack/nodes/condition.rb:220: warning: previous definition of arel_predicate was here
lib/ransack/nodes/grouping.rb:29: warning: method redefined; discarding old conditions
lib/ransack/context.rb:7: warning: method redefined; discarding old arel_visitor

The reporter saw the arel_predicate one in #1434; the same root cause produces two more.

Cause

Three methods were defined twice in the same class. In each case the first definition was dead code — it was shadowed by a later real implementation:

File Dead definition Live definition
lib/ransack/nodes/condition.rb def arel_predicate; raise "not implemented"; end (was at L220) actual implementation at L291
lib/ransack/nodes/grouping.rb attr_reader :conditions (was at L4) def conditions; @conditions ||= []; end at L29 (lazy-inits the array)
lib/ransack/context.rb second attr_reader :arel_visitor (was at L7) already covered by the multi-symbol attr_reader one line earlier

Fix

Delete the dead definitions. No behavioural change — the live implementations were already the ones in effect at runtime.

Verification

Before:

$ bundle exec ruby -W -r./lib/ransack -e 'puts "loaded"'
lib/ransack/nodes/condition.rb:291: warning: method redefined; discarding old arel_predicate
lib/ransack/nodes/condition.rb:220: warning: previous definition of arel_predicate was here
lib/ransack/nodes/grouping.rb:29: warning: method redefined; discarding old conditions
lib/ransack/context.rb:7: warning: method redefined; discarding old arel_visitor
loaded

After:

$ bundle exec ruby -W -r./lib/ransack -e 'puts "loaded"'
loaded

(Two unrelated "assigned but unused variable" warnings in adapters/active_record/context.rb remain — different category, out of scope for #1434.)

Compatibility

The three deleted lines were dead code, shadowed by the live definitions next to them. Behaviour is identical before and after; the only difference is the absence of the warnings on load. Diff is -6 / +0.

Full suite: 514 examples, 0 failures, 1 pending on v5.0.0 + this branch (SQLite, ActiveRecord 7.2.3.1, Ruby 3.4.9).

Targets v5.0.0 per #1640.

…erecord-hackery#1434)

Three methods were defined twice in the same class, causing Ruby to
emit 'method redefined; discarding old ...' warnings whenever the gem
is required with warnings enabled. The first definition in each pair
was dead code: it was shadowed by a later, real implementation.

* `Ransack::Nodes::Condition#arel_predicate` was defined as a stub
  raising "not implemented" and re-defined further down with the
  actual implementation.
* `Ransack::Nodes::Grouping#conditions` had an `attr_reader` that
  was immediately shadowed by an explicit `def` doing lazy init.
* `Ransack::Context#arel_visitor` was listed in two consecutive
  `attr_reader` calls.

Dropping the dead definitions removes the warnings without any
behavioural change (the live implementations were already the ones in
effect).
@ryoya1122 ryoya1122 force-pushed the fix/issue-1434-method-redefined-warnings branch from a8359df to c732757 Compare June 1, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant