In my previous blog post I mentioned how much fun I have in creating a small web project with Sinatra, Heroku and MongoHQ and today said project finally went online:
I’ve been toying around with the idea for a small website lately, but never actually got around to do it. In an attempt to clear out my personal to do list I finally motivated myself to start yesterday, and I have to say the following combination of tools is not only insanely efficient, but also really fun to work with:
Sinatra, “a DSL for quickly creating web applications in Ruby with minimal effort”. I recommend using it in combination with Mongrel, Shotgun and Haml.
MongoDB, “a scalable, high-performance, open source, schema-free, document-oriented database”. In this specific case I used MongoHQ for the database hosting, since they were nice enough to provide me with a beta account. I stored all the connection information for MongoHQ in environment variables as described above:
With this combination I was able to go from zero to mostly finished (I still need to write some of the content and make/steal a stylesheet) in very little time, while actually having fun! Thanks everybody for providing the Ruby community which such awesome tools! :-)
Io is a small small, prototype-based OO programming language that’s fun to play with. Unfortunately there’s not too much information about it out there and quite predictably googling for “io” gets you lots of results you are not looking for. But today I remembered that why once did a nice little article on the language, and thanks to the amazing Internet Archive I actually managed to find it! So without further ado, here we go:
I’m currently toying around with some ideas for Twitter bots etc. so I had a look at some of the available gems out there. Sometimes Ruby makes stuff almost too simple… ;-) Here are a few nice snippets of what you could do with a couple of minutes time and some Ruby:
Automatically translate all tweets for a given keyword:
Daemonize the above:
Alternatively you can also follow a userid instead of a keyword and daemonize the whole thing, thus making it super easy to create a bot which posts translations of Matz’s tweets:
If you put this into a file like transbot.rb, the command ruby transbot.rb will give you all the regular daemon commands like start, stop, etc.
Ruby is trendy:
Curious of getting a list of current trends on Twitter, without hashtags? There you go:
Combining all these ideas you could easily write a bot that follows the current Twitter trends and posts translations of them in only a couple of lines. :-)
After upgrading to Snow Leopard I had quite a bit of trouble with my MacPorts installation, since most builds just failed with configure errors. After the obvious first steps of installing a newer version for SL and making sure I have the most recent XCode, I was a bit stumped until I finally found this site:
After finding this guide – which btw really solved my problems – I felt a bit stupid, but since I didn’t have to do anything after the Tiger→Leopard upgrade, I really didn’t expect this.
unsavory is now available as a gem, which means that you can comfortably install it like this:
12
$ gem sources -a http://gems.github.com
$ sudo gem install citizen428-unsavory
Rubygems will automatically create a wrapper-script named “unsavory”, which you can use to start the program so you finally can get rid of all these outdated bookmarks.
After reading Scripting Java Libraries With Ruby on the Engine Yard blog, I decided to translate the two example programs to Clojure. Please keep in mind though that I’m pretty much a Clojure newbie myself, so the code shown here might not be very idiomatic. In case you have any suggestions on how to improve the code examples presented here, please leave a comment, thank you! To get the most out of this post, you should probably read the section on Java Interoperability in Mark Volkmann‘s excellent Clojure tutorial first.
A MIDI Keyboard
The first example listens for a line of text which then gets converted into a playable MIDI sequence. Let’s start with importing the classes we need.
Next we set up a loop and an exit clause. In case the user enters the word “exit”, we call System.exit to shut down all the threads that got started by Java’s MIDI support.
The main part of the application constructs a new MIDI sequence by creating a NOTE_ON event for every character in the entered text and then finishing the sequence with a STOP event. The original article states that it favors simplicity over efficiency, so the same also holds true for my “translation”.
While the first example was fun, it wasn’t as interactive as it could be. Therefore the second program launches a GUI window to receive keystroke events, turning a note on or off when a key is pressed or released. Since we don’t have to create MIDI sequences this time, our import statement becomes a lot shorter:
This example directly accesses the system synthesizer. The following lines open the default synthesizer’s channel 0 (and also create a JFrame object which we’ll use later):
We then launch a GUI frame as a simple and cross-platform way to receive keystroke events. The KeyListener interface is implemented using Clojure’s proxy macro, using keyPressed and keyReleased events to turn notes on or off.
I hope this article helped to illustrate how easy it is to use the plethora of existing Java libraries from Clojure. In case you have any feedback or questions, please feel free to leave a comment!
After reading Scripting Java Libraries With Ruby on the Engine Yard blog, I decided to translate the two example programs to Clojure. Please keep in mind though that I’m pretty much a Clojure newbie myself, so the code shown here might not be very idiomatic. In case you have any suggestions on how to improve the code examples presented here, please leave a comment, thank you! To get the most out of this post, you should probably read the section on Java Interoperability in Mark Volkmann’s excellent Clojure tutorial first.
A MIDI Keyboard
The first example listens for a line of text which then gets converted into a playable MIDI sequence. Let’s start with importing the classes we need.
Next we set up a loop and an exit clause. In case the user enters the word “exit”, we call System.exit to shut down all the threads that got started by Java’s MIDI support.
The main part of the application constructs a new MIDI sequence by creating a NOTE_ON event for every character in the entered text and then finishing the sequence with a STOP event. The original article states that it favors simplicity over efficiency, so the same also holds true for my “translation”.
While the first example was fun, it wasn’t as interactive as it could be. Therefore the second program launches a GUI window to receive keystroke events, turning a note on or off when a key is pressed or released. Since we don’t have to create MIDI sequences this time, our import statement becomes a lot shorter:
This example directly accesses the system synthesizer. The following lines open the default synthesizer’s channel 0 (and also create a JFrame object which we’ll use later):
We then launch a GUI frame as a simple and cross-platform way to receive keystroke events. The KeyListener interface is implemented using Clojure’s proxy macro, using keyPressed and keyReleased events to turn notes on or off.
Last but not least we display our frame to let the fun begin.
1
(.setVisibletrue))))
Full source for the second example
I hope this article helped to illustrate how easy it is to use the plethora of existing Java libraries from Clojure. In case you have any feedback or questions, please feel free to leave a comment!