[Ruby] In Rails, call your own class created under lib

less than 1 minute read

config/environment.rbに自作クラスをrequire


lib/ ├ assets/ ├ tasks/ └ test.rb <-追加

test.rb


class Test
    def aa
        puts "aa"
    end
end

config/environment.rbを編集

environment.rb



# Load the Rails application.
require_relative 'application'
require 'test' #これを追記

# Initialize the Rails application.
Rails.application.initialize!

使えるかテスト

 % rails c
Running via Spring preloader in process 62643
Loading development environment (Rails 6.0.3.2)
[1] pry(main)> Test
=> Test

以上

Tags: ,

Updated: