Ruby: Alias a method from a class

2019-07-14 02:34发布

问题:

Let's use Nokogiri as an example.

How can I rewrite

page = Nokogiri::HTML.parse(html)

as

page = myparse(html)

I see the "alias" keyword but since it involves opening up whatever modules/classes a method belongs to, there is no universal way of aliasing.

回答1:

You could create a method in your application/class to wrap this for you:

def myparse(html)
  Nokogiri::HTML.parse(html)
end


标签: ruby alias