Skip to content

Instantly share code, notes, and snippets.

@hoshinotsuyoshi
Created July 6, 2015 03:40
Show Gist options
  • Save hoshinotsuyoshi/4423b53a2ff6dc673d25 to your computer and use it in GitHub Desktop.
Save hoshinotsuyoshi/4423b53a2ff6dc673d25 to your computer and use it in GitHub Desktop.
railsで、例えばDate#yesterdayが利用できる理由

質問を受けて、どこからrequireされてるか知らないので調べてみた。調べた記録。

メソッドの定義箇所はMethod#source_locationで調べられる

Date.today.method(:yesterday).source_location

私の環境だと、 vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/date_and_time/calculations.rb:14 的な場所だった

vimでそのファイルを開いてcallerを吐かせる

+ puts caller
module DateAndTime
  module Calculations
    DAYS_INTO_WEEK = {
...

./bin/rails c

spring止めた後に rails cする

[/Users/hoshino/repos/maker-2]./bin/rails c                                                   [hoshino:(git)master]
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/time/calculations.rb:5:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/time/calculations.rb:5:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/time.rb:2:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/time.rb:2:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/time.rb:10:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/time.rb:10:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/hash/conversions.rb:2:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/hash/conversions.rb:2:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/object/conversions.rb:4:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/object/conversions.rb:4:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/object.rb:9:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/core_ext/object.rb:9:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/configuration.rb:2:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/configuration.rb:2:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/railtie.rb:2:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/railtie.rb:2:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/engine.rb:1:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/engine.rb:1:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/application.rb:6:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails/application.rb:6:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails.rb:11:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/railties-4.2.2/lib/rails.rb:11:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activemodel-4.2.2/lib/active_model/railtie.rb:2:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/activemodel-4.2.2/lib/active_model/railtie.rb:2:in `<top (required)>'
/Users/hoshino/repos/maker-2/config/application.rb:3:in `require'
/Users/hoshino/repos/maker-2/config/application.rb:3:in `<top (required)>'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:82:in `require'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:82:in `preload'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:143:in `serve'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
/Users/hoshino/repos/maker-2/vendor/bundle/ruby/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
/opt/rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/opt/rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Loading development environment (Rails 4.2.2)
[1] pry(main)>

結論:こんな順で呼ばれてる

  • config/application.rb
  • ...
  • active_model/railtie
  • ...
  • active_support/core_ext/object
  • ...
  • active_support/core_ext/date_and_time/calculations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment