Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Module | class_variable_get

概要

Module#class_variable_get(name) -> bool

詳細

クラス/モジュールに定義されているクラス変数 name の値を返却。

サンプルコード
require 'tbpgr_utils'

module Hogeable
  @@hoge = 1
end

class Hoge
  @@hoge = 2
end

class Hige
  include Hogeable
end

bulk_puts_eval binding, <<-EOS
Hoge.class_variable_get :@@hoge
Hige.class_variable_get :@@hoge
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

出力

Hoge.class_variable_get :@@hoge # => 2
Hige.class_variable_get :@@hoge # => 1