Adding Paging Titles for Short Micro.blog Posts

    I noticed posts without titles do not have proper HTML Title tags on my new Micro.blog.

    Even as I type this, I kind of get it. How do you have a title without a title? šŸ¤”

    Many services rely on a title element when building links, and seeing “ScottW’s Blog” (or whatever I eventually name this thing) looks silly.

    I checked the source for a couple other Micro.blog templates and found none of them did anything special to handle the missing title. Most looked something like this:

    1
    
    <title>{{ if not .IsHome }}{{ .Title }} - {{ end }}{{ site.Title }}</title>
    

    After some trial and error, I noticed the .Summary value for the default meta description tag and decided to use that. This is the first ‘code’ I have written in Hugo, so it might need some more tweaking in the future:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
      <title>
          {{ if .IsHome }}
            Home
          {{ else if .Title }}
            {{ .Title }}
          {{ else}}
            {{ .Summary }}}
          {{ end }}
          - {{ .Site.Title }}
      </title>
    

    CSS Loaders 100 sample loaders are all done with CSS. I continue to be amazed at what can be done with CSS, even with a personal goal to write as little CSS as possible for the rest of my life. šŸ˜„

    Rails Safety Mechanisms

    An overview of some of the ways Rails protects you from yourself.

    Percent notation in Ruby

    You can put this in the “the more you know” category.

    Phlex

    Phlex is a framework for building fast, reusable, testable views in pure Ruby.

    I look forward to trying Phlex for a small project in the next couple of weeks.

    I am still a fan of ViewComponent as well, but the markdown support for Phlex might be a game changer for me.

    ShortRuby - The best part about checking email on Monday morning is finding a new ShortRuby newsletter.

    This is a weekly newsletter about what happens in Ruby’s world (mainly on Twitter) curated by @lucianghinda

    The CodingFont game is excellent, but what if you want to do a quick test drive?

    Well, here you go Programming Fonts.

    Programming Fonts Screenshot

    CodingFont is a fun bracket game that helps you pick a coding font.

    I recommend checking the Hide Font Names option.

    My winner was Azeret Mono. A younger me would have picked a smaller font, but I will give this one a shot for a couple weeks.

    Optimizing ActiveRecord SQL Memory Usage in Rails

    A decent suggestion by Patel Urbanek - Easy to Miss Way to Optimize ActiveRecord SQL Memory Usage in Rails.

    Urbanek recommends using select in your ActiveRecord queries to limit the fields returned and thus save memory. While this certainly will work, I find it messy long term.

    1. It is not obvious what data you have access to. Your code appears to have access to the user object, but most of the data is missing.
    2. Similar to #1, other helpers/decorators/etc. that you have in your codebase may have model requirements that are not obvious as well. You pass along this partial object, and you may have some unintended consequences.
    3. If you use any kind of caching, you may accidentally expose this partial object to completely unaware parts of your app

    I typically use two approaches:

    1. pluck - As suggested in the article, you may be giving up some of your model niceties. Still, it is usually a worthwhile trade-off if the amount of data returned warrants optimization. It can also help expose some code that should not live directly within your model.
    2. pluck and Struct - when using pluck gets too messy, you can always take the data returned and load it into a struct (or a PORO).

    A First Look at Hanami

    I have not spent any time with Hanami, but I am hoping to change that.

    I like Sinatra when your goal is to respond to requests and do not need the full Rails functionality and ecosystem.

    Hanami looks like it fits somewhere between the two.

    Finding Duplicate Charactes in Multiple Arrays with Ruby

    We needed to find duplicate characters in multiple arrays for Saturday’s Advent of Code.

    In the first part of the problem, it was among two arrays, so I did something quick like this:

    1
    
    first_array.select { |char| second_array.include?(char) }
    

    This worked as expected, but in part 2, you needed to perform a similar search across three arrays.

    I could have chained a second select or duplicated the line entirely, but I figured there was a more straightforward way…and of course, in Ruby, there is almost always a more straightforward way.

    I do not have a traditional comp sci background, so my mind rarely jumps to Bitwise operators, but in this case, this is the Bitwise & operator is exactly what we needed.

    @scott pointed this is not Bitwise when dealing with strings:

    It is the unary and operator. On arrays, #& is a method that returns the set union of two arrays. On integers, it is indeed implemented as a bitwise and.

    1
    2
    3
    4
    5
    
    a = "vJrwpWtwJgWrhcsFMMfFFhFp".chars
    b = "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL".chars
    c = "PmmdzqPrVvPwwTWBwg".chars
    
    duplicates = a & b & c
    

    Even better, this easily scales across N arrays.

    I have not had much use for Sizzy in while, but fired it up today and was instantly re-impressed. One of the more indispensable tools for frontend web development.

    Bare Metal FizzBuzz

    If I were deploying this somewhere, I would lean towards something that looks more like the readable_solution below.

    But for the sake of the challenge:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    def solution(number)
     "#{"Fizz" if (number % 3).zero?}#{"Buzz" if (number % 5).zero?}"
    end
    
    raise unless solution(3) == "Fizz"
    raise unless solution(5) == "Buzz"
    raise unless solution(15) == "FizzBuzz"
    
    puts "It works!"
    
    

    If this were a one-liner competition, I would like my chances. šŸ˜„

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    def readable_solution(number)
     modulo_3 = (number % 3).zero?
     modulo_5 = (number % 5).zero?
    
     if modulo_3 && modulo_5
       "FizzBuzz"
     elsif modulo_3
       "Fizz"
      elsif modulo_5
        "Buzz"
      end
    end
    
ā† Newer Posts