Open devnote-dev opened 1 month ago
There's no need for another method. What you're looking for is the #gsub
with a block.
@Sija I presume you'r referring to something like "foo|bar|baz".gsub(/\|(.+)\|/) { |match| match.colorize(:red) }
?
That's not even necessary the use case from OP can be achieved directly: "foo|bar|baz".gsub(/\|(.+)\|/, "|#{"\\1".colorize(:red)}|")
.
Apart from this specific use case, I think it might still be useful to offer a method to get the escape sequence of a specific Colorize::Object
.
Quite simply, this method would print the ANSI codes to an optional IO (similar to how
to_s
operates). My primary use case for this is using colorize in regex expressions. At present, there aren't any good ways to handle colorize with regexes, especially when it comes to backreferences. For example, there's no way to use colorize withString#gsub
:With this new method, it would allow for the following: