A command line interface for managing/formatting source file pragma comments.
Ruby
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github
bin Fixed Ruby pragma. Nov 7, 2016
lib
spec Updated to Runcom 3.0.0. Apr 1, 2018
.codeclimate.yml Updated Code Climate configuration to Version 2.0.0. Dec 10, 2017
.gitignore Removed `.bundle` directory from `.gitignore`. Feb 4, 2017
.rubocop.yml Updated Rubocop configuration. Apr 29, 2017
.ruby-version Updated to Ruby 2.5.1. Mar 30, 2018
CHANGES.md Added version release changes. May 2, 2018
CODE_OF_CONDUCT.md Updated to Contributor Covenant Code of Conduct 1.4.1. Aug 12, 2018
CONTRIBUTING.md
Gemfile
Guardfile Updated Guardfile to always run RSpec with documentation format. Feb 26, 2017
LICENSE.md Updated to Apache 2.0 license. Dec 31, 2017
README.md Fixed Markdown ordered list numbering. Jul 20, 2018
Rakefile Updated to Gemsmith 12.0.0. Apr 1, 2018
circle.yml Removed Circle CI Bundler cache. Mar 31, 2018
pragmater.gemspec

README.md

Pragmater

Gem Version Code Climate Maintainability Code Climate Test Coverage Circle CI Status

A command line interface for managing/formatting source file directive pragma comments (a.k.a. magic comments). Examples:

#! /usr/bin/env ruby
# frozen_string_literal: true
# encoding: UTF-8

With Ruby 2.3.0, frozen strings are supported via a pragma comment. This gem provides an easy way to add pragma comments to single or multiple Ruby source files in order to benefit from improved memory and concurrency performance.

Table of Contents

Features

  • Supports adding a pragma comment or multiple pragma comments to single or multiple source files.
  • Supports removing a pragma comment(s) from single or multiple source files.
  • Supports file list filtering. Defaults to any file.
  • Ensures duplicate pragma comments never exist.
  • Ensures pragma comments are consistently formatted.

Screencasts

asciicast

Requirements

  1. Ruby 2.5.x

Setup

Type the following to install:

gem install pragmater

Usage

Command Line Interface (CLI)

From the command line, type: pragmater help

pragmater -a, [--add=PATH]      # Add pragma comments to source file(s).
pragmater -c, [--config]        # Manage gem configuration.
pragmater -h, [--help=COMMAND]  # Show this message or get help for a command.
pragmater -r, [--remove=PATH]   # Remove pragma comments from source file(s).
pragmater -v, [--version]       # Show gem version.

Both the --add and --remove commands support options for specifying pragma comments and/or included files (viewable by running pragmater --help --add or pragmater --help --remove):

-c, [--comments=one two three]  # Pragma comments
-i, [--includes=one two three]  # File include list

Example (same options could be used for the --remove command too):

pragmater --add --comments "# frozen_string_literal: true" --includes "Gemfile" "Guardfile" "Rakefile" ".gemspec" "config.ru" "bin/**/*" "**/*.rake" "**/*.rb"

The --add and --remove commands default to the current working directory so a path isn't necessary unless you want to run Pragmater on a directory structure other than your current working directory.

Customization

This gem can be configured via a global configuration:

~/.config/pragmater/configuration.yml

It can also be configured via XDG environment variables as provided by the Runcom gem. Check out the Runcom Examples for project specific usage.

The default configuration is as follows:

:add:
  :comments: []
  :includes: []
:remove:
  :comments: []
  :includes: []

Feel free to take this default configuration, modify, and save as your own custom configuration.yml.

The configuration.yml file can be configured as follows:

  • add: Defines global/local comments and/or file include lists when adding pragma comments. The comments and includes options can be either a single string or an array of values.
  • remove: Defines global/local comments and/or file include lists when removing pragma comments. The comments and includes options can be either a single string or an array of values.

Frozen String Literals

Support for frozen string literals was added in Ruby 2.3.0. The ability to freeze strings within a source can be done by placing a frozen string pragma at the top of each source file. Example:

# frozen_string_literal: true

This is great for selective enablement of frozen string literals but might be too much work for some (even with the aid of this gem). As an alternative, frozen string literals can be enabled via the following Ruby command line option:

--enable=frozen-string-literal

It is important to note that, once enabled, this freezes strings program-wide -- It's an all or nothing option.

Regardless of whether you leverage the capabilities of this gem or the Ruby command line option mentioned above, the following Ruby command line option is available to aid debugging and tracking down frozen string literal issues:

--debug=frozen-string-literal

Ruby 2.3.0 also added the following methods to the String class:

  • String#+@: Answers a duplicated, mutable, string if not already frozen. Example:

      immutable = "test".freeze
      mutable = +immutable
      mutable.capitalize! # => "Test"
    
  • String#-@: Answers a immutable string if not already frozen. Example:

      mutable = "test"
      immutable = -mutable
      immutable.capitalize! # => FrozenError
    

You can also use the methods, shown above, for variable initialization. Example:

immutable = -"test"
mutable = +"test"

Despite Ruby allowing you to do this, it is not recommended to use the above examples as it leads to hard to read code. Instead use the following:

immutable = "test".freeze
mutable = "test"

While this requires extra typing, it expresses intent more clearly. There is a slight caveat to this rule in that the use of String#-@ was enhanced in Ruby 2.5.0 to deduplicate all instances of the same string thus reducing your memory footprint. This can be valuable in situations where you are not using the frozen string comment and need to selectively freeze strings.

Available Comments

With Ruby 2.3 and higher, the following comments are available:

  • # encoding: Defaults to UTF-8 but any supported encoding can be used. For a list of values, launch an IRB session and run Encoding.name_list.
  • # coding: The shorthand for # encoding:. Supports the same values as mentioned above.
  • # frozen_string_literal: Defaults to false but can take either true or false as a value. When enabled, Ruby will throw errors when strings are used in a mutable fashion.
  • # warn_indent: Defaults to false but can take either true or false as a value. When enabled, and running Ruby with the -w option, it'll throw warnings for code that isn't indented by two spaces.

Tests

To test, run:

bundle exec rake

Versioning

Read Semantic Versioning for details. Briefly, it means:

  • Major (X.y.z) - Incremented for any backwards incompatible public API changes.
  • Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
  • Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.

Code of Conduct

Please note that this project is released with a CODE OF CONDUCT. By participating in this project you agree to abide by its terms.

Contributions

Read CONTRIBUTING for details.

License

Copyright 2015 Alchemists. Read LICENSE for details.

History

Read CHANGES for details. Built with Gemsmith.

Credits

Developed by Brooke Kuhlmann at Alchemists.