Oleg Borodko
Oleg Borodko

Reputation: 116

How can I include the module with class to another class?

I have module with class

module My < Grape::API
  prefix 'api'
  format :json

  class Users
    helpers do
      include My2 #I want put here method 'some_method'
    end
  end

end

I have another module

module My2
  class Circle
    def some_method
      "Hello"
    end
  end
end

I can do this, but I wonder how to do with the class

module My2
    def some_method
      "Hello"
    end
end

I don't understand logical .. Thank you
How can I do it another way?

Upvotes: 0

Views: 1921

Answers (2)

Oleg Borodko
Oleg Borodko

Reputation: 116

I can put

def some_method(x, y)
  My2::Circle.new.some_method(x, y)
end

Upvotes: 0

Gennady Grishkovtsov
Gennady Grishkovtsov

Reputation: 818

You can't use include for including a class, because it uses for including methods from modules in classes. If you want to use a class, try to pass an instance of class or create class's method.

class Foo
  def self.bar
    puts 'class method'
  end

  def baz
    puts 'instance method'
  end
end

Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e120>

Example

Upvotes: 1

Related Questions