Skip to content

Instantly share code, notes, and snippets.

@milesmatthias
Forked from ahoward/a.rb
Created May 16, 2014 20:10
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 milesmatthias/56f5163ce616dd093d0a to your computer and use it in GitHub Desktop.
Save milesmatthias/56f5163ce616dd093d0a to your computer and use it in GitHub Desktop.
# make this script run, update your gist with the running script, and it's
# output in a separate gist. the entire quzi should take 1-3 minutes, but you
# get 5.
#
# the meat - take < 5 minutes to do this
#
assert :hash do
x = {}
x.is_a?(Hash)
end
assert :array do
x = []
x.is_a?(Array)
end
assert :is_an_integer do
x = 1
x.is_a?(Integer)
end
assert :is_a_fixnum do
x = 1
x.is_a?(Fixnum)
end
assert :is_a_numeric do
x = 1
x.is_a?(Numeric)
end
assert :is_a_string do
x = "hello"
x.is_a?(String)
end
assert :is_a_symbol do
x = :hello
x.is_a?(Symbol)
end
assert :is_a_class do
x = nil.class
x.is_a?(Class)
end
assert :is_a_module do
x = Module.new
x.is_a?(Module)
end
assert :global_variable do
$x = 'global-variable'
defined?($x) == 'global-variable'
end
assert :local_variable do
x = 'local-variable'
defined?(x) == 'local-variable'
end
assert :instance_variable do
@x = 'instance-variable'
defined?(@x) == 'instance-variable'
end
assert :constant do
X = 'constant'
defined?(X) == 'constant'
end
# bonus, again take less than 5 minutes
#
assert :explain_the_assert_method_below do
<<-___
if given a block, call it and force the return value to be a boolean (!!).
if any exceptions are raised from the block, then return false (if we're debugging then show a backtrace)
if not given a block, check to see if there are arguments ?
if you don't pass a block, the first line of the assert method blows up.
___
end
assert :quine do
lines = nil
lines_orig = IO.readlines(__FILE__) # use another method to obtain lines
lines = File.open(__FILE__).read.lines # use another method to obtain lines
end
BEGIN {
require 'yaml'
require 'map'
def assert(label, *args, &block)
file, line = eval('[__FILE__, __LINE__]', block.binding) # <--- assumes you have a block, error when doing things like assert :test, true, true
boolean =
if block
begin
!!block.call
rescue Object => e
if ENV['DEBUG']
warn "#{ e.class }(#{ e.message })\n#{ Array(e.backtrace).join(10.chr) }"
end
false
end
else
args.empty? || args.all?
end
unless boolean
warn "#{ label } barfed bitch!"
end
end
}
@ahoward
Copy link

ahoward commented May 17, 2014

cool. you never want to

File.open.this_leaks_files_descriptors

tho

instead

File.open{|f| f.read.lines}  # blocks are good for system health - this closes the fd after reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment