Friedrich Ewald My Personal Website

Posts


  • Ruby on Rails nil check

    Rails provides a great way to check for nil and empty variables in the same call: blank?. This is especially helpful in ERB templates where a variable can be either nil or empty, depending on the object. Without it, the check would look similar to this:

    <% unless obj.nil? and obj != "" %>
        <%= obj %>
    <% end %>
    This is quite cumbersome and easy to forget. With blank?, this can be simplified to:
    <% unless obj.blank? %>
        <%= obj %>
    <% end %>
    For the following values blank? returns true and false respectively:
    "".blank?
    => true
    
    nil.blank?
    => true
    
    "hello".blank?
    => false
    
    5.blank?
    => false
    
    AnyClass.blank?
    => false

  • Jekyll JSONFeed installed

    I finally followed through with my plan from 2017 to install JSONFeed for this blog. Valid feed validated on JSONFeed.org Although I haven’t seen any breakthrough of this as a technology, I’ll keep it running next to the XML feed and will monitor all requests. This feed is compliant to the standard. To get it to work, I had to adjust my template a little bit and also use jsonify from the liquid template language. Overall, it was easier to get an XML feed to work as JSON is very picky about escaping of double quotes and other special HTML characters. My template for Jekyll is saved as feed.json and looks as follows:

    ---
    layout: null
    ---
    {
        "version": "https://jsonfeed.org/version/1.1",
        "title": {{ site.title | jsonify }},
        "description": {{ site.description | strip_newlines | jsonify }},
        "language": "{{ site.language }}",
        "home_page_url": "{{ site.url }}{{ site.baseurl }}",
        "feed_url": "{{ site.url }}{{ site.baseurl }}/feed.json",
        "items": [
            {% for post in site.posts limit:100 %}
            {
                "id": "{{ post.url | prepend: site.baseurl | prepend: site.url }}",
                "url": "{{ post.url | prepend: site.baseurl | prepend: site.url }}",
                "title": {{ post.title | jsonify }},
                "content_html": {{ post.content | strip_newlines | jsonify }},
                "date_published": "{{ post.date | date_to_xmlschema }}"
            }{% unless forloop.last %},{% endunless %}
            {% endfor %}
        ]
    }

  • Wisdom

    Buy the nicest screwdrivers you can afford.
    Source

  • How much Precision do you need?

    Probably not so much, or at least less than you might think. We often need to store dates and times in a database and retrieve them later. I have seen people choosing to store those in the highest precision available. For example, in Python the precision for a datetime object is microseconds. That is one millionth of a second! For a lot of use cases, this kind of precision is not needed. For many cases, second precision is enough. Examples for use-cases that probably don’t need the highest precision are:

    • Blog posts
    • Comments
    • Social networks
    • Audit logs
    Furthermore, it makes sense to use the same precision in your code as in your database. By doing so, you get the exact same date and time when you save an object to the database and when you retrieve it later. This is especially important in tests where exact equality is tested. One example is MySQL which has the DATETIME(3) type where the number denotes the sub-second precision. Defining the precision explicitly makes it not only more predictable in your code, it also avoid wasting space. As usual, it depends on the use case. It does make sense to think a little bit about the precision to avoid problems later in the development process.

  • Jekyll Compose

    I was looking for an easy way to create new jekyll posts via command line and found the plugin Jekyll compose. I am very late to the party but wanted to give this a little bit more visibility as I have used a more bare-bones Jekyll beforehand. Installation is simple via:

    gem 'jekyll-compose', group: [:jekyll-plugins]
    bundle
    Usage:
    bundle exec jekyll post "My new post"

Page: 17 of 28