Created
January 16, 2016 00:06
-
-
Save mctaylorpants/16d31fe37556daa19ec9 to your computer and use it in GitHub Desktop.
Fun with Ruby's BEGIN and END blocks
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 "[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'" | |
} |
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
# 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'" |
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 "[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