Code Monkey home page Code Monkey logo

Comments (7)

mattbrictson avatar mattbrictson commented on August 15, 2024 4

I've proposed a fix in #1506 if you all want to take a look.

from faraday.

iMacTia avatar iMacTia commented on August 15, 2024 2

Thank you @yykamei for the eye-opening explanation and @mattbrictson for providing a fix 🙇

from faraday.

yykamei avatar yykamei commented on August 15, 2024 1

This is caused by defining methods with the same name as Struct members.

cat test.rb
X = Struct.new(:a, :b) do
  def a
    :other
  end
end

X.new(1, 2).a # => :otherruby -W test.rb
test.rb:2: warning: method redefined; discarding old a

Before #1489, the above X was defined by inheriting from Struct.new(:a, :b). I'm not sure about Ruby's internal implementation, but I guess the difference between them is the timing of the method definition. Previously, a redefined method was declared as a method of the subclass, which is just a normal overriding.

# This case just overrides the method defined by the parent class.
class X < Struct.new(:a, :b)
  def a
  end
end

# NOTE: The above code is the same as the below.
TMP = Struct.new(:a, :b)
class X < TMP
  def a
  end
end

In the latest code, redefinition occurs during the setup of Struct. This means that defining methods from Struct members as well as custom methods within a block of .new is happening at the same time.

X = Struct.new(:a, :b) do
  def a
  end
end

# Defining the method within the block seems to be the same as the below.
class X
  def a
  end

  def a
  end
end

from faraday.

sarna avatar sarna commented on August 15, 2024 1

Thank you all ❤️

from faraday.

nholden avatar nholden commented on August 15, 2024

I'm also seeing this in Faraday 2.7.5 and not in 2.7.4.

from faraday.

iMacTia avatar iMacTia commented on August 15, 2024

This is probably a side-effect of #1489 and #1491 (cc @bdewater).
The warnings don't hurt, but I'd prefer them to go away as well if possible.

I don't like that memoized stuff going on in there and I'm not sure I fully understand why we need it, I'll need some time to look into this 👀

from faraday.

yykamei avatar yykamei commented on August 15, 2024

If possible, it might be the best to avoid defining the same name as Struct members (e.g., user should be renamed). However, such methods might be used by other plugins or applications, so another approach would be required.

from faraday.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.