Skip to content

Instantly share code, notes, and snippets.

t = Thread.new do
begin
1/0
rescue ZeroDivisionError => e
sleep 3
end
end
Thread.pass while t.status and t.status != "sleep"
t.raise
p t.value
class H
def hash
p "hash"
456
end
end
h={}
p h[H.new] == nil
[1].each { eval "Const = 1" }
p Const
class Object
remove_const :Const
end
a, b = Object.new, Object.new
result = nil
p a.instance_eval "x = 1"
lambda do
b.instance_eval "result = x"
end.call
p result == 1
module Autoload
end
module Autoload
class W
autoload :Y, "/tmp/empty_file.rb"
class Y
end
end
end
["/tmp/caller.rb:8", "/tmp/caller.rb:8"]
["(eval):1:in `<main>'",
"/tmp/caller.rb:5:in `eval'",
"/tmp/caller.rb:5:in `caller_of'",
"/tmp/caller.rb:9:in `<main>'"]
require 'pp'
class Caller
def self.caller_of(block)
eval("caller(0)", block.binding)
end
end
block = lambda { 1 }
pp Caller.caller_of(block)
rd, wr = IO.pipe
if Kernel::fork then
wr.close
p rd.read == "finalized"
rd.close
else
rd.close
obj = "Test"
handler = Proc.new { wr.write "finalized"; wr.close }
ObjectSpace.define_finalizer(obj, handler)
def valid_syntax?(code, fname)
code.force_encoding("ascii-8bit")
code = code.sub(/\A(?:\s*\#.*$)*(\n)?/n) {
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
}
catch {|tag| eval(code, binding, fname, 0)}
end