From dac82eb2c2fd37384291b42affdbbdff3e5460d4 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:16:03 -0500 Subject: [PATCH] Options: Add #to_s method `Options` objects are converted to a string in some circumstances but this produces output like `#`. For example, formulae.brew.sh contains analytics entries with incomprehensible names like `adns #`. This shortcoming is apparent when compared to other entries like `adns --HEAD`. The `Option` class has a `#to_s` method that returns the `flag`, so this commit simply adds an `Options#to_s` method that calls `#to_s` on each `Option` object in `@options` and joins them using spaces. This should produce more useful output when an `Options` object is converted to a string (e.g., `--first --second`). --- Library/Homebrew/options.rb | 5 +++++ Library/Homebrew/test/options_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/Library/Homebrew/options.rb b/Library/Homebrew/options.rb index 1126ab2260..0e919eaa67 100644 --- a/Library/Homebrew/options.rb +++ b/Library/Homebrew/options.rb @@ -145,6 +145,11 @@ class Options alias to_ary to_a + sig { returns(String) } + def to_s + @options.map(&:to_s).join(" ") + end + sig { returns(String) } def inspect "#<#{self.class.name}: #{to_a.inspect}>" diff --git a/Library/Homebrew/test/options_spec.rb b/Library/Homebrew/test/options_spec.rb index 62e2fb420f..e97c4a7b4a 100644 --- a/Library/Homebrew/test/options_spec.rb +++ b/Library/Homebrew/test/options_spec.rb @@ -90,6 +90,14 @@ describe Options do expect(described_class.create(array).sort).to eq([option1, option2].sort) end + specify "#to_s" do + expect(options.to_s).to eq("") + options << Option.new("first") + expect(options.to_s).to eq("--first") + options << Option.new("second") + expect(options.to_s).to eq("--first --second") + end + specify "#inspect" do expect(options.inspect).to eq("#") options << Option.new("foo")