Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby mixins errors

Tags:

ruby

mixins

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
like image 480
Autodidact Avatar asked Jun 03 '26 13:06

Autodidact


1 Answers

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

like image 61
bor1s Avatar answered Jun 06 '26 05:06

bor1s