Ruby mixins errors

I am confused with the following code snippet.

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

I include it in the module Client, and then include this module Clientin my class Lineand refer to the method getin my method def self.hi(). But when I run, it throws an 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 can't I access this method getfor HTTParty? Below 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
+3
source share
2 answers

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

+4

, include HTTParty Client, get Client.get. Client Line, get Client.get . , get Line, . :

require 'rubygems'
require 'httparty'

module Client
  include HTTParty
end

class Line

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

puts Line.hi

get Line, - :

class Client
  include HTTParty
end

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

puts Line.hi
0

All Articles