What are the reserved words BEGIN or END used for in Ruby?

后端 未结 6 1793
失恋的感觉
失恋的感觉 2021-02-01 18:30

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.

          


        
6条回答
  •  抹茶落季
    2021-02-01 18:41

    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.

    puts "Goodbye cruel world!"
    
    BEGIN {
    puts "Hello World!"
    }
    

    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.

提交回复
热议问题