jemnotesversion 2 / featuring latest 10 or see all/search

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/identity.pub
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 == '['
      end
    end
    stack.empty?
  end
end
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?
end
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
end

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}
    end
    true
  end

  # etc.
end
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 = MemCache.new '127.0.0.1'

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

# When using it:
data_cache 'tag' do
  thing_to_cache
end
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.