Coding Hell

Programming and stuff.

File Handling: Never Trust Default Encoding Settings

Whenever you are doing any kind of file shenanigans, you should always explicitly set the expected encoding of the file you are reading from. Most programming languages’ standard file handling libraries use encoding settings by default which do not lead to the desired behaviour.

ReChat for Twitch

A little while ago, I released an extension that adds a nifty feature to Twitch (Twitch is the video streaming platform for games that was acquired by Amazon last year for $970 million): ReChat for Twitch.

Twitch does not only allow users to see live video streams but also records them so that users can watch them later. The one thing you miss out on when you watch a recorded stream is the chat. This is where ReChat comes into play: ReChat allows you to see the recorded chat messages as if it was a live stream.

Migrate From RVM to Rbenv

I switched from RVM to rbenv for managing Ruby versions on my development machine. RVM does not follow the classic UNIX philosophy; RVM does too many things (Ruby environment management, Ruby compilation and installation, …). It feels kind of bloated to me.

How and When (Not) to Use Exceptions

Did you ever ask yourself, how you should “design” your exceptions? Ever wondered if you understood the idea behind exceptions? Do you know, what you should use exceptions for? This article aims to clarify how and when (not) to use exceptions.

CASino: An Easy to Use Single Sign-on (SSO)

Back in 2012 we were looking for a solution to replace our company-wide SSO with something more robust and future proof. After comparing various SSO technologies and standards, we decided to go with a CAS-based solution. CAS is an open standard and there are libraries for a wide range of client platforms (e.g. Ruby, Java, PHP, .NET, Apache).

RubyCAS-Server

We didn’t have any knowledge in running a big Java web application, so we decided to use the RubyCAS-Server. Running and maintaining Ruby web applications was (and still is) our daily business.

After some weeks and months of usage, we encountered several bugs and shortcomings in RubyCAS-Server. What’s worse: RubyCAS-Server does not fully adhere the CAS standard. “No problem, it’s open source!”, you’re probably saying. That’s what we thought too… After looking through the source code, we almost cried: it’s a mess and not well tested. But we didn’t give up here and began to hack our changes in. We then opened issues and pull requests but did not get any feedback. Very frustrating! The project seemed dead to us.

CASino

That’s when we formed an organization (rbCAS) and decided to build our own Ruby on Rails based CAS server: CASino

It took us just a few weeks to implement basic CAS support. We had a clean code base, a responsive user interface and 100% compatibility with the CAS standard.

Anvil: Menubar App for Managing Pow

If you are (like me) developing Ruby on Rails applications under Mac OS X, you are probably already familiar with Pow. Pow is doubtless the easiest way to run multiple Rack applications on your development machine.

The only thing not that intuitive to me was the whole linking progress as it was easy to lose track of what you already “deployed” to your local Pow installation.

Not anymore, Anvil to the rescue!

It adds a little menubar icon that gives you access to your Pow apps, allows adding new ones and lets you easily execute various Pow commands (start, stop, …). Finally a nice GUI for Pow! :)

Hide Actual Database IDs From Your Users

In a project I’m working on in my spare time, I wanted to hide / obfuscate the databases IDs of my ActiveRecord model. I didn’t want the users to find records by simply incrementing a number in the URL.

I ended up writing my own Gem, which manipulates ActiveRecord::Base to hide the actual ID. Rails’s path functions still work as expected. The only thing I had to add to the actual project was the following line:

1
2
3
class User < ActiveRecord::Base
  encrypted_id key: '5gA6lgr5g3GOg7EOQ1caYQ'
end

I open sourced the project on GitHub (pencil/encrypted_id) and published it on RubyGems.org.

Tic-tac-toe AI Part 4: Learning by Doing

The second bot doesn’t know any strategies to win tic-tac-toe. This bot will simply try things out and give each move a score once the game ended. If the bot wins, all moves that lead to this situation will get score+1, if he loses, all moves will get score-1. The default score is 0.

So when it is this bots turn, it will decide based on the scores:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private Map<Integer, Move> scores = new HashMap<>();

private Move chooseMove() {
  Move bestMove = null;
  for (int i = 0; i < 9; ++i) {
      if (gameGrid.getField(i) == Field.EMPTY) {
          GameGrid possibleGrid = new GameGrid(gameGrid);
          possibleGrid.setField(i, playersField);
          int hashCode = possibleGrid.hashCode();
          if (!scores.containsKey(hashCode)) {
              scores.put(hashCode, new Move(hashCode));
          }
          Move move = scores.get(hashCode);
          if (bestMove == null || move.score > bestMove.score) {
              bestMove = move;
          }
      }
  }
  return bestMove;
}

We then have to save the performed moves, so that we later can set the score accordingly:

1
2
3
4
5
6
7
8
9
10
11
12
public void reset() {
  int score = 0;
  if (gameGrid.getWinner() == playersField) {
      score = 1;
  } else if (gameGrid.getWinner() != null) {
      score = -1;
  }
  for (Move move : performedMoves) {
      move.score += score;
  }
  performedMoves.clear();
}

That’s it! So the learning AI is even simpler than the Minimax bot.

Java 7: Try-with-resources

A nice Java 7 feature is the new try-with-resources statement.

When you did some SQL things back in the Java 6 days, you often ended up with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
try {
  Connection con = createConnection();
  // do something
  Statement stmt = null;
  try {
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT ...");
    // do something with rs
    rs.close();
  } catch (SQLException e) {
    e.printStackTrace();
  } finally {
    if (stmt != null) {
      stmt.close();
    }
  }
} finally {
  con.close();
}

Now with Java 7, this got a lot easier and shorter:

1
2
3
4
5
6
7
8
9
try (Connection con = createConnection()) {
  // do something
  try (Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("SELECT ...")) {
    // do something with rs
  } catch (SQLException e) {
    e.printStackTrace();
  }
}

Awesome!

If you want to write a class that supports this syntax, you may take a look a the AutoClosable interface.