Skip to content

Instantly share code, notes, and snippets.

@corrupt952
Last active July 2, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corrupt952/c8f571e0678478ee3f8fb84410eda4ca to your computer and use it in GitHub Desktop.
Save corrupt952/c8f571e0678478ee3f8fb84410eda4ca to your computer and use it in GitHub Desktop.
Rubyにおけるコンテキストの確認

Rubyにおけるコンテキストの確認

requireloadFile.read+evalで、それぞれのコンテキストがどう変わるのかを確認するためのgist

実行

ruby main.rb

結果

main.rb:
	self.class: Class
	self.hash: -950369771375252526
	respond_to?(:hello): true
/.../be_require.rb:
	self.class: Object
	self.hash: -2118450235971363218
	respond_to?(:hello): false
/.../be_load.rb:
	self.class: Object
	self.hash: -2118450235971363218
	respond_to?(:hello): false
(eval):
	self.class: Class
	self.hash: -950369771375252526
	respond_to?(:hello): true

require_relative'output'
class Main
def self.hello; end
def self.exec
output
require_relative "be_require"
load "./be_load.rb"
eval File.read("./be_read.rb")
end
end
Main.exec
def output
filename = caller.first.match(/^(.+?):(\d+)(?::in `(.*)')?/)[1]
puts "#{filename}:"
puts "\tself.class: #{self.class}"
puts "\tself.hash: #{self.hash}"
puts "\trespond_to?(:hello): #{respond_to?(:hello)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment