Skip to content

Instantly share code, notes, and snippets.

@hachi8833
Last active March 13, 2017 05:15
Show Gist options
  • Save hachi8833/e41a3a8f75df4856f5a73cfc90585b28 to your computer and use it in GitHub Desktop.
Save hachi8833/e41a3a8f75df4856f5a73cfc90585b28 to your computer and use it in GitHub Desktop.
# kazzさん謹製
module MyModlue
class BaseClass
def public_method
'called public_method'
end
def call_methods_with_self
puts self.public_method rescue puts 'CANNOT call public_method'
puts self.protected_method rescue puts 'CANNOT call protected_method'
puts self.private_method rescue puts 'CANNOT call private_method'
end
def call_methods_without_self
puts public_method rescue puts 'CANNOT call public_method'
puts protected_method rescue puts 'CANNOT call protected_method'
puts private_method rescue puts 'CANNOT call private_method'
end
protected
def protected_method
'called protected_method'
end
private
def private_method
'called private_method'
end
end
class SubClass < BaseClass
def call_methods_with_self
puts self.public_method rescue puts 'CANNOT call public_method'
puts self.protected_method rescue puts 'CANNOT call protected_method'
puts self.private_method rescue puts 'CANNOT call private_method'
end
def call_methods_without_self
puts public_method rescue puts 'CANNOT call public_method'
puts protected_method rescue puts 'CANNOT call protected_method'
puts private_method rescue puts 'CANNOT call private_method'
end
end
class OtherClass
def call_methods
puts BaseClass.new.public_method rescue puts 'CANNOT call public_method'
puts BaseClass.new.protected_method rescue puts 'CANNOT call protected_method'
puts BaseClass.new.private_method rescue puts 'CANNOT call private_method'
end
end
end
puts '*** calling BaseClass self(with self) ***'
MyModlue::BaseClass.new.call_methods_with_self
puts
puts '*** calling BaseClass self(without self) ***'
MyModlue::BaseClass.new.call_methods_without_self
puts
puts '*** Calling subclass (with self) ***'
MyModlue::SubClass.new.call_methods_with_self
puts
puts '*** calling subclass (without self) ***'
MyModlue::SubClass.new.call_methods_without_self
puts
puts '*** calling inner module ***'
MyModlue::OtherClass.new.call_methods
puts
puts '*** calling outer module ***'
puts MyModlue::BaseClass.new.public_method rescue puts 'CANNOT call public_method'
puts MyModlue::BaseClass.new.protected_method rescue puts 'CANNOT call protected_method'
puts MyModlue::BaseClass.new.private_method rescue puts 'CANNOT call private_method'
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment