The only reason here is to have more concise code (you do not need to add an instance name before calling the method). Using blocks in general, you can write very neat, concise, and readable code. Several times you can save your code consumers a lot of input and even code logic. Here is a traditional case!
file = File.open("some_file.txt","w") file << "more code" file.close
Compare this to this nice block alternative:
File.open("some_file.txt","w") { |file| file << "adding new stuff" }
It saved the user from the need to open and close (I personally forgot this) file itself. Instead, he made him focus on what he wanted.
Try investing blocks in such situations + when you want to write beautiful DSLs.
source share