ruby mixin 错误

发布于 2024-11-05 17:43:13 字数 670 浏览 3 评论 0原文

我对下面的代码感到困惑。

HTTParty 库具有名为 def self.get(..) 的类方法。

我将其包含在 Client 模块中,然后将该 Client 模块包含在我的 Line 类中并访问 get 方法在我的 def self.hi() 方法中。 但是当我运行时,它会抛出错误:

ruby geek-module.rb
geek-module.rb:12:in `hi': undefined method `get' for Line:Class (NoMethodError)
  from geek-module.rb:16:in `<main>'

为什么我无法访问 HTTParty 的 get 方法? 以下是代码:

require 'rubygems'
require 'httparty'

module Client
  include HTTParty
end

class Line
  include Client

  def self.hi
    get("http://gogle.com")
  end
end

puts Line.hi

I'm confused with the following piece of code.

HTTParty library has class method named def self.get(..).

I include it in the Client module and then include that Client module in my Line class and access the get method in my def self.hi() method.
But when I run, it throws out the error:

ruby geek-module.rb
geek-module.rb:12:in `hi': undefined method `get' for Line:Class (NoMethodError)
  from geek-module.rb:16:in `<main>'

Why I'm not being able to access that get method of HTTParty?
Following is the code:

require 'rubygems'
require 'httparty'

module Client
  include HTTParty
end

class Line
  include Client

  def self.hi
    get("http://gogle.com")
  end
end

puts Line.hi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-11-12 17:43:13

您无法访问 self.get 方法,因为您使用 include HTTParty,include 使方法可由类实例而不是类本身访问,您的 hi 方法是类方法,但 get 方法是实例方法。如果您使用类似的内容:

class Line
 include Client

 def hi
   get("http://gogle.com")
 end 
end

line = Line.new
line.get

我认为它应该有效

...或者只使用 extend Client 而不是 include

You cannot access self.get method because you use include HTTParty, include makes methods acessible by instances of class not class himself, your hi method is class method but get method is the instance method. If you use something like:

class Line
 include Client

 def hi
   get("http://gogle.com")
 end 
end

line = Line.new
line.get

I think it should work

... or just use extend Client rather than include

孤独陪着我 2024-11-12 17:43:13

因此,当您在 Client 模块中包含 HTTParty 时,您可以通过 Client.get 访问 get 方法。当您在 Line 类中包含 Client 时,您也可以通过 Client.get 访问 get 方法。实际上,如果您想在 Line 类中使用 get 方法,则不需要包含它。所以:

require 'rubygems'
require 'httparty'

module Client
  include HTTParty
end

class Line

  def self.hi
    Client.get("http://google.com")
  end
end

puts Line.hi

或者如果你想在你的 Line 类中使用 get 方法,你可以使用类似的方法:

class Client
  include HTTParty
end

class Line < Client
  def self.hi
    get("http://google.com")
  end
end

puts Line.hi

So, when you include HTTParty in the Client module you can access the get method through Client.get. And when you include Client in the Line class you can access the get method through Client.get too. Actually, if you want to use the get method in your Line class, you don't need to include it. So:

require 'rubygems'
require 'httparty'

module Client
  include HTTParty
end

class Line

  def self.hi
    Client.get("http://google.com")
  end
end

puts Line.hi

or if you want the get method in your Line class you can use something like that:

class Client
  include HTTParty
end

class Line < Client
  def self.hi
    get("http://google.com")
  end
end

puts Line.hi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文