Skip to main content

Posts

Showing posts with the label Ruby

Using a Raspberry Pi as a MIDI USB/5-pin bridge

In my constant... need... to get everything music instrument related to communicate with each other, I wanted to look into ways to get some of my keyboards/synths with only MIDI over USB to talk to devices with regular good old-fashioned 5-pin MIDI ports from the eighties. Cables! First I had a quick look at off the shelf solutions. The most interesting one being the Kenton MIDI USB Host – providing MIDI host functionality for USB devices as well as regular MIDI in and out in a small box. Unfortunately it is rather expensive (~125 €) and a reliable online source warned me that it was not entirely stable in collaboration with my OP-1, so I started thinking of more... home-grown solutions. I decided to try to use my old Raspberry Pi and see if that would serve as a USB host with a borrowed MIDI USB adapter. (Thanks Simon.) A cheaper, and, as an added boon, a nerdier solution. Step 1: Get the USB MIDI device up and running This was the easy part. The device I have been lent ...

Introducing Noisevote, or "A Democratic Rock Concert"

A little duo I'm part of these days, 1bisHill (What's with the name you ask? It is a long story...), was asked to play some songs at a gathering at my work the other day. (With a guest star from Brazil.) So, without thinking, I asked my partner in crime, and he said yes, so I said yes. And then I thought about things, and realised that we have lately drifted towards more and more... noise - less, what might be regarded as, ... music ... Perhaps not compatible with playing at people at a party at work. So I got scared. And then I did what I always do when I get scared - tried to find a programmatic solution! This time I created a tiny web service where people could vote for more or less noise. Noisevote, if you want. Pedals with  NSFW names like Big Muff and Swollen Pickle en masse. Basically, server side, it is a really simple Ruby and Sinatra web service with a sqlite database to store votes (running on my Raspberry Pi at home). Client side it is Bootstrap and D3.js w...

Board Game Geek Data - Sliced and Diced

Inspired by the discovery of the API for the BoardGameGeek website , I decided to play around with some of its data by some Ruby and JavaScript scripting. One thing I wanted to look at was the interconnectedness of game mechanics, but first I wanted to find alternative ways to look at how to measure ones "use" of ones game collection, and the associated question of "can a board game collection ever be too big?". H-index and play per game For example, the concept of H-index  is being discussed quite a bit on the ’geek. In short this entails sorting your games by number of plays and finding the last entry in the list with a number of plays higher than (or equal to) the position in the list. It’s rank is your H-index. (This does, of course require one to  meticulously  log ones plays, but that is another story… For example my overall BGG H-index for the last 12 months is (or seems to be) a measly 5.) Another concept used is to take the number og games owned an...

Instaworries -- and a resulting mass unfollowing hack

The other day I noticed/remembered that I had an instagram account. And, according to my sister, I was quite active on it. Apparently. I kept following new persons. That suprised me, so I logged on and investigated. “I” followed 974 (or so) persons I had never heard of… My first instinct was to delete the account.* But that would have been too easy, so I decided to investigate the Instagram-API to see if that could help me “get rid” (most) of the people I followed. And lo and behold I found this page  - very promising, so I registered an app to get access to this. Since Instagram uses OAuth that landed me a client ID and a client secret. Not awfully familiar with the innards of OAuth, I realized that for anything to work I would need an access token. Since this script was just for personal use, I figured I could just generate this once(ish) and use that to get access to my account. I followed the instructions on this (page):  http://dmolsen.com/2013/04/05/generating-access-...

Implementing k-means clustering in Ruby

Inspired by the partitioning problem I set about to implement a well known algorithm, k-means clustering, from memory, for fun! ... and, for science... Interestingly, this is somehow the opposite of the partitioning problem. In the partitioning problem we tried to maximize variation of categorical variables within groups, whereas here we're trying to find groups of elements that are the most "similar" to each other in a n-dimensional continuous space. The main idea of k-means is the following - if you have a bunch of elements and a given number of clusters: Create the initial clusters randomly within the space spanned by the elements (typically (always?) you would pick randomly from your elements). Lob all elements into the cluster with the nearest center (using some Euclidean distance metric typically). Recenter each cluster on the average of its elements,  If necessary move the elements to their now nearest clusters.  Repeat the "re-centering" and mov...

Sequel on the Pi - and how to access Access DBs from Ruby (in parenthesis)

Recently I had to liberate some data from an old MS Access DB, so I discovered a gem of a Ruby gem called " sequel " that generalizes database access (for many different databases) - ORM style. Most excellent, so I figured I wanted to run it on my raspberry pi to access various sqlite databases I have lying around. Assuming you already have ruby (1.9) installed, you can simply do (in any Debian flavored Linux, for that matter): sudo apt-get install sqlite3 libsqlite3-dev and sudo gem install sequel sqlite3 So now one can fire up irb and do things like: require 'sequel' db = Sequel.sqlite('some_db.db') Etc . (This will, of course, not let you access MS Access DBs on your pi, as they rely on the "win32ole" gem - only available on windows, and the " Microsoft Access Database Engine " - also only available on windows. More info on how to connect to those kinds of database can be found here  - under 'ado'.)

Counting in Ruby Revisited - Benchmarked

After writing the post a while back on counting in Ruby I wondered if the two different implementations I outlined had similar runtimes. Inspired by this post by makaroni4  I went ahead and wrote the following little "benchmarker": Loading... So, you see the block centered implementation is about 20% faster than my initial one. Interesting.

Partitioning - or, students into n groups - in Ruby

A while back a friend of mine asked if I could automate the creation of groups in a class of students - to maximize the variation within each group, but minimize the difference between them. This sounded like an interesting problem, so I set about to solve it in my own naive experimental Monte Carlo (inspired) way - without looking into ways this has been solved (surely elegantly) before. The result was this little Ruby script: Loading... First I just require some code I have previously written. (I guess I really should make them into gems or something instead of copying code around...) names.rb  tries to guess sex from a person's name and/or title, and countries.rb  maps countries to continents, regions etc with some fuzzy matching of names. (I'm looking at you Democratic Republic of Kongo,  Koreas  etc.) Easy-peasy. Then I set some standard variables. (Maybe I'll make this dynamic in the future, why not?) The most interesting entry here is the classifiers ...

Unicode characters in irb on OS X

I noticed that my RVM-handled irb in OS X (Mountain Lion) didn't like Norwegian characters today. So instead of an 'ø' , I got '\U+FFC3\U+FFB8' during entry, and nothing afterwards. Like so: Not really satisfactory. After googling: "os x irb unicode characters rvm" I found the answer on stackoverflow . Almost. My adapted solution was this: So now I can do: Better. (Not quite sure why the '--verify-downloads 1' part is needed, but it did the trick.)

Un, deux, tscha-tscha - or Counting in Ruby

Inspired by my little son's eagerness to count when he sees groups of things that (he thinks) can be grouped together these days, I wanted to post my often used, tiny, fairly naïve extension to Ruby's Array class that I call count_by . This might not be the most beautiful way to solve this, but it is fairly readable and is leveraging some key aspects of the Ruby language - open classes, or monkey patching if you must, and dynamic calling of functions. Basically I open up the class Array and add a count_by function to it - that takes as argument the name of the function one wants to count the elements by. The function itself starts off by instantiating a Hash , called map , with default values of 0. For each element in the Array I then call the function by the name provided and increments the  entry in map  corresponding to the result. Like this, if I have a array of, let's say Olympic medalist objects with functions nationality in list , I can ...

Kjært barn har mange navn - or, the story of how I discovered Ruby's each_cons

Reading the Pragmatic Programmer's PragPub from March (I must admit I don't have/take the time to read them regularly enough) I stumbled upon Scala's sliding function on Arrays. This creates a sliding window of consecutive sub arrays of an array. So an  Array(1,2,3,4).sliding(2) would produce an iterator of arrays (1,2), (2,3), and (3,4). Useful! While googling around to see if Ruby has something similar I stumbled across the windowed function in F# that pretty much works the same way, before finding Ruby's each_cons . The first thing I did to test this was to revisit an old (fairly obvious, brute force) seven line Ruby solution (from back in the day when I had just discovered the language) to an Euler problem , and replaced it with the following (admittedly still brute force) one liner (where 'number' is a 1000 character long string containing only numbers): Unlike Scala where you would have the function sliding on string objects, we need to call chars ...

Parenthesis (and dots) (in Scala (as compared to Ruby))

I must say that I quite like what I encounter of the Scala language in Martin Odersky's online " Functional Programming in Scala " course at EPFL (as well as the course itself!). It is almost as expressive as my favourite language these days, Ruby . (Of course it will never really be able to match it on expressiveness, I'd say (the latter being duck typed, and the former being strictly typed (even with a most excellent type inference)).) One detail that I like about both languages is that parenthesis are (most of the times) optional. (By "(most of the time)" I mean that you (obviously) can't really skip them if it leads to problems with operator precedence.) This (can) really lead(s) to more readable code. Scala, however, (seemingly) has some inconsistencies when it comes to when  they are needed. Most notably the println  (and similar?) function(s). ((Quite possibly inspired by scripting languages) you have a quick way to display text at the user w...

Tabular data from PDFs with the help of Ruby

Sometimes I need to get data out of tables buried in PDFs, so I've written a tiny adaptable Ruby hack for when copying and pasting doesn't cut it - exploiting a couple of excellent libraries. Here's an example. Loading... (Admittedly not the most elegant of scripts, but hey! it gets the job done.) What I do first here is just load up the relevant libraries. (The pdf-reader I use is this one: https://github.com/yob/pdf-reader - installable with a simple command line 'sudo gem install pdf-reader' if you are using Ruby 1.9. (Drop the sudo if you're on Windows.)) Next up is to instantiate input pdf and output table. Straight forward enough. By examining the pdf I found the data I was interested in on pages 42 through 69, so a simple call to pages give me those -  pdf_reader . pages [ 42 . . 69 ] . I'll go through the text on each of them line by line and decide if they contain what I want based on regular expressions. These are my simple rules (in th...