jemnotesversion 2 / showing all 180 or see latest only

Aug 3
From here comes the ability to disable Mac OS X screenshot drop-shadows.
defaults write disable-shadow -bool true
killall SystemUIServer
and then to re-enable:
defaults delete disable-shadow
killall SystemUIServer
Jun 6
From here come some interesting hints.
  • In bash, type alt + ., or esc + . to append the last argument from the last command. Repeat to go back up the command stack. You can also use alt + 1 alt + . to get the first command, etc.
  • Use du to summarize disk usage for directories only, via du -h –max-depth=1.
  • Use readline to edit a command in an editor: ctrl-x ctrl-e. Make sure you set $EDITOR. Can also use fc (fix command) to do this for the last command.
  • To create or wipe a file, use > filename. Compare: touch filename.
  • Quickly look at ascii character codes with man ascii.
  • Nice simple stopwatch with time read (then enter). Or for a countdown timer, read -t 5 for 5 seconds.
  • Use pushd /path/to/dir instead of cd, then popd to get back.
  • Use alt + b, alt + f to move forwards/ by a word. You can use numeric arguments as well.
  • Kill a word backwards with ctrl + w, then ctrl + y to yank. Rotate kill ring with alt + y. Kill word forwards with alt + d.
  • Kill backwards to the start of the line with ctrl + u.
  • Use alt + t to transpose the last two words.
  • alt + \ will remove all whitespace around the cursor.
A nice trick to copy your ssh key to a remote machine:
ssh remote-machine 'cat >> .ssh/authorized_keys' < .ssh/
Tunnel ssh via an intermediate host, or otherwise use a pseudo-tty:
ssh -t reachable_host ssh unreachable_host
May 18
This little fragment extends the ruby string class to check that all round, square and curly brackets occur in appropriate couplings.
class String
  def brackets_match?
    stack = []
    each_char do |c|
      case c
      when '(', '[', '{'
        stack << c
      when ')'
        return false unless stack.pop == '('
      when '}'
        return false unless stack.pop == '{'
      when ']'
        return false unless stack.pop == '['
and the tests:
should 'test matching brackets' do
  assert '(this)'.brackets_match?
  assert '(this)(fine)'.brackets_match?
  assert '(this)(fine)'.brackets_match?
  assert '[this][fine]'.brackets_match?
  assert '(this){fine}'.brackets_match?
  assert '(thi(s)){fine}'.brackets_match?

  refute '(thi(s)((fine)'.brackets_match?
  refute '{this){fine}'.brackets_match?
  refute '{this)(fine}'.brackets_match?
  refute '{this)(fine}'.brackets_match?
Apr 27
I have now several times had hard-to-debug problems that came down to field size limits. When developing with sqlite, it’s easy to forget about field limits. When deploying to mysql, these come into play. Don’t forget about them!
Apr 13
In git, to clone a remote branch, fetch it to a branch of the same name:
git fetch origin demo:demo
Apr 11
From here comes a nice way of raising 404 errors in rails.
Inside application_controller.rb:
class Error404 < StandardError

class ApplicationController < ActionController::Base
  rescue_from Error404, with: :render_404

  def render_404
    respond_to do |format|
      format.html{render file: "#{RAILS_ROOT}/public/404.html", status: 404}
      format.all{render nothing: true, status: 404}

  # etc.
Then, inside the relevant controller:
raise Error404
Also, as explained on the above website, the last line true in render_404 means you can
render_404 and return
Apr 11
I was trying to figure out why protect_from_forgery didn’t seem to be working in rails. I was trying to get rails to fail by changing the authenticity_token passed to the server during ajax requests. However, I had forgotten that XMLHttpRequests are subject to the same origin policy (SOP), so, for ajax, you don’t need a valid authenticity token to be sure the request is ok.
I once learnt all this, but forgot when trying to protect my application.
Apr 8
It’s easy to set up memory caching in rails. This page has some details. The only steps from it that I required were
# Inside config/environment.rb:
require 'memcache'
CACHE = ''

# A helper method:
def data_cache key
  unless output = CACHE.get(key)
    output = yield
    CACHE.set key, output, 1.hour

# When using it:
data_cache 'tag' do
Apr 7
A few notes for rails:
  • If you are using serialize, you can’t use the self[:attr] form. You must use self.attr. If necessary, use alias to make this happen.
  • rake stats gives some interesting statistics about your rails project.
  • Instead of read_attribute :name you can just use self[:name]. Similarly for write_attribute and []=.
Mar 29
From the simple and obvious, yet extremely useful department: add
$DEBUG = true
to the front of your code, and threads will abort on exceptions, yielding the useful error trace. Details here.
Mar 28
Perhaps || should always be used in Ruby in preference to or—I thought it was less attractive than or to look at, but check this out:
puts false or true   # interpreted as puts(false) or true
=> false
puts false || true   # interpreted as puts(false || true)
=> true
I knew that the precedence for or was low in Ruby, but I didn’t realise it was this low.
Mar 28
In terminal:
sudo defaults write /Library/Preferences/SystemConfiguration/ \
  'Kernel Flags' 'arch=x86_64'
Mar 27
Here are some ways of resolving a conflicted merge in git.
git checkout --theirs filename
# or
git checkout --ours filename
# or, for an interactive interface
git mergetool
Mar 27
In rails, instead of read_attribute(:attr_name) you can just use self[:attr_name]. Similarly for write_attribute.
Mar 27
A good article here about the difference between alias and alias_method in ruby. It suggests:
alias_method :new_name, :old_name
in preference to
alias new_name old_name
Not sure I agree which is better, but at least this makes the differences clear.
Mar 19
This page summarizes ‘ruby-debug’, which looks like a nice useful gem. I particularly like this line:
(rdb:1) b app/controllers/my_controller.rb:10
Mar 8
Having spaces in filenames is, in general, reprehensible. However, if you’re dealing with something like an iPhoto Library, it’s not up to you. To deal with this in rsync, note the following, from this page.
# Copy three separate files on host to tmp.
rsync -av host:'file1 file2 file3' /tmp/

# Copy one file from host to tmp. ? matches any character.
rsync -av host:a?long?filename /tmp/
rsync -av host:'"a long filename"' /tmp/
rsync -av host:'a\ long\ filename' /tmp/
rsync -av host:a\\\ long\\\ filename /tmp/
Mar 7
Attach a certain remote repository within another git repository using
git submodule add /path/to/remote local_name
then, after cloning this new repository-with-a-submodule, tack on a few more steps, like this:
git clone /path/to/repo-with-submod
cd repo-with-submod
git submodule init
git submodule update
Mar 6
Suppose you have this:
class Widget < ActiveRecord::Base
  def errors
where errors is a supposedly harmless, unrelated method that returns errors related to your Widgets. Then, try the following in your controller:
wdgt =!
and you will get a message saying something like
NoMethodError (undefined method `full_messages' for #<Array:0x00000102344c68>)
This is because rails tries to print out wdgt.errors.full_messages, and fails to find full_messages on the array [1,2] returned by your custom errors method.
Thus, my rails hint: don’t create an errors method in an activerecord model.
Feb 25
From here: in latex, you can type \verb|{| and get a real brace in a typewriter font. But if you type {\tt \{ }, you get a sans-serif replacement. In code:
\verb|{|    % nice, real brace.
{\tt \{}    % hideously replaced by sans-serif glyph.
two ways to fix this:
% or
{\tt {\char '173} }
Feb 25
  • Include the lmodern package for “enhanced versions of the Computer Modern fonts”, including “enhanced metrics and glyph coverage”. The package does seem to improve kerning, and also improves performance when you
  • Include upquote (or textcomp, not sure of the differences), and you get a \textquotesingle. That is good for writing computer code in latex.
Feb 24
Finally, a decent way of controlling preview on the mac via the apple remote. iRed Lite works well, and finally makes it possible to use the remote for texed presentations.
Feb 24
To retrieve a list of files matching a certain pattern, and/or in a certain directory, use Dir.glob.
Two examples:
Dir.glob 'dir/*'
Dir.glob 'dir/*.rb'
Feb 21
I have long used ^x^l to complete lines in vim. I only just realized, thanks to the always-excellent vim help pages, that if you do it multiple times, it keeps completing further lines under the previously-matched line. Not only that, but it will offer choices based on the entire block matched up until then.
Feb 21
Call it the principal of least surprise, or don’t, but so often in Ruby what I hope is the case, actually is the case. Among many examples I’ve found today alone, take the word super. This calls a method of the same name, using the implementation next up the class hierachy. This is as expected. You can pass arguments to super if you like, as expected.
And, as hoped, if you call it without arguments, it will pass the original method’s parameters. Nice.
Feb 16
Apparently, “the memmon executable monitors a linux process and provides summary statistics about the memory usage of the process after it terminates.”
Feb 16
When you hit o for a newline in vim, you often get the comment character prepended to your line. You can avoid this using
set fo-=o
Feb 1
timeago is a jQuery plugin that makes it easy to write automatically updating messages that say how long ago something happened.
Feb 1
The syntax for serialize in rails is
serialize attr_name, class_name=Object
thus, you can’t chain serialize :attr_1, :attr_2 like you can with attr_reader :attr_1, :attr_2—you’ll get some bad errors.
Feb 1
From, to get mongrel working with rails, just do this:
gem install mongrel --source
Feb 1
It seems that getting multi-threaded processes working nicely in rails is quite hard. You can’t just use a ruby thread—you’ll have all sorts of problems with classes being unloaded behind your back, and weird failures for models to be adjusted in the background. Here are some pages about it:
  • Threading in rails, an article from August 2007 which describes some problems the author was having, which sound very similar to mine.
  • A new version of the above article from February 2009, which talks about some of the problems he had with the first approach.
  • Spawn, which describes the module I ended up using.
  • BackgrounDRb, which has awful capitalization in its name, but is an alternative approach for running background jobs. This moves it outside the main process, and is more involved an approach than I wanted.
  • delayed_job, an alternative to BackgrounDRb.
Good luck getting threading working how you want it in rails. In my case, I’ve installed the spawn plugin using
script/plugin install git://
and then I have
spawn do
  # job running in the background
which seems to be working for now. Phew.
Jan 29
I use this constantly to disable beeps produced by X. They usually come through loud and painful through my headphones. Even matlab’s massive gui produces beeps that are fixed by this. Just type this in any terminal.
xset b off
Jan 27
Sewerage is the practice of conveying sewage in sewers. They’re easily confused words.
Jan 26
If a file is stored with ANSI color escape codes, you can page the file and preserve the colors using
ls -R filename
Jan 26
Problem when starting rails:
<blah blah blah>: no such file to load -- net/https (RuntimeError)
Solution: install libopenssl-ruby.
Jan 26
Go here, and after a brief (and captivating) message you’ll be straight in and enjoying the sight. Well done.
Jan 26
PiCloud has a clean and simple website, explaining their clean and simple way of adding python computing in the cloud. Here’s their example:
def func()
  # Do something.

import cloud
Jan 25
The chronic gem looks great, as a way of parsing natural language dates. Here are their examples, lifted straight from the above website. Would be interesting to build this into a website.
require 'chronic'   #=> Sun Aug 27 23:18:25 PDT 2006

  #=> Mon Aug 28 12:00:00 PDT 2006

Chronic.parse('monday', :context => :past)
  #=> Mon Aug 21 12:00:00 PDT 2006

Chronic.parse('this tuesday 5:00')
  #=> Tue Aug 29 17:00:00 PDT 2006

Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
  #=> Tue Aug 29 05:00:00 PDT 2006

Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
  #=> Sat May 27 12:00:00 PDT 2000

Chronic.parse('may 27th', :guess => false)
  #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
Jan 19
When writing software, it’s better to very carefully think out how your users should (want) to use your software, rather than providing many different configuration options for them to choose among various sub-optimal methods. I just opened the preferences dialog box of a large piece of software, and was irritated to see how much junk was in there. I think I’d rather not be able to configure what I want, than to have to move through 1000 options trying to find if the one I want exists.
Jan 17
This bizarre command, when typed in the address bar, allows you to edit the local version of a webpage in place (not sure on which browsers this works). (source.)
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0
Jan 16
Pull a non-ascii character onto the search bar (courtesy this good page):
This obviously expands to copying larger amounts of text as well. (And makes plenty of sense.)
Jan 14
Yep, it’s totally trivial, but I always forget how to create a new command in latex that has arguments. Here it is once and for all.
Jan 14
For example, to see functions defined in the matlab library libeng,
nm ./bin/maci/libeng.dylib
Jan 14
This is an interesting article about how I really should switch to mercurial…
Jan 13
Why, oh why do you do
git checkout sha1 filename
and things work fine, but then if you instead dared to try
git show sha1 filename
you wouldn’t get an error, your computer would just blink at you and then move on. You have to instead insert a colon:
git show sha1:filename
and the file magically appears.
But try and put the colon back in for checkout and what do you get? A nice, friendly fatal error. I will be trying mercurial as soon as I get a chance. I hope they have some scripts to migrate two-year-old git repos to mercurial.
> git checkout sha1:filename
fatal: reference is not a tree: sha1:filename
Jan 12
In matlab, deal can be used for multiple simultaneous assignment. For example, from matlab’s help,
% Same as a = x, b = y, c = z:
[a, b, c] = deal(x, y, z);
Jan 12
The geokit gem for ruby is extremely good. Within seconds of installing it I could use it to look up locations and distances. Now to apply this to analyzing tracks made by runkeeper, exported to gpx format, processed with hpricot, and bound together with much ruby goodness.
Jan 12
According to wikipedia, dry arabica coffee contains about “12mg of caffeine per gram of dry mass”. Some of this will be damaged by roasting, and it won’t all be extracted after brewing, but this means 15g of ground coffee probably contains around 150mg of caffeine. This is the same as around three cans of coke.
Jan 12
I’ve had some trouble getting the jemnotes server running in the background. I’m going to try using the daemons gem. It looks excellent.
Update: I immediately got things working exactly the way you’d expect. This is a great gem. I haven’t yet got the logging working, though.
Jan 11
Smashing magazine has an unusual name, but a good article with some usability findings. They’re not necessarily completely reliable, but they’re stated in a clear, usable (ahem) way: “the ideal search box is 27 characters wide”.
Jan 11
Just use this (source):
Jan 4
ZeroC comes recommended as a way to communicate between different applications / processes.
Jan 2
this is quite a fun online flash game.
Dec 22
I looked and listened to lots of the portraits here, of the leaders at the Copenhagen summit. They’re quite fascinating.
Dec 15
Here’s how to create a directory, if it doesn’t already exist. This works to an arbitrary depth—perfect for creating a whole hierachy straight away.
FileUtils.mkdir_p '/tmp/a/b/c/d/e/'
Apparently, it receives a list, too. Details here.
Dec 15
It seems Ruby rounds inconsistently—when told to display to 1 or 2 significant figures, it rounds 5s to 1 and 2, respectively—instead of being the same. Not sure why!
>> [1.14, 1.15, 1.16].map{|x| "%.1f" % x}
=> ["1.1", "1.1", "1.2"]
>> [1.4, 1.5, 1.6].map{|x| "%.0f" % x}
=> ["1", "2", "2"]
Edit: Nope, not a bug. This is because 1.15 gets translated to an internal, imprecise float which is slightly less than 1.15. I thought I understood most of the implications of floating-point imprecision—apparently not.
Dec 12
To find where a particular ruby gem lives, use this:
> gem1.9 which haml
(checking gem haml-2.2.15 for haml)
Dec 12
I spent ages searching for this. I like to have my Sent folder on mac, with Apple Mail, contain both local messages on my computer and remote messages on the server. You get a nice hierarchy where, most of the time, a single “Sent” box holds all sent messages, but click the arrow next to it and you have your local sent messages and your remote sent messages separately.
It sounds simple, but it took me ten minutes to find. Just say in the imap settings to store sent messages on the server, then rename your local sent folder to “Sent” and the two will magically merge. No amount of dragging and dropping will get you there, though.
Dec 10
Another reason I love ruby: say I have an array of items (a ‘list’, in python) and want to convert it to a hash (python: ‘dictionary’) giving the frequency of each item. I can do it like this:
def freq_table(a)
  d = {}
  d.default = 0
  a.each{|x| d[x] += 1}
(Yep, that’s all you need!)
Dec 10
Here’s a nice patch to the string class in Ruby to test if a string represents a valid floating-point number:
class String
  def valid_float?
    true if Float self rescue false
Dec 10
The amazon remembers feature of their iPhone app is quite hilarious, and very good. I took photos of lots of bits of office stationery, and it recognised them very well—including a small yellow post-it note pad, some gum and various other random bits.
It seems they’re using their mechanical turk service. It is a bit disappointing that it’s not automated, but a cool application of ‘old technology,’ too, I guess.
Addendum: I tried writing “Ideal $10 gift for co-worker” on a post-it note, photographed it, and promptly got this product suggestion. Nice job :)
Dec 10
If you want to replace a line in vim with the contents of the unnamed register, you can just V to select the line, then p to paste over it. No need to use the black hole register.
Dec 10
There are some amazing pictures and videos called Britain from Above. Even the thumbnails look great! For example, start with one of these:
Dec 10
Porsche’s new four door sedan looks and sounds amazing. A nice car.
Dec 9
I saw artist Candice Breitz’ video installation of 30 fans singing along to a Madonna album in the Boston Museum of Fine Arts Seeing Songs exhibition. I mentioned it to some friends, and we ended up trying it ourselves. We first, all together, sang a song and recorded the sound. Then, one at a time, we videoed each other singing the music while listening to the group track with headphones. That gave us 9 videos that we wanted to tile in a 3x3 grid, synchronize, then convert to one video.
I started off using iMovie, but the interface was laggy and inconsistent as my computer struggled with the load. You can also only play two videos simultaneously, using the picture-in-picture feature. You’d have to add one video at a time, and constantly export and re-import. This wasn’t going to work.
Then I discovered rmov, an excellent library for opening, editing and exporting QuickTime movies within Ruby. It worked spectacularly well, and I made the video as desired. I found the required start-time offsets to synchronize the streams by importing streams one by one and comparing them to a single person; then I could just compose all the streams together. I often just exported the audio, which made it much faster to render while deciding on the offsets.
Lastly, here’s the code I used.
require 'rubygems'
require 'rmov'

# locs refers to the filename of the original files.
locs = [2732, 2734, 2736, 2739, 2740, 2744, 2746, 2748, 2749]
offsets = [41.12, 4.25, 1.8, 0.0, 0.8, 2.5, 1.65, 4.7, 0.8]

clips = [] do |l,off|
  filename = "source/MVI_#{l}/MVI_#{l} - iPhone.m4v"
  clips <<
  clips.last.delete_section 0, off

background = 'source/background_480x360.png'

q = QuickTime::Movie.empty
length =
puts length
(length*15).times do
  q.insert_movie background, 0

WIDTH = 480
HEIGHT = 360

clips.each_with_index do |c,i|
  row = i / 3
  col = i % 3

  q.composite_movie c, 0

  a = q.tracks[-1]
  a.scale 0.34, 0.34
  a.translate col*WIDTH/3, row*HEIGHT/3

exporter = q.exporter
if File.exist? ''
  exporter.load_settings ''
  exporter.save_settings ''

outname = ""
puts "creating #{outname}."
exporter.export outname do |progress|
  percent = (progress*100).round
  puts "#{percent}% complete"
Dec 7
This page explains how to get symlink following working on the web server built into mac.
sudo vim /etc/apache2/users/username.conf
# add FollowSymLinks.
sudo apachectl graceful
Dec 6
git’s user interface isn’t very consistent. For example, I keep forgetting how to show an historic version of a file. Take a look at this:
git diff HEAD^ file  # shows differences between previous and current version of file.
This is good. But suppose you just want to see the old file. Look at this:
git show HEAD^ file  # WRONG: prints a blank line. No messages, no warnings.
git show HEAD^:file  # RIGHT: (note colon) shows the previous version of file.
OK. So maybe you should just always use that :? Well, nope—git diff will fail if you add in the colon.
Dec 6
Here’s how to open a server at the next available port, then retrieve the port number:
server = '', 0
port = server.addr[1]
Dec 3
Flow looks like a nice tool for transferring files between computers. I probably won’t use it because scp does the trick most of the time, but good to know about (and remember).
Dec 3
Unusual branding, but these folks have some nice tools. I’ve used heckle before, but flog and flay look good, too.
  • heckle modifies your code, making sure code modifications break at least one test.
  • flog analyzes your code, showing you how hard it is to test.
  • flay tries to find code that looks similar to other bits of code, reporting code that is a good candidate for refactoring.
Dec 3
You can force an app to start in 32-bit mode using (for example)
arch -i386 irb
Nov 28
I couldn’t get script/console to load the right version of Ruby. Turns out you can use
script/console --irb=irb1.9
Nov 4
Nov 3
Logitech’s mouse website has an unusual interface, but shows some signs of very careful thought, and some interesting elements. Sadly it has some badly-thought-out stuff, too—for example, there’s some ajax stuff that makes loading feature details very fast, but then the back page operation doesn’t go ‘back’ from the user’s point of view. This leaves you confused.
Oct 30
Oct 29
Fantastic icons that I saw a while ago, and had to re-find this evening.
Oct 29
Some more details about using rake.
And this page talks about some options when you want the output folder to be different to the input.
Oct 29
Leave off the end of a regex replace in vim, and it will just remove the matched expression. For example, these are identical:
Oct 22
When you install xcode, you get FileMerge, which you can open from the terminal with opendiff. It’s a great graphical way of comparing two files—for when diff isn’t quite enough.
Oct 20
Bash seems to contain more features than I could ever imagine. Check this out, for example:
# Both lines will produce the same output, but the first is a variable.
echo ~+
Oct 20
The special DATA variable in Ruby is kind of interesting. It lets you easily parse multiple input files as one.
Oct 20
Ruby 1.9 has some nice new methods on the Enumerable class; for example, each_cons, which iterates through consecutive slices of an array. You can add these to Ruby 1.8 with
require 'enumerator'
Oct 17
Some nice shortcuts from here.
cd -                  # go back to previous directory.
pushd /new/directory  # go to new dir; add current dir to stack.
popd                  # retrieve dir from stack; go there.
Oct 15
Surely rakefiles are the future. GNU Make just looks antiquated.
Update: not so sure. Rake seems like it’s not that great after all. I might try writing my own sometime.
Oct 15
Oct 6
Oct 6
A rather long but quite interesting discussion of domain specific languages.
Oct 5
The leading dimension (LDA) in lapack refers to the ‘distance in memory between elements of two consecutive columns which have the same row index’ (source).
Thus, in general, LDA is the same as N, but if you’re working with a submatrix A(91:100, 1:100), you’ll set N = 10 but LDA = 100.
Oct 4
Install a good fork of rcov, which works without the dependency on REXML.
gem install relevance-rcov --source
Sep 24
Excellent graphic design: SOFA.
Sep 23
Tioga is ‘plotting using Ruby, PDF, and TeX’. It looks pretty good.
Sep 23
Download a file, much like wget, but with curl:
curl -O
# equivalent to
This is especially appropriate on mac, because curl comes with mac, unlike wget.
Sep 15
Converting a pdf to a png and making it 1000x1000 pixels:
convert -density 300x300 -resize 1000x1000 infile.pdf outfile.png
Sep 11
Change the hostname reported by mac, permanently:
sudo scutil --set HostName
Sep 11
When setting up a remote repo, you now need to set up
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
Sep 11
  • Open the last file with :e\#.
  • U undoes all changes to the current line.
  • <Ctrl-D> <Ctrl-T> to change indent in insert mode.
  • :helpgrep, then :cn / :cN to move between matches.
  • '^ to last position where insert mode was stopped.
  • '' to the position before the latest jump.
  • '. to the position where the last change was made.
  • ": contains the last-executed command, so @: repeats that command.
  • set virtualedit lets you position the cursor where there is no actual character. Useful for editing tables etc.
Aug 18
After searching for something in vim, you can use +s//repl pattern/, and the search string will default to the previous search you just performed.
Aug 13
A nice explanation of camera sensors.
Aug 5
Nice way of seeing a cloud of words. For example, for this page I got {{<img src=“/jemnotes.png” alt=“wordle cloud” />}}
Aug 5
A nice introduction to ragel with ruby.
Aug 5
Taking things to extremes when calculating the product of 1 through to 9.
# Python version.
prod = 1
for x in range(1, 10):
    prod *= x
# Ruby version.
(1..9).inject :*
Aug 5
Mostly from the pickaxe book.
  • Compare array[start, length] and array[start..end].
  • x.freeze prevents modification to x.
  • books.first, but also books.first(3).
  • do |line, index|.
Aug 5
From the pickaxe book.
Create an enumerator object: e = (1..10).to_enum, or e = (1..10).each (without a block). Then, x = to_enum and enum_for are the same.
Or, create multiple, and use a loop to figure out the details (and throw a StopIteration when any of the iterators run out of elements).
x = ('a'..'z').to_enum
y = (1..10).to_enum
loop do
  puts "#{}: #{}"
Can also use with_index on an enumerator to convert it back for use with a code block. Or call a method for each element of the iterator, then convert it to an array: (1..10).to_enum(:each_slice, 3).to_a.
Aug 3
After gem installing wirble, add these lines to your ~/.irbrc to get syntax colouring etc. Courtesy of pablotron.
  # Load wirble
  require 'rubygems'
  require 'wirble'

  # Start wirble (with color)
rescue LoadError => err
  warn "Couldn't load Wirble: #{err}"
Jul 30
Short, simple and sweet. Found here, which reports that it’s attributable to Sam Stephenson.'image.png')[0x10..0x18].unpack('NN')
Jul 29
When typesetting mathematics with LaTeX, note that \mid is a vertical line with spacing as befits a binary relation, and \vert is a vertical line as an ordinary symbol. Compare: $p(x mid y)$ (with \mid); $p(x vert y)$ (with \vert).
Jul 28
A good summary of ruby operators.
Jul 28
An explanation of Ruby’s percent syntax. Example:
# Same as ['symmetric', 'matrix', 'vector', 'scalar']:
%w{symmetric matrix vector scalar}
Jul 24
Jul 22
Interactive Ruby terminal via ajax. Very well implemented.
Jul 21
Ruby has a conditional assignment operator: ||=. This will assign to a if a is nil (ie, not initialized).
Jun 19
Sick of messages like this when logging in?
Last login: Thu May 21 23:31:00 2009
touch .hushlogin
and they’ll be all gone.
Jun 11
I found it difficult to find help on the various ways of modifying the filename in a vim script. Turns out that :help filename-modifiers uses the correct keyword.
May 27
May 25
May 22
cProfile and friends work well for profiling larger programs where things are split into functions. For more detailed investigation, however, you need something like line_profiler, where you can see output like this (trimmed):
Line #      Hits         Time  Per Hit   + Time  Line Contents
202      1741        12946      7.4      0.0      for k in zip(K.I, K.J):
203      1740       222659    128.0      0.9          K[k] = randn()
Once installed, decorate any function worthy of analysis with @profile, run -l <> and then view results with python -m line_profiler <scriptname>.lprof. Works well.
May 21
I wrote down below about enumerate, which lets you access (index, item) pairs within a loop. I guess I thought it was a good idea, because I was about to write it here again. If you need both the index and item from a list, use enumerate:
for index, item in enumerate(items):
I haven’t used it in the intervening two months, but oh well.
May 18
A nice page explaining how to set up .gitignore rules for rails apps.
May 7
For some reason, appeared to be completely ignoring my request to disable the audible bell. This worked perfectly instead: xset b off.
May 7
Reminder: use ctrl+t and ctrl+d to increase/decrease the indent while in insert mode.
May 5
In smartindent mode, vim removes the indent if # is the first character on the line. This is annoying. I’ve tried various solutions that should work but don’t. Here’s one that should, and does:
inoremap # a#<Left><BS><Right>
(Courtesy of this vim tip, but beware; it contains some false information as well.) Use ^v (ctrl+v) to enter the special sequences easily.
May 1
This code will fail:
l = []
def app():
  l += (1,)
It will fail with an UnboundLocalError. According to this website, this is because ‘If a value is assigned to a variable in a function body, the variable will be local, even if there is a global variable with the same name, and this global variable has been used before the assignment.’
This is quite different to the behaviour if you don’t try and perform an assignment, and so is a bit confusing. Of course, there are few cases where global variables are the correct tool, but sometimes they can be handy.
Apr 14
Could py.test be used for test-driven development in Python?
Apr 14
This is a better summary of git than I’ve seen before. I intend to write my own guide, sometime—mostly for my own learning—and will probably use some of this material to remind me what to write about.
Mar 31
Ruby has an unless statement equivalent to if not. Nice. And the RailsTips blog has some good suggestions on usage.
Mar 31
Compare the popularity of two search terms over time at Google Trends.
Mar 31
Start using a remote server to backup / distribute a local app:
# On the server, to avoid having copies of local files.
git --bare init 

# On the client.
git remote add origin ssh://hostname/path/to/repo
git push origin master
Mar 30
A brilliant way of creating a ‘Site Specific Browser’ so that your dock contains a particular web app / site as a separate process. Makes a web app feel even more like it belongs as a fully-fledged app.
Mar 30
Undo the last git commit while leaving the tree as is. (Perhaps your commit message was wrong, or you forgot to add certain files.)
git reset --soft HEAD^
Mar 28
Set up a global ignore file for git using
git config --global core.excludesfile ~/.gitignore
Mar 28
The first javascript libraries I used were prototype and scriptaculous. However, I just started using jQuery, which is insanely great. The (css-like) selectors they use, for example, are so much fun: consider
This slowly hides and removes any visible list items that have no child elements that are images in the checkmark (tick for commonwealth readers) and checked (ticked) classes.
In other words, this slowly fades any checked off items that haven’t yet been hidden. So clean!
Mar 27
A few interesting things about Python that I had forgotten come from here. I’ve modified the examples.
First, you can use an instance’s namespace to give an interesting way to print strings. Sometimes this will be better:
# Basic version.
print 'My name is +s and I am +d.' + (, p.age)
# Advanced version.
print 'My name is +(name)s and I am +(age)d.' + p.__dict__
There is also the enumerate function, which returns (index, item) pairs. For example,
for (index, item) in enumerate(items):
    print index, item
Dictionaries have a setdefault method. Consider these alternative pieces of code:
# Naive piece of code.
for (cat, amount) in data:
    if cat not in d:
        d[cat] = 0
    d[cat] += amount

# Simpler piece of code.
for (cat, amount) in data:
    d.setdefault(cat, 0)
    d[cat] += amount

# A cunning third option: setdefault also gets.
for (cat, amount) in data:
    d[cat] = d.setdefault(cat, 0) + amount
But, better than all of these optons is the new defaultdict in Python 2.5.
from collections import defaultdict
d = defaultdict(generator_function)

# Example like those above.
d = defaultdict(lambda: 0)
Finally, generator expressions: say we want to sum a large list that we generate on the fly (ie, we don’t care about the list):
sum(i**2 for i in xrange(1e5))
(I didn’t find much difference in evaluation time, though.)
Mar 26
Slicehost clearly know what they’re doing, with a simple, clean website, and nice tools—including a web browser based console for emergency situations.
Mar 17
The brief version first:
# fine.
assert expr1, expr2

assert(expr1, expr2)
Be very careful with the syntax of assertions in Python! If you get the syntax wrong, the assertions will just always hold. This is particularly disastrous, as assertions are usually used to expose hidden bugs.
The exact syntax that you should use is assert expr1, expr2. This tests expr1 in the boolean sense, and raises an exception with value str(expr2) if expr1 is False. You can also just use assert expr1, which won’t give a special message. Note the total lack of brackets. If you insert parentheses, though, as in assert(thing1, thing2), what you are really doing is calling assert (thing1, thing2). That is, an assertion with expr1 being (thing1, thing2)! This will never evaluate to False, so you will never get an error.
Mar 16
Show the history of a particular file tracked by git, using
git log -p filename
Mar 9
To immediately view the old version of a file tracked by git, use, for example,
git show HEAD^:filename
Mar 3
To create a new group on mac with a specific gid, then add a user, type
dseditgroup -o create -i 499 groupname
dseditgroup -o edit -a username groupname
Feb 26
Feb 24
Nicely designed fruit website. Especially check out the charts which show fruit availability. Very clear.
Feb 11
This dictionary stores a limited number of entries, removing the oldest entries when it gets full. This could be used for a cache. Create a max-50-entry dictionary with f = FifoDict(50), for example.
class FifoDict(object):
    def __init__(self, maxentries=100):
        self.lastentries = []
        self.mydict = {}
        self.maxentries = maxentries

    def __getitem__(self, k):
        return self.mydict[k]

    def __setitem__(self, k, val):
        if len(self.lastentries) >= self.maxentries:
            del self.mydict[self.lastentries.pop()]
        self.mydict[k] = val
        self.lastentries.insert(0, k)

    def __str__(self):
        return str(self.mydict)

    def __repr__(self):
        return repr(self.mydict)
Feb 10
(mt) Media Temple have a very nicely designed website (though quite small font sizes).
Jan 31
The letter k toggles keep mode in xdvi; with keep on, the horizontal and vertical page offset remains the same, even when switching pages.
Jan 30
Let’s say we want to remove the prefix one from a series of files. Use this:
for FILE in one* ; do mv $FILE `echo $FILE | sed 's/one//'` ; done
Or, use +rename+. Thanks to the reader who pointed out that, in Debian GNU Linux (for example), you can also use rename, as in
rename 's/^one//' one*
Much cleaner, if rename is available.
Jan 9
Jan 8
Dec 6
In Safari (and possibly other browsers), you can log a small message to the debug console with the javascript command console.log(message). This is helpful for debugging.
Nov 18
This page pointed out a bizarre but perfect way of fixing readline on mac. Apparently the GNU license doesn’t play nice for Apple so it doesn’t work by default. I did this:
sudo easy_install ipython
sudo easy_install -f readline
And suddenly readline worked in ipython on mac.
Nov 18
Very odd, but also quite interesting industrial design. Reading this made me realise that sorting these entries by date is silly. I’ll fix this sometime.
Edit on 2009-12-07: I’m finally moving to version 2 of jemnotes, which addresses this.
Nov 6
I was shown an optimization solver that ran fine with certain parameter sizes, yet started segfaulting with larger problem sizes. It turned out that a desired single large, amorphous array could not be allocated within the available stack size limit. Increasing the stack size limit via ulimit -s 50000 (for example) fixed the problem.
You can view a shell’s resource limits using, for example,
> ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) 6144
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 266
virtual memory          (kbytes, -v) unlimited
Nov 6
Peregrine espresso in Washington, DC (map). The best espresso I’ve had in the US.
Oct 19
Useful page with git configuration details.
Oct 5
Sep 8
Sep 2
Expire a user’s password, requiring them to change it immediately upon their next login:
passwd -e username
Sep 2
Nice looking free fonts: exljbris and Gentium.
Sep 2
Leopard irritatingly prevents you from creating an ssh tunnel to another computer and then connecting a samba share to your localhost. Get around this by first creating an alias for your localhost interface:
sudo ifconfig lo0 alias up
Then, forward the ports via ssh, eg with
sudo ssh -N -L -L
Finally, connect using Finder to smb://
Sep 1
Nice way of having two different kinds of syntax highlighting within one file, with vim.
Aug 25
Great setup guide for openvpn. Goes well with Tunnelblick.
Aug 21
Check for common memory errors in C with valgrind. Also profile cache misses and branch mispredictions.
Sample usage:
# Check memory accesses.
valgrind --tool=memcheck ./prog

# View cache and branch profiling information.
valgrind --tool=cachegrind ./prog
For more detailed output from valgrind, add the -g tag to your gcc compilation flags to add debugging information to the binary.
Aug 16
Infinite labs: excellent little applications for mac, including PlugSuit and Afloat.
Aug 16
Aug 14
Disable highlighting of spurious _ and ^ symbols in LaTeX files as errors (say, when a macro is used to start equation environments).
autocmd Filetype tex syntax clear texOnlyMath
Aug 13
Use multiple processes (and hence cores) with make by using make -j.
Aug 13
A quick way of finding out where a Python package lives:
>>> import pylab
>>> print pylab
<module 'pylab' from '/usr/lib/python2.5/site-packages/pylab.pyc'>
Aug 13
Disconnect ssh quickly, even when ^d is not allowed, with ~..
Aug 11
Run a Python script through a profiler, then interpret the output. I don’t think it’s worth the bother going through iPython; I couldn’t get %run -p to work properly.
At the shell:
> python -m cProfile -o outprof.tmp examples/
Then, in iPython:
>>> import pstats
>>> p = pstats.Stats('outprof.tmp')
>>> p.strip_dirs().sort_stats('cumulative').print_stats(50)
Aug 11
A reminder of how tee works comes from
./prog | tee >(gzip > logfile.tgz)
Aug 11
From this blog, it seems that to convert from a pdf to an eps the following is required.
gs -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=epswrite -sOutputFile=new.eps old.pdf
There’s also an installer for ipe at the same blog.
Aug 11
find and sed operate slightly differently on a mac. Here’s some code that replaces 4.3 with 4.4 in all Makefiles, recursively.
find . -name 'Makefile' -exec sed -i -e 's/4\.3/4.4/g' {} \;
Aug 6
The numbered register "0 contains the text from the most recent yank command, while the numbered register "1 contains the text from the most recent delete or change command. If the delete or change was ‘small’, though, (ie, less than one line), the small delete register "- is used instead.
This means that if you yank some text to move it elsewhere, then rearrange some stuff (including deleting, say, newlines), you can still easily retrieve the text that you yanked with "0p.
Aug 5
I made some aliases for use when writing LaTeX documents. Add these (perhaps unwieldly) lines to your .bashrc:
# Next command on a single line.
alias lr='latex -file-line-error-style -interaction=nonstopmode $LR.tex > 
  /tmp/latexoutput || cat /tmp/latexoutput ; 
  grep -e "\(LaTeX Warning\)\|\(Overfull\)\|\(Underfull\)" /tmp/latexoutput | 
  grep -v "There were undefined references";
  killall -SIGUSR1 xdvi.bin 2> /dev/null'

alias xdj='xdvi -paper us -expert -keep $LR.dvi 2>/dev/null >/dev/null&'
Note: depending on your system, you made need to replace xdvi.bin with xdvi-xaw3d.bin.
Then, when you are preparing a document, you can do this:
>> LR=myfilename # set filename. The suffix .tex is added automatically.
>> lr            # compiles your latex document.
>> xdj           # displays your latex document.
This sounds pretty ordinary, but these aliases take advantage of these things:
  • lr will only show the output if there was a problem. This avoids polluting your screen.
  • lr will never stop with an error while running latex and leave you punching keys in the hopes of getting back to the command line.
  • latex will tell you the line number of any errors.
  • lr will automatically kick xdvi after compiling the document to show your changes. No need to click on xdvi or wait for it to notice the file changed. I really like the way xdvi permits such updates without flickering.
Aug 4
To see the assembly instructions generated for a piece of C code, use this:
gcc -c -g -Wa,-ahl src.c
Find out the macros, and values, predefined by gcc (includes useful things like __INT_MAX__).
gcc -dM -E - < /dev/null
Aug 2
Google’s chart api makes nice looking graphs, like this one.
Jul 31
Sampling from a discrete distribution, where p is a vector of probabilities:
min(find(rand() < cumsum(p)))
Jul 29
Woah, typing s/search/repl/ in a Skype conversation actually performs a replacement for both parties on your last message (only). You can also remove the last message with s///. It turns out you can also do the same by right clicking.
Jul 29
Have a look at this bizarre Matlab behaviour:
>> 1 / [1 2 3]' 
ans =
         0         0    0.3333
Apparently, this is how they wanted it. Excusing the use of \\backslash as if it were mathematics, we have the equivalence:
A / B = (B^T \backslash A^T)^T,
leading to the bizarre conclusion that in Matlab,
    / = \backslash^T.
Of course, the syntax that I really wanted was 1 ./ [1 2 3]'. This is fully consistent with Matlab’s elementwise operations, but would it be so bad if 1 / [1 2 3]' just did not work, especially rather than silently giving you something totally weird but the same shape?
Jul 29
I’ve often tried to use find, but have never really got over its syntax. It seems that it’s one of those powerful tools that’s not necessarily obvious to use at first.
Here’s one way to use it. This:
find . -name '*.sw*' -print
prints filename which have .sw in them (say, leftover vim swapfiles).
You can combine this with grepping. This:
find . -name "*.bib" -exec echo {}; grep "tribute to" '{}'
looks for the words ‘tribute to’ in files with the extension .bib.
Jul 22
Version 4.3 of gcc, has some significant improvements (more info), including this nice feature:
>> gcc -c -Q -Os --help=optimizers
The following options control optimizations:
  -falign-jumps                         [disabled]
  -falign-labels                        [disabled]
  -falign-loops                         [enabled]
# ...
  -fwhole-program                       [disabled]
  -fwrapv                               [disabled]
Jul 22
When pasting from the clipboard, in the terminal version of vim, use :put + (or :put *, depending on your system) to avoid indentation problems.
Jul 21
First, tic() and toc() functions. These work like Matlab: tic() starts the timer, and toc() stops it and gives formatted output. There’s also a tocq() function which just returns the number of elapsed seconds.
#include <time.h>
static clock_t tic_timestart;

void tic(void) {
   tic_timestart = clock();

float toc(void) {
   clock_t tic_timestop;
   tic_timestop = clock();
   printf("time: %8.2f.\n", (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC);
   return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC;

float tocq(void) {
   clock_t tic_timestop;
   tic_timestop = clock();
   return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC;
A bit of code for counting flops: first, a statistics structure, then some defines.
/* CFDIV    divides             1/a[3]
* CFADD    adds                c[12] + d[12]
* CFMUL    multiplies          b[4] * h[7]
* CFASN    assigns             A[5] = 0
* CFSTO    stores              A[5] = (...)
* CFTFR    transfers           A[5] = b[7]
* CFNTF    negated transfers   A[5] = -b[7]
* CFCOM    compares            c[5] < b[7]
typedef struct statistics_t *statistics;
typedef struct statistics_t {
   long div;
   long add;
   long mul;
   long asn;
   long sto;
   long tfr;
   long ntf;
   long com;
} Statistics;