Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override class methods

I want to override the method Selenium::WebDriver.for. This is what I tried:

module SeleniumWebDriverExtension
  def self.for(browser, *args)
    if browser != :phantomjs
      super(browser, *args)
    else
      options = {
          "phantomjs.cli.args" => ["--ssl-protocol=tlsv1"]
      }
      capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs(options)
      super(browser, desired_capabilities: capabilities)
    end
  end
end

Selenium::WebDriver.prepend(SeleniumWebDriverExtension)

But I got error when Selenium::Webdriver.for(:phantomjs) is called.

NoMethodError: super: no superclass method `for' for Selenium::WebDriver::Driver:Class

How can I call the original method from the overriding method?

like image 343
ironsand Avatar asked Dec 15 '15 04:12

ironsand


People also ask

What would you override a method of a base class?

An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.

How do you select a method to override?

quick command search : ctrl + shift + A Type constructor to override all of them quickly :D.

Can class methods be overridden?

In C#, class methods, indexers, properties and events can all be overridden. Non-virtual or static methods cannot be overridden. The overridden base method must be virtual, abstract, or override.

What is @override in Java class?

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

How to override a class in Python?

Using a method that overrides a class, you can "copy" another class, avoiding duplication of code, and at the same time improve or customize part of it. Thus, overriding a method is part of the inheritance mechanism. In Python, overriding a method is done by simply defining it in a child method class with the same method name in the parent class.

What is overriding superclass methods in Java?

Overriding Super Class Methods in Java | Inheritance in Java 9.3 Overriding Superclass Methods In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

How to properly override a base class method?

How to “properly” override a base class method? 1 Call base.Method (), and then provide my implementation. 2 Provide my implementation and then call base.Method () 3 Just provide my implementation. More ...

What is the difference between method overloading and method overriding?

Method overloading vs. method overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.


2 Answers

module SeleniumWebDriverExtension
  def for(browser, *args)
    ...
  end
end

Selenium::WebDriver.singleton_class.prepend(SeleniumWebDriverExtension)
like image 107
sawa Avatar answered Sep 21 '22 20:09

sawa


When to you use self inside a module like this:

def self.for(browser, *args)

end

it is declared as a module function, not an instance method on the class that will include this module. What this means is that won't appear on included classes when the module is mixed into another class.

It is similar to writing:

 def SeleniumWebDriverExtension::for
 end

So if you want to call super from within the module, declare it as a simple instance method like the accepted answer has suggested. Just wanted to clear you on the reasoning behind this.

Btw SeleniumWebDriverExtension.ancestors to be clear on the inheritance hierarchy.

like image 25
mhaseeb Avatar answered Sep 24 '22 20:09

mhaseeb