»
Free Ruby on Rails book
SitePoint’s Rails book, “Build Your Own Ruby On Rails Web Applications” by Patrick Lenz, which I acted as technical reviewer for, is now available completely free of charge for the next 60 days.
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
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
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)