Archive for the ‘programming’ Category

Experiments in BrainFuck

Thursday, November 7th, 2013

I’ve enjoyed learning about esoteric programming languages ever since I began programming. One of my favorites is BrainFuck, which feels very much like programming a turing machine directly. It has 8 commands

  • +/- increment / decrement the current data cell
  • </> move the data pointer one position left / right
  • [ if the current data cell is zero, goto the matching close brace
  • ] if the current data cell isn’t zero, goto the matching open brace
  • . print the value of the current data cell
  • , receive input into the current data cell

[] are it’s looping / conditional construct, most interesting things are built using them.

Here’s what hello world looks like

As an experiment, I tried writing a BF interpreter in Mirah (mirah-brainfuck). At this point it’s 120 some lines of code and it totally works. My next crazy idea is to write an embedded brain fuck program that works like embedded ruby. You supply a .ebf file, and it gives you back a brainfuck program. Maybe I could use it to write a brainfuck web framework :p. For example, it might look like
Because this is a terrible experiment, I’ve been trying to write it in BrainFuck itself, which makes things really awkward/interesting. Because there is only one looping / conditional and it’s dependent on modifying cells on the data tape, figuring out how to compare input characters is really obtuse. But I like working with BrainFuck because it forces me to think weirdly about problems.

RockyMountainRuby 2013

Wednesday, November 6th, 2013

Rocky Mountain Ruby 2013 was at the end of September this year. I taught a workshop on writing external DSLs that was fun and difficult to put together. You can look through my class notes, if you are interested.

I also gave a short lightning talk on Muskox, a small library I’ve been working on. The talk’s up on Confreaks / YouTube now, which is awesome. The TL;DR is that populating input data structures willy-nilly from some generic language like JSON or especially XML can be dangerous, so you should prevent bad data from entering your system during parsing.

JRuby, Can’t Find File in Classpath (You Needed a Slash, in <= 1.5.6)

Monday, January 10th, 2011

Update: this is no longer valid as of the release of 1.6.0.

I was debugging this Dubious problem where a class I was sure was on the class path couldn’t be found(link). First, I tried compiling on my local checkout, and it worked oddly. Then I realized I was dumb, because the class was being picked up from the finished jar. So, I clobbered my build artifacts and then my local copy didn’t compile either.

I did some digging and found out why.

In the end it was because of how JRuby implements $CLASSPATH–the way you can modify the classpath from within JRuby. JRuby uses a subclass of URLClassLoader to implement it which gives it some possibly unexpected behavior. In particular, URLClassLoader treats anything that does not end in a slash as a jar. That’s why the class files in my build directory were not being picked up properly.

Lessons Learned:

Combat “It Works on My Machine™”. Clobber local generated files every now and again, and checkout a fresh tree from time to time to reduce the chance something local is weird.

Look for and ask for help when you don’t know something. First time I tried to figure out the problem, I just tinkered and compiled and tinkered and compiled. A little research would have saved some time.

TL;DR

If JRuby can’t find your classes and you think they should have been added to $CLASSPATH, make sure you have trailing slashes on your directory names.

Update:

This has actually been fixed, but not released yet: https://github.com/jruby/jruby/commit/8740a6b3ea946a1442dd7fa833aed3c30d82e23f

RubyConfX: Awesomesauce!? Hells Yes!

Thursday, November 18th, 2010

RubyConf X in NOLA last week was epic.

Epic.

Seriously though, I had a rocking time. I gave part of a talk, I talked to an amazing amount of awesomely smart people and just generally enjoyed my ass off.

It was the first RubyConf I’d been to, and I think the biggest conference I’ve gone to yet as a developer(ALA doesn’t count). I liked being in New Orleans again, surrounded by Jazz and a culture that contains people who think it is perfectly reasonable to drive around playing a trombone out of a car window. It’d be awesome if RubyConf was there next year.

My talk went pretty well I think, though I wish I’d taken more time to practice. It didn’t help that I hadn’t met my co-speaker, Bob Aman before the conference. But, it worked out pretty well, I think (speakerrate may or may not say otherwise). And we got some good feedback on twitter.

New Orleans, as I well know, is a great place to experience. I mean, look at this Po’Boy:

A Large Dressed Roast Beef Po'Boy

Running in the  RubyConf Xk(I ran the 5k) was fun. I totally expected to be unable to jog the whole way, but I did. And, I upped my pace on the second lap coming in at a respectable 29:41. Not bad for someone who has not gotten enough exercise in recent months. Oh, and @tenderlove ran too, in spandex no less:

Heroic Race Finishers

I definitely need to up my silliness for the next Ruby conference I go to. I feel out classed.

All in all, I had a freaking blast.

Mountain West Ruby Conf Rocked This Year

Monday, March 22nd, 2010

Mountain West Ruby Conf this year was awesome. I totally had a blast.

It was the third time I’ve gone to it. It was my first conference and still is my favorite. I got to meet Matz, I gave a lightning talk, I met all sorts of interesting people and I learned a bunch of new things. To quote Matz, MountainWest RubyConfはすばらしかった。

Matz & me

Things to revisit

  • Rack(1.1.0 in particular)
  • Chef(looks better than the last time I poked at it)

New things(to me)

  • RVM(rvm looks pretty handy, especially for cross vm library development)
  • Hubris(this might be a neat way to get into Haskell development)

My lightning talk

Usdo. Misspellings turn into gem-fu practice

Saturday, November 14th, 2009

Indian Paintbrush
A few weeks ago, during a period of frustration, I found myself repeatedly mistyping ‘sudo.’ So, in a fit of silliness I wrote a short script to insult me when I did it, and put it in the path.

Later, I packaged it as a gem, because a) I had never built a gem with an executable and b) gems are a great way to share things.

The end result?

http://github.com/baroquebobcat/usdo

It isn’t particularly smart, but I happen to find it funny. It also showed to me how far gem packaging has come. Gemcutter and Jeweler make building and distributing ruby gems freakishly easy. This is especially awesome in light of the recent announcement that gemcutter is going to be the default gems host(though at rubygems.org)

How I put it together

I ran jeweler to create the default directory structure and added a bin dir to it, for the files I wanted to end up in the path.

$ jeweler usdo
cd usdo
mkdir bin

I put my gist from before in the bin directory, and set up git.

Set up my gem info in my Rakefile

#...
Jeweler::Tasks.new do |gem|
  gem.name = "usdo"
  gem.summary = %Q{adds usdo command to ridicule mispellings of sudo}
  gem.description = %Q{...}
  gem.email = "ndh@baroquebobcat.com"
  gem.homepage = "http://github.com/baroquebobcat/usdo"
  gem.authors = ["Nick Howard"]
end

then did the jeweler gem initialization dance

rake version:write
rake gemspec
rake install

Testing it out:

$ usdo -l
----USDO----
    You mispelled sudo
    You can't do anything, you can't even spell sudo
    are you really sure you want to try running
    'sudo -l'????

Now that was working, to send it up for distribution.

Jeweler is awesome and now has gemcutter support. I followed the jeweler README‘s directions on uploading to gemcutter. Which makes pushing your gem as simple as

$ rake gemcutter:release

Awesome.

Check out my code if you want.

Scrubyt On A Server Workaround

Wednesday, November 4th, 2009

So you got some awesome scraper script and you want to deploy it to your server but you don’t want to have to install firewatir. Or, you don’t want to pull in the firewatir gem because you are not planning on using it. Whatever.

Problem: scrubyt
requires the firewatir gem

Hacky Solution: comment out the require

But where you ask?

/lib/scrubyt/core/navigation/agents/firewatir.rb line 1

This works on the current rubyforge version(0.4.1).
Recommended: Or, you could just use the version on github that is fixed(see commit)

grab the files via git or archive from github.com/scrubber/scrubyt

then

cd scrubyt-somehash
rake gem
gem install pkg/scrubyt-0.4.26.gem

Not as nice as using jeweler where you can just `rake install` it, but pretty nifty none the less.

OpenID Provider in Sinatra

Thursday, September 17th, 2009

As part of a personal project, I have been working on an OpenID Provider written in Sinatra.

The idea is to make it ridiculously easy to set up an openid provider within a rack middleware stack.

It’s just a single sinatra app without any tests using code I cribbed from the example rails implementation in ruby-openid. But, I intend on rewriting it once I get a better feel for how it should go.

The problem I face is a matter of interface, specifically application programming interface. I want it to be simple to use. But that is alittle complicated underneath.

Today I read The Little Manual of API Design(pdf), which made me think about my approach to building things in, I think, a good way. It talks about the goals of building a good API and boils it down to five things.

  • Easy to learn and memorize
  • Leads to readable code
  • Hard to misuse
  • Easy to extend
  • Complete

For my app, complete is pretty easy. I’ve boiled the interface down to two main things:

  • OpenID Store
  • User/Identity

First, for an openid app, you need an openid store to put all the associations, nonces etc.

Then, the meaty bit, the users and their identities. The stuff we are serving up. My current thought is to have one of the app’s parameters be a lambda or Proc that returns the current user, or nil when called with the request.env. So that using it would look somewhat like this.
It’s kind of ugly, but I like it better than some of my other options. One other way I could get the user to the app would be to use rack request hash. I could have users put the user object in the request env before the OpenID provider gets it and tell the provider where to look. But the problem with that is that I would need to add another middleware layer that added the user to the environment. That would look something like this:

Maybe that would be better.

Ultimately I just need to pick something and run with it.

The thing I wonder about though, is what is the most idomatic way of doing this. Using the most rubyish, most Rack like interface would make it easier to learn and memorize.

RubyNation Etc

Tuesday, June 16th, 2009

I am back from RubyNation and reapplying my nose to the grindstone, wheel to the asphalt and hands to the keyboard. I am still planning a big summary and commentary post based on my notes, but as I started working on that, I realized it might need more than one night to see to completion.

So, I give you this:

Me
Hyatt Ceiling Pastry and Me in My Hat

Carrot Cake(Thanks Hyatt catering)

Mmm Carrot Cake

And Lys, the cat who is strangely fond of white ceramics.

Lys, Sink and You

Tune in some indeterminate point in the future for a fuller update. Or, you could just read this guy’s take.

Twitter Search from Javascript

Tuesday, May 5th, 2009

I was working on building a js twitter search app for jazzfest, which I didn’t finish in time really, but I liked the interface I came up with.

Twitter = {
  _callbacks:[]
};
Twitter.search = function(opts){
//opts.q
//opts.callback
var callback = function(data){
    opts.callback(data); 
    Twitter._callbacks = Twitter._callbacks.without(callback)
  }
var len = Twitter._callbacks.push(callback)
document.body.insert("