Skip to content

Instantly share code, notes, and snippets.

@morimori
Created August 6, 2012 03:30
Show Gist options
  • Save morimori/3269649 to your computer and use it in GitHub Desktop.
Save morimori/3269649 to your computer and use it in GitHub Desktop.
クラス変数とモジュール
class A
@@a = 1
end
class B < A
@@b = 2
m_def
end
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
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
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