home.social

#rubystrings — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #rubystrings, aggregated by home.social.

fetched live
  1. Have you ever combined heredocs with format strings:

    <<TEMPLATE % [23, 42]
    First value: %s
    Second value: %s
    TEMPLATE

    Unusual to have the substitutions in the beginning, but I kind of like it 🤔

    Format string docs: idiosyncratic-ruby.com/49-what

    #ruby #rubystrings

  2. We are kind of used to this, but still it is somhow illogical: A Range object, which represents *empty*, can still have an effect when used as a string index:

    range = 0..-3
    range.to_a.empty? #=> true
    "Ruby"[range] #=> "Ru"

    It feels less "wrong" when using Ruby's more recent beginless ranges:

    "Ruby"[..-3] #=> "Ru"

    #idiosyncratic #ruby #rubystrings

  3. In #Ruby, you cannot create invalid UTF-8 string literals like "\u{D800}\u{D801}" (🚫 UTF-16 surrogates)

    A helpful tool to still be able to create such strings is Array#pack:

    str = [0xD800, 0xD801].pack("U*") # => "\xED\xA0\x80\xED\xA0\x81"
    str.encoding # => #<Encoding:UTF-8>
    str.valid_encoding? # => false

    #rubystrings #unicode

    More about Array#pack: idiosyncratic-ruby.com/4-what-

  4. #Ruby's String#sum method takes a bit count, which is handy for calculating the digit sum of a string of numbers:

    "20221220".sum(4) #=>11

    So how did this work? ASCII digits are represented by bytes from the 48..57 range, in binay that is

    0b110000..0b111001

    By passing 4 to sum, we define that only the four lower bits of each byte get used, like a bitwise or with a bitmask:

    57&0b1111 #=>9

    Voilà! We could express the above like this:

    "20221220".bytes.map{ _1&0b1111}.sum #=>11

    #rubystrings

  5. Ruby strings have a sum method which will return the sum of all bytes used in the string:

    "ab".sum # => 195

    You can think of it as .bytes.sum:

    "ab".bytes # [97, 98]
    "ab".bytes.sum # => 195

    However, this is not particularly useful when used with multi-byte encodings like UTF-8:

    "⌨".sum #=> 534
    "⌨".bytes # => [226, 140, 168]

    "☜".sum #=> 534
    "☜".bytes => [226, 152, 156]

    Docs: ruby-doc.org/core/String.html#

    #ruby #rubystrings

  6. I once created an account on the birdsite that would just post some lesser known tidbits about using strings in #Ruby. Will repost some of them here over the next days (+ new stuff I come across) #rubystrings