This is a very hard to find word because in most cases they are not sensitive during a search. The best I could find outside of documentation is a test in IRB.
BEGIN{puts x = 10}
10
This is a very hard to find word because in most cases they are not sensitive during a search. The best I could find outside of documentation is a test in IRB.
BEGIN{puts x = 10}
10
the BEGIN/END is really handy when using the -e option to process a stream. For example, to total a file of numbers:
note how the BEGIN zeros out the global, and the END prints the result.
BEGIN and END is also used to comment such as:
You can check the same here: https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(syntax)#Comments
As all keywords
BEGIN
andEND
are documented as public instance methods ofObject
(even though you won't see them returned fromObject.public_instance_methods
)Some more detailed explanation from Programming Ruby The Pragmatic Programmer's Guide
The BEGIN block is exactly what you may assume, and that is that the block given will run before the rest of the code in your program.
This being an example.
I hope that helps.
There is a working example of this in a minitest where a collection of values is put out of the way at the end of the file, but evaluated first.
One thing that hasn't been mentioned is that in earlier versions of Ruby,
BEGIN
was unconditional:If you try that with Ruby 1.8.7, the sentence is printed, even though it's in the branch of
if
that isn't taken.Under Ruby 2.0.0, it's a syntax error to use
BEGIN
outside of the top-level (a much smarter way to handle that):Edit: In a way, nobody has answered the question you raise in your comment: Why does Ruby have
BEGIN
at all? I'll try.BEGIN
comes to Ruby (like many things) from Perl. Perl has it because it existed inawk
. It made a ton of sense inawk
because by default, anawk
file consists of a series of patterns and actions:Every pattern is checked for every line. If the pattern matches, then the action is performed. Otherwise,
awk
moves on to the next pattern. So in the mini script above, if the line matches 'foo', then the first field is printed. If the line matches 'bar', then the second field is printed.But by now you can see the gap that
BEGIN
(andEND
) blocks fill: What if you want to do something unconditionally before any intput has been tested or after all the input has been seen (like print a header at the top of your report or print a row of totals at the end of the report)? Normalawk
lines of pattern + action can't help you there.That's why
BEGIN
andEND
exist. But I'm not sure how useful they are for modern, idiomatic Ruby scripts. But as dbenhur points out in the comments, you can still use Ruby very well forawk
-like one-liners. (I also have a recollection that MiniTest, the standard Ruby testing library, used to use anat_exit
function for testing, but I'm not sure it does any longer.)Two good links about Ruby,
awk
and Ruby one-liners:From The Ruby Programming Language:
BEGIN
andEND
BlocksEvery Ruby source file can declare blocks of code to be run as the file is being loaded (the
BEGIN
blocks) and after the program has finished executing (theEND
blocks).A program may include multiple
BEGIN
andEND
blocks.BEGIN
blocks are executed in the order they are encountered.END
blocks are executed in reverse order.So: