Skip to content

Instantly share code, notes, and snippets.

@kirs
Created February 1, 2021 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirs/e575f7a32267b07b6b856a72390494fc to your computer and use it in GitHub Desktop.
Save kirs/e575f7a32267b07b6b856a72390494fc to your computer and use it in GitHub Desktop.
Top level method
def handler(event:, context:)
  "Hello, world!"
end

While this code appears straightforward, it’s important to remember what it actually does. It adds this “function” as a private method on the Object class, the base class of the Ruby class hierarchy. In other words, the “function” has been added to nearly every object in the Ruby virtual machine. (Unless, of course, the application changes the main object and class context when loading the file, a technique that carries other risks.) At best, this breaks encapsulation and single responsibility. At worst, it risks interfering with the functionality of your application, its dependencies, or even the Ruby standard library. This is why such “top level” methods, while common in simple single-file Ruby scripts and Rakefiles, are not recommended in larger Ruby applications.

From https://daniel-azuma.com/blog/2021/01/20/designing-a-ruby-serverless-runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment