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:
parent
344d32bf7f
commit
dac82eb2c2
@ -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}>"
|
||||
|
||||
@ -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: []>")
|
||||
options << Option.new("foo")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user