Options: Add #to_s method

`Options` objects are converted to a string in some circumstances but
this produces output like `#<Options:0x0000000102592c90>`.

For example, formulae.brew.sh contains analytics entries with
incomprehensible names like `adns #<Options:0x0000000102592c90>`.
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`).
This commit is contained in:
Sam Ford 2023-01-19 12:16:03 -05:00
parent 344d32bf7f
commit dac82eb2c2
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
2 changed files with 13 additions and 0 deletions

View File

@ -145,6 +145,11 @@ class Options
alias to_ary to_a alias to_ary to_a
sig { returns(String) }
def to_s
@options.map(&:to_s).join(" ")
end
sig { returns(String) } sig { returns(String) }
def inspect def inspect
"#<#{self.class.name}: #{to_a.inspect}>" "#<#{self.class.name}: #{to_a.inspect}>"

View File

@ -90,6 +90,14 @@ describe Options do
expect(described_class.create(array).sort).to eq([option1, option2].sort) expect(described_class.create(array).sort).to eq([option1, option2].sort)
end 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 specify "#inspect" do
expect(options.inspect).to eq("#<Options: []>") expect(options.inspect).to eq("#<Options: []>")
options << Option.new("foo") options << Option.new("foo")