Skip to content

Enable # frozen_string_literal: true to optimize string handling #70

@tmaier

Description

@tmaier

Many files in this gem work heavily with recurring string literals (such as "Windows", "Android", "Mozilla", "4.0", "Opera", etc.) and other user agent fragments. Enabling # frozen_string_literal: true at the top of each Ruby file:

  • Reduces string allocations by ensuring repeated literals share the same frozen object
  • Guards against unintended mutations of constant strings
  • Brings this gem up to modern Ruby best practices

There appear to be no code paths that depend on mutating string literals, so this change should be safe and brings potential allocation/performance improvements for free. See https://github.com/gshutler/useragent/blob/master/lib/user_agent.rb and related files for discussion of literal usage.

Proposal:

  • Add # frozen_string_literal: true to all Ruby files in lib/ (and possibly specs)
  • Confirm the test suite passes
  • Optionally, measure object allocations with a profiler to demonstrate reduced string allocations (optional, but can be persuasive)

References: Ruby style guide on frozen string literals

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions