I spent a few good hours trying to figure out how to strip a coloured ruby string:
![]()
which comes out as this:
>> p t
"2 scenarios (e[33m2 undefinede[0m), 7 steps (e[36m2 skippede[0m, e[33m3 undefinede[0m, e[32m2 passede[0m)"
=> nil
Now I tried to gsub the string and remove the colour using a RegEx like this:
> t.gsub(/\e[(d+)m/, "")
=> "2 scenarios (e[33m2 undefinede[0m), 7 steps (e[36m2 skippede[0m, e[33m3 undefinede[0m, e[32m2 passede[0m)"
But as you can see, nothing changed. After reading a whole lot of docs on Ruby, Googling around and reading the Ruby String class I came to this conclusion:
>> b = "e[33m"
=> "e[33m"
>> b.each_byte{|c| puts c}
27
91
51
51
109
=> "e[33m"
>> "" << 27 #The only way I know how to add ASCII codes to string
=> "e"
Now what this allowed me to do is figured out the ASCII code for the e character. However because normally you escape the back slash on regular expressions, once I figured out e was a valid character, I got this:
>> "e[33m".gsub(/e[(d+)m/, '')
=> ""
I hope this saves someone a few hours!
Learn More