Today I Learned Notes to self about software development

    Copying records

    When I was setting up Ruby assignment1 this week, I wanted to copy all of the exercises and specs from last quarter and associate them with the assignment for this quarter.

    I came up with a pretty nice solution using .attributes which returns a Hash of the existing attributes for a record that I can pass straight to the .create method.

    new_assignment = Assignment.find(...)
    old_assignment = Assignment.find(...)
    old_assignment.exercises.each do |e|
      copy = e.attributes
      new_e = new_assignment.exercises.create(copy)
    end
    

    BUT this .attributes includes the id too, so this doesn’t work yet. You can exclude key/value pairs from a Hash using .except. You can provide multiple keys at the same time as well.

    new_assignment = Assignment.find(...)
    old_assignment = Assignment.find(...)
    old_assignment.exercises.each do |e|
      copy = e.attributes.except("created_at", "updated_at", "id")
      new_e = new_assignment.exercises.create(copy)
    end
    
    1. This “assignment” is a Rails app, where an “assignment” is a model, and each assignment has many exercises, and each exercise has many specs. 

    JS Polyfill

    I was just trying to do a simple loop over a list of elements in JS

    var inputs = document.getElementsByTagName('input');
    inputs.forEach(function(input, index) {
        // ...
    });
    

    but suprisingly to me I got an error:

    VM52:1 Uncaught TypeError: inputs.forEach is not a function
        at <anonymous>:1:8
    

    inputs is an HTMLCollection, which is array-like object so it should be iterable— right?

    Well it is— but in modern browsers, you need to use a for loop:

    for (const i of inputs) {
      // ...
    }
    

    Alternatively you can convert it to an Array with Array.from().

    This SO answer made me curious what a “polyfill” was so I looked it up.

    In web development, a polyfill is code that implements a feature on web browsers that do not support the feature. Most often, it refers to a JavaScript library that implements an HTML5 or CSS web standard, either an established standard (supported by some browsers) on older browsers, or a proposed standard (not supported by any browsers) on existing browsers.

    —Wikipedia, Polyfill (programming)

    Serving static files with Rack

    You can use a Proc like this to serve static files.

    # config.ru
    map '/' do
      path = '/index.html'
      default_homepage = File.read(path)
      app = proc do |env|
        [200, { 'Content-Type' => 'text/html' }, [default_homepage]]
        # last argument needs to be an array
      end
      run app
    end
    

    The proc keyword is the equivalent to Proc.new.

    Pushing gems to rubygems

    In your gem project run:

    rake build
    

    to create pkg/<gem-name>-0.1.0.gem

    Then run

    gem push pkg/<gem-name>-0.1.0.gem
    

    This should ask you to sign in, if you’re not already. Sign in credentials are stored in ~/.gem/credentials. So remove that if you want to “sign out” of rubygems.

    XCode Developer Cache

    In the eternal journey of freeing up more memory, I noticed that the storage management settings on my laptop (MacOS 12) had a new section for “developer cache”.

    xcode-cache.png

    (not actually my computer)

    Not only do I not know what is does, it also was taking up a lotta space!

    It turns out all of this “cache” is related to writing code for apple products which is not why I have xcode installed (I just need it for Ruby I think). So deleting the cache is safe to do, but it will keep coming back.

    To prevent that from happening, you need to open xcode (which actually made me install something else 😠) and delete all of the simulators.

    Menu > Window > Devices > Simulators

    then highlight and delete!