MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
Slide 14
Slide 14 text
MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
定数参照している
スコープから
探索開始
Slide 15
Slide 15 text
MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
外側のスコープ
を探索
Slide 16
Slide 16 text
MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
スーパークラス
を探索
Slide 17
Slide 17 text
MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
トップレベルを探索
Slide 18
Slide 18 text
MY_CONSTANT = "I’m from the top level scope"
module MyModule
MY_CONSTANT = "I’m from the my module scope"
class ParentClass
MY_CONSTANT = "I’m from the super class scope”
end
class MyClass < ParentClass
MY_CONSTANT = "I’m from the current scope”
def self.show_constant
puts MY_CONSTANT
end
end
end
Ruby
の定数探索アルゴリズム
見つからなかったら
エラーを返す
require "application_controller"
require "user"
class UsersController < ApplicationController
def index
@users = User.all
end
end
require
は
必要ない!
Rails
のautoload
# app/controllers/user/notifications_controller.rb
class User::NotificationsController < ApplicationController
def index
roles = User::ROLES
end
end
# app/models/user.rb
class User < ApplicationRecord
ROLES = ["member", "guest"]
end
定数参照の失敗例①
model
のUser::ROLES
を
参照したかったのに
controller
のUser
が参照され
エラーになってしまった...
~階層化していたcontroller
の名前空間とmodel
のクラス名が被ってしまったケース~
Slide 30
Slide 30 text
# app/controllers/user/notifications_controller.rb
class User::NotificationsController < ApplicationController
def index
roles = User::ROLES
end
end
# app/models/user.rb
class User < ApplicationRecord
ROLES = ["member", "guest"]
end
定数参照の失敗例①
~階層化していたcontroller
の名前空間とmodel
のクラス名が被ってしまったケース~
::User::ROLES
のように
トップレベルから
参照すればOK
Slide 31
Slide 31 text
module Wrapper
module Aws
class S3
def self.upload_file
s3 = Aws::S3::Client.new(region: ‘xxxx’)
end
end
end
end
定数参照の失敗例②
〜名前空間がネストしたモジュールやクラス〜
unitialized constant
Wrapper::Aws::S3::Client
というエラーが発生🥺
Aws::S3::Client
はgem
に定義されたクラス
Slide 32
Slide 32 text
module Wrapper
module Aws
class S3
def self.upload_file
s3 = Aws::S3::Client.new(region: ‘xxxx’)
end
end
end
end
定数参照の失敗例②
〜名前空間がネストしたモジュールやクラス〜
Wrapper::Aws::S3
と
みなされて、Client
という
定数は存在しない、と
エラーになっていた💦
Wrapper
まで辿って
Aws::S3
を発見!