Skip to content

Commit 3d60490

Browse files
committed
Add #to_s and pretty print for ActiveSupport::InheritableOptions
1 parent ea46a00 commit 3d60490

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

activesupport/lib/active_support/ordered_options.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ def inspect
116116
"#<#{self.class.name} #{to_h.inspect}>"
117117
end
118118

119+
def to_s
120+
to_h.to_s
121+
end
122+
123+
def pretty_print(pp)
124+
pp.pp_hash(to_h)
125+
end
126+
119127
alias_method :own_key?, :key?
120128
private :own_key?
121129

activesupport/test/ordered_options_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "pp"
34
require_relative "abstract_unit"
45
require "active_support/ordered_options"
56

@@ -278,4 +279,46 @@ def test_inheritable_options_count
278279

279280
assert_equal 3, object.count
280281
end
282+
283+
def test_ordered_options_to_s
284+
object = ActiveSupport::OrderedOptions.new
285+
assert_equal "{}", object.to_s
286+
287+
object.one = "first value"
288+
object[:two] = "second value"
289+
object["three"] = "third value"
290+
291+
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
292+
end
293+
294+
def test_inheritable_options_to_s
295+
object = ActiveSupport::InheritableOptions.new(one: "first value")
296+
assert_equal "{:one=>\"first value\"}", object.to_s
297+
298+
object[:two] = "second value"
299+
object["three"] = "third value"
300+
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
301+
end
302+
303+
def test_odrered_options_pp
304+
object = ActiveSupport::OrderedOptions.new
305+
object.one = "first value"
306+
object[:two] = "second value"
307+
object["three"] = "third value"
308+
309+
io = StringIO.new
310+
PP.pp(object, io)
311+
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}\n", io.string
312+
end
313+
314+
def test_inheritable_options_pp
315+
object = ActiveSupport::InheritableOptions.new(one: "first value")
316+
object[:two] = "second value"
317+
object["three"] = "third value"
318+
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}", object.to_s
319+
320+
io = StringIO.new
321+
PP.pp(object, io)
322+
assert_equal "{:one=>\"first value\", :two=>\"second value\", :three=>\"third value\"}\n", io.string
323+
end
281324
end

0 commit comments

Comments
 (0)