Skip to content

Instantly share code, notes, and snippets.

@mctaylorpants
Created January 16, 2016 00:06
Show Gist options
  • Save mctaylorpants/16d31fe37556daa19ec9 to your computer and use it in GitHub Desktop.
Save mctaylorpants/16d31fe37556daa19ec9 to your computer and use it in GitHub Desktop.
Fun with Ruby's BEGIN and END blocks
puts "[3] Inside 'another_required_file.rb'"
BEGIN {
puts "[2] BEGIN inside 'another_required_file.rb'"
}
# END blocks will execute at the end of the program, in reverse order to when
# they're encountered
END {
puts "[5] END inside 'another_required_file.rb'"
}
# http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-END
$LOAD_PATH << File.dirname(__FILE__)
require 'required_file'
require 'another_required_file'
puts "[4] End of 'app.rb'"
puts "[1] Inside 'required_file.rb'"
BEGIN {
puts "[0] BEGIN inside 'required_file.rb'"
}
END {
puts "[6] END inside 'required_file.rb'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment