From e122eddf8643d6b13bc4516235c457a640f16e18 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 26 Jan 2016 10:22:18 -0600 Subject: [PATCH] Add Defaulting To Frozen String Literals as a ruby til --- README.md | 1 + ruby/defaulting-to-frozen-string-literals.md | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 ruby/defaulting-to-frozen-string-literals.md diff --git a/README.md b/README.md index 1c3608c..a3f8a3f 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ _305 TILs and counting..._ - [Comparing Arrays In RSpec](ruby/comparing-arrays-in-rspec.md) - [Construct A Constant From A String](ruby/construct-a-constant-from-a-string.md) - [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md) +- [Defaulting To Frozen String Literals](ruby/defaulting-to-frozen-string-literals.md) - [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md) - [Disassemble Some Codes](ruby/disassemble-some-codes.md) - [Double Splat To Merge Hashes](ruby/double-splat-to-merge-hashes.md) diff --git a/ruby/defaulting-to-frozen-string-literals.md b/ruby/defaulting-to-frozen-string-literals.md new file mode 100644 index 0000000..a98542b --- /dev/null +++ b/ruby/defaulting-to-frozen-string-literals.md @@ -0,0 +1,19 @@ +# Defaulting To Frozen String Literals + +> The cold never bothered me anyway. + +The release of Ruby 2.2 introduced the ability to freeze string literals, +making them immutable. With the release of Ruby 2.3, strings can be frozen +by default without the use of `#freeze`. By adding the following magic +comment at the top of a file + +```ruby +# frozen_string_literal: true +``` + +all string literals will default to frozen. That means that all string +literals in that file are immutable, cannot be modified. This gives the Ruby +interpreter some performance gains due to reduced object allocation. + +This is the [issue](https://bugs.ruby-lang.org/issues/11473) that introduced +it.