The rules of Ruby’s super keyword is that if it is called without arguments, all of the original arguments are forwarded. If it is called with explicit arguments, the explicit arguments are exclusively passed in.
In this example, arguments should never be forwarded, since I am calling super with exact arguments.
Example:
@doc = Nokogiri::HTML::DocumentFragment.parse("<body></body>")
class Cat < Nokogiri::XML::Node
def initialize(arg1, arg2)
super("cat", arg2) # Pass arg2 to super
# Do something with arg1 later
end
end
When calling: Cat.new("dog", @doc) I expect to get back a <cat></cat> tag, and I expect the first argument to be ignored. Instead I am getting a <dog></dog> tag.
Is there a reason this case would defy expected behavior?
If you look at the source to nokogiri, it’s actually the
newmethod that sets the node’s name, not theinitializemethod. Nothing mysterious is happening with regards to invokingsuper, it’s just that the initialize method doesn’t do anything with those arguments.I assume this is because the
newmethod is the one that is supposed to be allocating storage and so on for the object, which in nokogiri’s case means creating the underlying libxml node, which is the thing that contain’s the node’s name.