Posts

A modular, test-driven Sinatra template

I recently found a (somewhat old) but great repository. The structure really helps you build modular Sinatra apps and has the basics needed for you to jump in and start building.

However, after discovering the joys of the SimpleCov gem, which generates code coverage reports in HTML, I wanted to tweak this already-awesome repository. Also, I wanted a few more Rake tasks to help with everyday development.

So, here’s my fork with some improvements.

...

Colby: Small Wrapper and Functions for Persistent, Immutable Data

Today, I cleaned up an earlier project I’d left hanging around called Colby. It’s a little wrapper around the data structures that the Hamster gem provides, and it adds some functions to make working with those data structures a little similar to the Clojure-way.

At this point, Colby is very experimental and is only at a 0.0.1 release to reflect its brittleness. However, all specs pass currently and it’s useable as-is. I’ve pushed the initial release to RubyGems, so you can start using Colby by just doing gem install colby.

...

Project Euler Problem 3 using Clojure 1.5 and a lazy technique

The problem: find the largest prime factor of 600851475143.

I used the Sieve of Eratosthenes and Ruby to solve it last time.

This time, I tried a lazy, functional approach. The result was an efficient, straightfoward solution that involved no sieve or any static/full-realized data structure at all! I make a recipe for the kind of numbers I want and then take just as many as I need in order to finally output the answer.

...

RepoMap: track your local git repositories

Github link: RepoMap

There are hundreds of git repositories stored at various folder depths all over my computer. Organizing them, remembering where they are or determining whether I still have a working copy cloned from a remote repository is a chore.

So, why not have a map of where all my repositories are? Should be simple enough to keep a YAML file with a map from paths to repository names.

After some hacking on a flight (after having finished finals and an interview), I made a little Ruby CLI program and found it to be useful enough to expand into a tiny little gem. It’s still very basic, and specs aren’t where they should be, but the essentials are there.

...

Sherlock: A Chrome extension to Search Inconvenient Websites

sherlock-chrome-extension

…and it was really quite simple to do! Sherlock is pretty stupid: all you can do is click on him to specifically google the current tab’s website. So, if you find yourself adding “site:example.com” to your Google searches often, this should be a nice shortcut.

Here’s the MIT-licensed code: https://github.com/gnarmis/sherlock. Download the install package here; double-click to install. I’ve even put this puny release on the Chrome Web Store, but it’s not showing up yet.

...

Ready Spam Detection in Twitter - Extended Abstract

img

Marcel Flores, Maciej Swiech and I have been working hard on our machine learning project, in which we are asking whether we can detect twitter spammer upon account creation, before they’ve made a single spammy tweet. Our results have been promising and I’m posting here a link to the project website: Ready Spam Detection in Twitter.

At the above link you’ll find our project details, approach, results, analysis, and an accompanying extended abstract in ACM format.

...

Hack Northwestern Updates

from-our-second-ever-hack-night

It’s been a busy few days at Hack Northwestern! We got a new home, announced a hackathon sponsored by imo, and just wrapped up a tech talk / hack night by folks from Seelio yesterday!

It’s humbling to note that this started as an idle suggestion last spring, and then a quick get-together of around 7 students. Here’s an article about only the second such hack night, which exploded to around 30-40 people (even some from UChicago!). The idea behind this effort has stayed constant: fostering student creativity, entrepreneurship, and hacker spirit. It’s a testament to Northwestern’s active entrepreneur and hacker community that Hack Northwestern is seeing such quick validation of its efforts.

...

Ruby Method Missing

I had an idea to try and bring a tiny bit of Clojure’s ideas of hash-maps to Ruby’s hashes. I came up with a simple monkey-patch that allows you to truly use hashes as functions of their keys. In the process, I’ve come to really dig method_missing, although debugging it can be a chore. Word of advice: always implement respond_to? with your method_missing. Principle of Least Surprise, people.

# In Clojure, hash-maps are truly functions of keys to values.
# So you can do `(:a {:a 1})` and get `1` as the result
# Why not put this in Ruby?

# access keys of a hash like a function

class Object
  def respond_to?(method)
    if (method.to_s =~ /^_.*/) == 0
      true
    else
      super
    end
  end
  def method_missing(name, *args, &b)
    if (args.count==1) && b.nil? && name[0]=="_" &&
        args[0].has_key?(name[1..-1].to_sym)
      args[0][name[1..-1].to_sym]
    else
      super
    end
  end
end


hash = {:a => 1}
(_a hash) == 1 #=> true
(_a hash) == (hash [:a]) #=> true

# the underscore serves to prevent naming conflicts to a degree

On Ruby 1.9's Flexible Syntax

Over the past day or so, I’ve been playing around with small bits of Ruby after spending most of the summer in Clojure-land. There’s great similarities in both Ruby and Clojure (functional style, dynamic, etc), and both are very practical choices. Clojure gives you amazing access to JVM land while not forcing you to pull your hair out, and Ruby grants you a good selection of libraries and amazing tools for web development.

...

Piping Arguments Through Multiple Functions in Ruby

After asking #ruby what the equivalent of Clojure’s -> macro could look like, I compiled the collective solution into a gist. You might find this useful somewhere, especially when you have multiple transformation functions operating on the same basic data structures.

# piping example in Ruby

def foo(data)
  data[:a] += 1
  data
end

def bar(data)
  data[:b] += 10
  data
end

def pipe args, *methods
  methods.reduce(args) { |a, m| send(m, a) }
end

hash = {:a => 0, :b => 0}
pipe hash, :foo, :bar
#=> {:a=>1, :b=>10}