I'm creating a ruby gem with rails 6. This is my main ruby gem file:
#lib/ruby_gem_name.rb
require 'active_support'
require 'active_record'
require 'ruby_gem_name/version'
require 'ruby_gem_name/class_methods'
# frozen_string_literal: true
puts 'The gem was loaded'
module RubyGemName; end
This is lib/ruby_gem_name/class_methods.rb
module RubyGemName
module ClassMethods
def self.ruby_gem_name_class_method
puts 'Hello'
end
end
extend ClassMethods
end
i enter in irb console and i can see:
ruby-gem-name$ irb
2.7.1 :001 > require 'ruby-gem-name'
The gem was loaded
=> true
2.7.1 :002 > RubyGemName::ClassMethods.ruby_gem_name_class_method
Hello
=> nil
Now....I've added my gem to the gemfile in my rails project:
gem 'ruby_gem_name', path: 'path_to_ruby_gem_name'
I can see the installed gem in Gemfile.lock:
PATH
remote: 'path_to_ruby_gem_name'
specs:
ruby_gem_name (0.1.0)
activerecord (~> 6)
activesupport (~> 6)
rails (~> 6)
GEM
remote: https://rubygems.org/
specs:
actioncable (6.0.3.5)
actionpack (= 6.0.3.5)
Now, i want to use this class method in models of my rails project: i have this code:
class Cart < ApplicationRecord
extend RubyGemName::ClassMethods
end
and when i try to use this class method from my rails project console, i see:
shopping-cart$ bundle exec rails c
Running via Spring preloader in process 32494
Loading development environment (Rails 6.0.3.5)
2.7.1 :001 > Cart.ruby_gem_name_class_method
Traceback (most recent call last):
1: from (irb):1
NoMethodError (undefined method `ruby_gem_name_class_method' for Cart (call 'Cart.connection' to establish a connection):Class)