tr is a Unix command that is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.
I came across it working on this problem on Codewars. One of the top solutions used the string::count method in ruby, but passed along arguments in a syntax I had not seen before.
In Ruby, to get letters in a range say A to M you could pass, "a".."m"
, but this will return a range object. However, the String#count method takes in strings. As such str.count("a".."m")
would return an error. However, passing along "a-m"
would not because the latter uses an implementation of the tr Unix command.
TLDR : Ruby strings utilise an implementation of the tr Unix command allowing us to pass strings like "a-z"
and have it unpacked into the entire range without having it as a Range object. Useful in commands that do not take in ranges as arguments.
Top comments (2)
My favorite is:
tr '[:lower:]' '[:upper:]'
This makes some things easier to deal with only upper case rather than both cases.
Oh this is nice.