14km

May 25

For when you realise that the file you want to edit is owned by root.


Jan 12

Nov 28

Jan 17

Embedding IRB into your Ruby application

I needed to play around with Capybara/Selenium, but it’s a pain waiting for Firefox to start up every time you make a small change. Fortunately, I’m not the only one with the problem. Drop this somewhere:

require 'irb'

module IRB # :nodoc:
  def self.start_session(binding)
    unless @__initialized
      args = ARGV
      ARGV.replace(ARGV.dup)
      IRB.setup(nil)
      ARGV.replace(args)
      @__initialized = true
    end

    workspace = WorkSpace.new(binding)

    irb = Irb.new(workspace)

    @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
    @CONF[:MAIN_CONTEXT] = irb.context

    catch(:IRB_EXIT) do
      irb.eval_input
    end
  end
end 

And it’s use is quite simple. When you want to drop into an irb session, you simply do

IRB.start_session(binding)

Nov 21

As of Emacs 20.3, there is indeed a repeat command (C-x z) that repeats the last command. If you preface it with a prefix argument, the prefix arg is applied to the command.

You can also type C-x <ESC> <ESC> (repeat-complex-command) to reinvoke commands that used the minibuffer to get arguments. In repeat-complex-command you can type M-p and M-n (and also up-arrow and down-arrow, if your keyboard has these keys) to scan through all the different complex commands you’ve typed.

GNU Emacs FAQ

Jul 6
Rainbow Mode:


  a minor mode for Emacs. It displays strings representing colors with the color they represent as background.


by Julien Danjou

Rainbow Mode:

a minor mode for Emacs. It displays strings representing colors with the color they represent as background.

by Julien Danjou


May 27

Mocha doesn’t play nice with Rails 3 & Bundler

Using mocha with edge beta Rails 3, I added Mocha to the Gemfile

gem "mocha"

All was going swimmingly until I ran into a problem. I reduced it to:

The result of running this test was:

#test_mock_doesnt_fail should fail, but doesn’t. Changing the require lines at the top of the file to not load Rails and only load Mocha (+Rubygems+Test::Unit):

require "rubygems"
require "test/unit"
require "mocha"

caused the test to fail as expected. After an hour or two digging around, I stumbled across a post on the Mocha mailing-list that mentioned ordering problems with Mocha and Test::Unit. Sure enough, if I rearranged the modified require lines so that Test::Unit was loaded after Mocha:

require "rubygems"
require "mocha"
require "test/unit"

the test would break — I.e. wouldn’t fail.

Following the advice in the post — modified for bundler — I changed my Gemfile to read:

gem 'mocha', :require => nil

and added:

require "mocha"

to test/test_helper.rb to make sure Mocha was loaded. This isn’t strictly needed, but it’s good to be clear.

Final result, the test fails as expected, and Mocha is seems to be working properly.


May 16

Money isn’t always a good motivator, as Alfie Kohn has been saying for years

Via Stretta


Apr 22

‘Now will this be everything, or…’

‘Well, I might need to raise an exception.’

The compiler purses its lips. ‘An exception? Hmmm… let’s see…’

JRM at Abstract Heresies

Apr 13

Be mindful of array copying

Trying

irb> d = [1,2,3,4]
=> [1,2,3,4]
irb> d[0..-2].each_with_index {|k,i| d[i+1] = "x"; puts k }
1
2
3
=> [1,2,3]
irb> d
[1, "x", "x", "x"]

Reasonable, d[0..-2] would make a copy of d

irb> d.each_with_index {|k,i| d[i+1] = "x" if i &lt; 3; puts k  }
1
x
x
x
=>  [1,2,3]
irb> d
[1,  "x", "x", "x"]

Apr 7

Mar 31

Y Combinations

I found myself needing to use a Y-combinator last night, while playing with regular expressions. I wanted to recursively scan a string, finding matches inside other matches. Of course it could have been done by defining a function and calling it recursively, but that seemed unnecessary. I had a vague understanding of what a Y combinator was and what it could do, which allowed me to realise that it was what I needed in my regexp task. But of course I didn’t have one handy in my REPL of choice.

So that motivated me to spend the afternoon learning more about the Y combinator. Richard Gabriel’s Why of Y provided a good overview in Scheme, with me following along in a SBCL REPL — Which was a good experience about the differences between a Lisp-1 and a Lisp-2 in itself. An old post on Raganwald’s weblog had an implementation in Ruby, but it started a few steps in, so I spent some time re-deriving the contents of that post. I doubt that I’d be able to produce a Y combinator by myself yet, but it gave me a good idea how it works.

I ended up with (in Ruby):

def y &h
  lambda { |g|
    g[g]
  }[lambda { |f| lambda { |*args| h[f[f],*args] } }]
end

And in Common-lisp:

(defun y (f) 
  (let ((g (lambda (h)
    (lambda (n) 
      (funcall (funcall f (funcall h h)) n)))))
    (funcall g g)))

But I also just learned about flet, which would simplify the CL version a bit.

There is a simpler Ruby version that they start with on Raganwald’s site:

def y(&f)
    lambda { |*args| f[y(&f)][*args] }
end

And an equivalent Common-lisp version could be made (doesn’t seem very idiomatic, with the double funcall):

(defun y (f)
  (lambda (args)
    (funcall (funcall f (y f) args))))

I should post my working file that I used to nut it all out…


Feb 13

Date manipulation

rubyquicktips:

You can add or subtract days or month from a Date object:

  • +(n): add n number of days
  • -(n): subtract n number of days
  • >>(n): add n number of months
  • <<(n): subtract n number of months

Here are some examples:

$ irb
>> date = Date.today     # => #<Date: ...>
>> date.to_s
=> "2010-01-26"
>> tomorrow = date + 1   # => #<Date: ...>
>> tomorrow.to_s
=> "2010-01-27"
>> nextmonth = date >> 1 # => #<Date: ...>
>> nextmonth.to_s
=> "2010-02-26"

Read the documentation for a more precise description.


Feb 11
Quite a misleading graph. Where do the numbers come from? Are the values stacked? Apparently they are, according to a comment on the article:
windows div = 5.4B Business div = 3B
So the whole graph is pushed up from the surge in profit from the Windows division. The numbers &#8212; profit numbers &#8212; are still mind boggling, though.

Quite a misleading graph. Where do the numbers come from? Are the values stacked? Apparently they are, according to a comment on the article:

windows div = 5.4B 
Business div = 3B

So the whole graph is pushed up from the surge in profit from the Windows division. The numbers — profit numbers — are still mind boggling, though.


Jan 13

Page 1 of 2