Ruby 如何扩展模块、重写方法,并且仍然调用被重写的方法?
我想这样使用URI:Ruby 如何扩展模块、重写方法,并且仍然调用被重写的方法?,ruby,Ruby,我想这样使用URI: require 'open-uri' uri = URI.parse('http://subdomain.domain.com/section/page.html') puts uri.first_level_domain # => 'domain.com' 我该怎么做 我正在努力: module URI def parse ret = super domain = ret.host.split('.').last(2).jo
require 'open-uri'
uri = URI.parse('http://subdomain.domain.com/section/page.html')
puts uri.first_level_domain # => 'domain.com'
我该怎么做
我正在努力:
module URI
def parse
ret = super
domain = ret.host.split('.').last(2).join('.')
ret.send(:define_method, :first_level_domain, lambda { domain })
ret
end
end
但是我得到了#(NoMethodError)
未定义的方法“第一级域”
为什么事情这么复杂?你可以这样做
module URI
def first_level_domain
host.split('.').last(2).join('.')
end
end
uri = URI.parse('http://subdomain.domain.com/section/page.html')
uri.first_level_domain
# => "domain.com"
在这里,您不需要它,但可以看看
alias\u method\u chain