Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Change behavior of Enum's to_s and to_sym methods (#326)

This changes the behavior of `to_s` and `to_sym` on Enums.

`to_s` returns a humanized version of the demodularized class name, eg:
"Red," "Slate gray," "Spring green"

`to_sym` converts the demodularized class name to a symbol, eg: `:Red`,
`:SlateGray`, `:SPRING_GREEN`

Note that this was deemed a minor breaking change.

---------

Co-authored-by: Joel Drapper <joel@drapper.me>
Co-authored-by: Alexey Zapparov <alexey@zapparov.com>

authored by

Charles Luzar
Joel Drapper
Alexey Zapparov
and committed by
GitHub
f6910ffe b96277c8

+22 -1
+8 -1
lib/literal/enum.rb
··· 199 199 end 200 200 201 201 alias_method :inspect, :name 202 - alias_method :to_s, :name 202 + 203 + def to_sym 204 + self.class.names[self] 205 + end 206 + 207 + def to_s 208 + to_sym.to_s 209 + end 203 210 204 211 def deconstruct 205 212 [@value]
+14
test/enum.test.rb
··· 130 130 ] 131 131 end 132 132 133 + test ".to_s" do 134 + assert_equal Color::Red.to_s, "Red" 135 + assert_equal Color::SPRING_GREEN.to_s, "SPRING_GREEN" 136 + assert_equal Switch::On.to_s, "On" 137 + assert_equal SymbolTypedEnum::A.to_s, "A" 138 + end 139 + 140 + test ".to_sym" do 141 + assert_equal Color::Red.to_sym, :Red 142 + assert_equal Color::SPRING_GREEN.to_sym, :SPRING_GREEN 143 + assert_equal Switch::On.to_sym, :On 144 + assert_equal SymbolTypedEnum::A.to_sym, :A 145 + end 146 + 133 147 test "#succ" do 134 148 assert_equal Color::Red.succ, Color::Green 135 149 assert_equal Color::Green.succ, Color::Blue