Hi friends. I’ve been learning a lot of things recently, but nothing cool enough to be worth an entire blog post of its own, so here they are.

Weblabora

I’ve been resurrecting Weblabora recently by abstracting out the game logic into a JAR and rewriting the front-end with more modern technology. The front-end is also open source on Github and will be hosted at Kennerspiel.com. Not much to see right now, but I am making slow and steady progress. The idea, this time, is to make it so that more games can be added relatively easily.

A JAR of the game library is published to Maven repositories is the first JAR I’ve published under the com.philihp group http://maven-repository.com/artifact/com.philihp/weblabora.

Sailing

Lately I’ve been learning how to sail. The other day in practicing man-over-board drills in a J/24 we spotted a watermelon floating in San Francisco Bay. We rescued it and ate it for lunch.

Baymelon

Portland

For the second half of 2015 I will be living in Portland, Oregon. That’s about all there is to say about that.

Posted in Uncategorized.

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 English a little better, and is capable of still-readable more-complex assertions. The is() function here isn’t from extending TestCase (JUnit 4 no longer needs this, that was a JUnit 3 thing). It’s actually a static import, although there seems to be conflicting information on the internet on what needs to be imported. I found these to work in most cases:

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

From what I can tell, JUnit pulled in some, but not all of the matchers from Hamcrest. There are still some useful matchers in Hamcrest, so I like to import them, and there aren’t any namespace collisions. It doesn’t add any additional lines to your POM since you can actually leave out the JUnit dependency and just import the Hamcrest library (since it itself depends on JUnit).

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>

In the actual test that motivated this post, I had a really specific need to test that a method would return a list which contained at any index a POJO bean which had properties for settlement being an instance of a Village class and score being 42. I was able to express this as

assertThat(
      settlementScoresList,
      hasItem(allOf(
          hasProperty("settlement", instanceOf(Village.class)),
          hasProperty("score", is(42)))));

I have relocated to San Francisco, and I now work at Airbnb as a Software Engineer.

Unfortunately, this blog will probably not see many more SAS-related posts until I can get my hands on a license. It will probably see a lot of new blog posts with newer, freer technologies. I’m learning a lot of Ruby and Rails, so expect some cool stuff on that front.

Also, I’ll probably drop more F-bombs.

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 with 1 row per letter, and we want to combine those letters into a word.

data have;
    input letter $1.;
    datalines;
h
e
l
l
o
;
run;

My initial thought was to just retain and append each letter:

data want;
  length word $200;
  retain word;
  set have nobs=nobs;
  call cats(word,letter);
  if(nobs=_n_) then output;
run;

However this breaks if one of the letters is a space, and SAS treats the space as null, so appending a space has no effect on the string. Instead of appending, just stick the letter where it’s supposed to be.

data want;
  length word $200;
  retain word ' ';
  set have nobs=nobs;
  substr(word,_n_,1) = letter;
  if(_n_==nobs) then output;
run;

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 don’t do this.

sudo dpkg-reconfigure dash

That being done, you should be able to install SAS Foundation.

Second, SAS requires libjpeg to run. When you first run sas, you’ll get this error:

ERROR:  BRIDGE FAILURE - ERROR LOADING IMAGE 
        MODULE: sasmotifsasvsub HyS SUBSYSTEM: 8 SLOT: 11 
ERROR: Could not load /usr/local/SASHome/SASFoundation/9.4/sasexe/sasmotif (35 images loaded) 
ERROR: libjpeg.so.62: cannot open shared object file: No such file or directory 
ERROR:  BRIDGE FAILURE - ERROR LOADING IMAGE 
        MODULE: sasmotifsasvsub HyS SUBSYSTEM: 8 SLOT: 11

Install this by running this command:

sudo apt-get install libjpeg62

SAS should now run.

Additionally, while not common, but if you have SAS/Secure installed and want to pull files via https, you’ll need to point it at SSL CA certificates. You can kick off SAS with

-SSLCALISTLOC="/etc/ssl/certs/ca-certificates.crt"

Or just create a file ~/sasv9.cfg with that in it.

Update: This works on 13.10 and 14.04

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, when x was defined, y did not exist. We’ll get the following warning, which can be ignored.

WARNING: Apparent symbolic reference Y not resolved.

As far as I can tell (I could be wrong!), I don’t think there’s a way of turning off that warning.

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 doesn’t have the SSL certificates from those authorities. Those come with OpenSSL, so get that.

sudo apt-get install openssl

With that installed, the SSL certificates should be in /etc/ssl/certs, with ca-certificates.crt being an uber-cert that contains all of the other files in one file. To tell SAS to look at that file, add this to SASHOME/SASFoundation/9.4/sasv9_local.cfg:

-SSLCALISTLOC="/etc/ssl/certs/ca-certificates.crt"

You’ll probably need sudo to modify that file, but you probably had that if you installed OpenSSL. But maybe your admin installed it, he just didn’t know to configure it, and he’s out on holiday because he’s European. You can put that config option in ~/sasv9.cfg instead, creating it if it doesn’t exist.

NOTE: This was tested on Ubuntu Linux x64 13.10. The location of the cert file will probably differ from distro to distro and version to version. Just look for the SSL CA cert directory, and find the biggest file in it, that’s probably the uber-cert.

If you’re using Play Scala views with Handlebars in them, and you get errors that look like these:

not found: value key
In /home/phil/myproject/app/views/myview.scala.html at line 108.

or

not found: value index
In /home/phil/myproject/app/views/myview.scala.html at line 108.

It’s probably because you’re using {{@index}} when iterating through an array with {{#each myarray}} or {{@key}} when iterating through an object’s properties.

This happens because Play framework views are Scala, and the @ character is a special character. Scala thinks you’re trying to access a Scala value (and comes back with the error above). You’ll need to escape it with @@ so Scala will pass it along as just a single @ for Handlebars to see. It looks like this:

{{#each objects}}
  {{@key}}:{{this}}
{{/each}}

A Backbone model’s property has to be accessed with the get(property) method.

var myObj = Backbone.Model.extend({ ... });
//don't do this
console.log(myObj.myProp);
 
//do this instead
console.log(myObj.get('myProp'));

If you’re trying to pass this object to a Handlebars.js, things start to get messy. If you’ve got a template like this

var myTemplate = Handlebars.compile("Property: {{ myObj.myProp }}");
console.log(myTemplate(myObj));

Handlebars won’t find the property, and nothing will be displayed. There’s a well-known workaround to this though, which is to convert the Backbone Model into JSON

console.log(myTemplate(myObj.toJSON()));

However this isn’t as readable as I’d prefer. If your collection gets really big, it can make your application slow, and in situations where you’ve got a Backbone Collection of Models, it doesn’t even work. A. Matias Quezada has a better solution, which is to tell Handlebars, “hey, if it’s a Model, use the get method”:

Handlebars.JavaScriptCompiler.prototype.nameLookup = function(parent, name, type) {
	var result = '(' + parent + ' instanceof Backbone.Model ? ' + parent + '.get("' + name + '") : ' + parent;
	if (/^[0-9]+$/.test(name)) {
		return result + "[" + name + "])";
	} else if (Handlebars.JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
		return result + "." + name + ')';
	} else {
		return result + "['" + name + "'])";
	}
};

That’s it. If you run that somewhere before your Template runs, Handlebars will be able to find the Backbone Model’s property.

In writing this post, I also came across Thorax.js, which probably also fixes this problem.