Search code examples
rubygsubjekyll-extensions

How can I add hair space ( ) to gsub string?


I have the following line in a plugin to display page views on my Jekyll site:

html = pv.to_s.reverse.gsub(/...(?=.)/,'\& ').reverse

It adds space between thousands, for example 23 678.

How can I add hair space   instead of regular space in this string?


Solution

  • In HTML   is a so-called decimal numeric character reference:

    The ampersand must be followed by a "#" (U+0023) character, followed by one or more ASCII digits, representing a base-ten integer that corresponds to a Unicode code point that is allowed according to the definition below. The digits must then be followed by a ";" (U+003B) character.

    Ruby has the \u escape sequence. However it expects the following characters to represent a hexadecimal (base-sixteen) integer. That's 200A. You also have to use a double-quoted string literal which means now the \ character needs to be escaped with another one:

    "\\&\u200A"
    

    Alternatively just use it directly:

    '\& '