Tag Archives: Programming

Testing a Redux Connected Component with Thunk Actions with Enzyme

Let’s say you’ve got a React component connected to a Redux store import React from ‘react’; import PropTypes from ‘prop-types’; import { connect } from ‘react-redux’; import { saveColor } from ‘../actions/save’; import ColorButtons from ‘../components/ColorButtons’; const ColorButtons = ({ colors, onClick }) => ( <div> {colors.map(color => { <button type=”button” key={color} onClick={onClick(color)}>{color}</button> })} </div> […]

Removing the First Instance of an Element from a ES6 JavaScript Array

To build upon Marius Schulz‘s approaches to removing from an array, if you want to remove the first instance of an element in an array, and you don’t want to mutate it but instead return a new array (because perhaps you want your React components to update), use a slice. function removeFirst(src, element) { const […]

Prefer `if object.present?` over `if object`

So you’ve got a controller method class DressesController < ApplicationController def show @dress = Dress.find_by(params.permit(:id)) end end And a view <div class=”<%= dress ? ‘dress&.status’ : ” %>”>Status Indicator</div> Looks good! Works great! Now your app grows, and you have a lot of other views that use the dress. Long after your view has been […]

Using Java 8 Lambdas with Google Guava Caches

With Guava, you can define a simple in-memory cache with import static java.util.concurrent.TimeUnit.DAYS; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; Cache cache = CacheBuilder.newBuilder() .maximumSize(100000) .expireAfterAccess(7, DAYS) .build(); With this you can use .put(K, V) to load values, and .getIfPresent(K), which returns null if the key isn’t present. Sometimes it’s more convenientĀ to use get(K key, Callable<? extends V> […]

How to parameterize an RSpec shared context

I had a RSpec shared_context which was creating a FactoryGirl user mock and then logging in with it, and then running some shared examples for testing permissions on a generic user. I needed to modify it to accept a parameter of using a different user. It originally looked like this: RSpec.shared_context “auth” do let(:current_user) { […]

How to use JUnit4 Hamcrest Matchers

JUnit borrows matchers from a library called Hamcrest, which look a lot like RSpec matchers in Ruby. They can be tricky to write, but end up being much more readable for complex assertions. At its simplest case, we can turn this assertion assertTrue(board.isGameOver()); into assertThat(board.isGameOver(), is(true)); It’s slightly wordier, but I think it reads in […]

Using SUBSTR on the left of an assignment in SAS

This is a pretty cool feature, and as far as I know it’s unique to SAS and Perl. In SAS, you can have a substr to the left of an assignment. stringDate=’29MAR1984′; substr(stringDate,3,3)=’FEB’; This avoids potentially awkward situations like this: stringDate=’29MAR1984′; stringDate=substr(stringDate,1,2)||’FEB’||substr(stringDate,6,4); The bounds don’t have to be static. Lets suppose we have a dataset […]

Installing SAS 9.4 on Ubuntu 14.04

Officially SAS does not support Ubuntu Linux as a platform, in spite of it being the most popular desktop distribution. It will run, but there are two things you need to do: First, reconfigure Ubuntu not to use dash. The SAS Deployment Wizard will freeze when you first click “Next” during the install if you […]

SAS Macro Variable Resolution Timing

I thought this was neat and quirky. The implications of it are important, too. When you assign a macro variable in SAS, it doesn’t resolve immediately. It resolves It will also warn you if the variable doesn’t exist yet, too. %let x = &y; %let y = z; %put &x; Will output z Even though, […]

Using Secure HTTPS (SSL) URLs in SAS on Linux

Out of the box SAS 9.4 is not configured to use SSL on Linux x64. If you try to do this, you’ll get the error ERROR: SSL Error: Missing CA trust list This happens because when SAS gets the SSL certificate from the server, it sees that it’s signed by a Certificate Authority, but it […]