-
-
Save morimori/3269649 to your computer and use it in GitHub Desktop.
クラス変数とモジュール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
@@a = 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class B < A | |
@@b = 2 | |
m_def | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class B < A | |
puts "can access @@a #{@@a}" rescue puts "cannot access @@a" | |
puts "can access @@b #{@@b}" rescue puts "cannot access @@b" | |
puts "can access @@mv1 #{@@mv1}" rescue puts "cannot access @@mv1" | |
puts "can access @@mv2 #{@@mv2}" rescue puts "cannot access @@mv2" | |
mm1 rescue puts "cannot call mm1" | |
mm2 rescue puts "cannot call mm2" | |
end | |
class A | |
puts "can access @@a #{@@a}" rescue puts "cannot access @@a" | |
puts "can access @@b #{@@b}" rescue puts "cannot access @@b" | |
puts "can access @@mv1 #{@@mv1}" rescue puts "cannot access @@mv1" | |
puts "can access @@mv2 #{@@mv2}" rescue puts "cannot access @@mv2" | |
mm1 rescue puts "cannot call mm1" | |
mm2 rescue puts "cannot call mm2" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module M | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def m_def | |
self.class_eval do | |
@@mv1 = 3 | |
def self.mm1 | |
puts 'called mm1' | |
end | |
end | |
self.class_eval <<-EOS | |
@@mv2 = 4 | |
def self.mm2 | |
puts 'called mm2' | |
end | |
EOS | |
def mm3 | |
puts 'called mm3' | |
end | |
end | |
end | |
end | |
A.send :include, M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'active_support' | |
require './a' | |
require './m' | |
require './b' | |
require './c' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment