RSS

tumble.lukeredpath

Oct 31
Permalink
Oct 24
Permalink
Oct 16
Permalink
Oct 02
Permalink
Sep 23
Permalink

Timeout on any Ruby method


require 'timeout'

class TimeoutProxy
  include Timeout
  
  def initialize(receiver, timeout_in_seconds)
    @receiver = receiver
    @timeout_in_seconds = timeout_in_seconds
  end
  
  def method_missing(method, *args, &block)
    timeout(@timeout_in_seconds) do
      @receiver.send(method, *args, &block)
    end
  end
end

class Object
  def with_timeout(timeout_in_seconds)
    TimeoutProxy.new(self, timeout_in_seconds)
  end
end

# EXAMPLE:
#  class Foo
#    def self.some_long_running_method
#      sleep 10; puts 'done';
#    end
#  end
#
#  # without timeout
#  Foo.some_long_running_method
#  #=> 'done'
#
#  # with timeout
#  Foo.with_timeout(3).long_running_method
#  #=> raises Timeout::Error
Sep 20
Permalink
Sep 19
Permalink

Using pastie from the command line

I was having some problems with the Ruby and Curl scripts on Pastie so I hacked together a quick script of my own. It takes the text you want to paste from STDIN and sends it to pastie (run pastie -h for options) and if successful it prints the resulting pastie URL.

Its easy to chain common unix commands together with the pastie script - try this to send the contents of a file to pastie then open the resulting paste in your default browser (OSX/*nix):


$ cat ~/somefile.html | pastie -f html | xargs open
Sep 13
Permalink
If Eistein were alive today, he’d probably be hacking the Linux Kernel along side Alan Cox during his lunch break.
Sep 12
Permalink

More sneaky #to_proc tricks

class Class
  def to_proc
    proc { |obj| self.new(obj) }
  end
end

class UserPresenter
  def initialize(user)
    @user = user
  end
end

@user_presenters = User.find(:all).map(&UserPresenter)
Aug 29
Permalink

#caboose (16:29, 29 Aug)

  • topfunky: there's also a channel for Rails 3.0 discussion. It's at #merb