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
# test is_a? and instance_of? methods | |
# instance_of? ignore the superclass | |
class SuperClass; end | |
class ChildClass < SuperClass ; end | |
b = ChildClass.new | |
puts b, b.instance_of?(SuperClass), b.is_a?(SuperClass) |
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
[35] pry(main)> " strinsg ".strip | |
=> "strinsg" | |
[36] pry(main)> " strinsg ".squish | |
=> "strinsg" | |
[37] pry(main)> " strin sg ".strip | |
=> "strin sg" | |
[38] pry(main)> " strin sg ".squish | |
=> "strin sg" |
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 NoDoubleRaise | |
def error_handled! | |
$! = nil | |
end | |
def raise(*args) | |
if $! && args.first != $! | |
warn "Double raise at #{caller.first}, aborting" | |
exit! false | |
else |
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
t1 = Thread.new do | |
raise RuntimeError, "oops!" rescue $! | |
end | |
t2 = Thread.new do | |
raise ArgumentError, "doh!" rescue $! | |
end | |
puts "main: #{$!.inspect}" | |
puts "t1: #{t1.value.inspect}" |
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
def tag_errors | |
yield | |
rescue Exception => error | |
error.extend(Lib::Error) | |
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
puts "parent process pid is #{Process.pid}" | |
if fork | |
puts "entered the if block from #{Process.pid}" | |
else | |
puts "entered the else block from #{Process.pid}" | |
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
fork do | |
5.times do | |
sleep 1 | |
puts "I'm an orphan!" | |
end | |
end | |
abort "Parent process died..." |
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
fork do | |
5.times do | |
sleep 1 | |
puts "I am an orphan!" | |
end | |
end | |
Process.wait | |
abort "Parent process died..." |
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
child_processes = 3 | |
dead_processes = 0 | |
# We fork 3 child processes. | |
child_processes.times do | |
fork do | |
# They sleep for 3 seconds. | |
sleep 3 | |
end | |
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
puts Process.pid | |
trap(:INT) { print "Na na na, you can't get me" } | |
sleep # so that we have time to send it a signal |
OlderNewer