Brian Ewing
Prying into Rails
Pry is an amazing, light-weight alternative to IRB packed with nifty features.
I wanted to be able to run a rails console, but with Pry instead of IRB. I tried initially hacking at the “rails console” command, but it’s not nicely feasible as it calls IRB.start with no room for customisation without overriding the whole Rails::Console#start method (see the source code, cerca line 47)
So, a rake task will have to do. Here’s what I came up with:
1 2 3 4 5 6 7 8 9 10 |
task :pry => :environment do begin Pry.start rescue NameError STDERR.puts "Pry isn't installed. Place this in the development group in your Gemfile" end end task :console => :pry task :c => :pry |
This works great! Running “rake pry” drops into a Pry shell with my app at the ready. Nice :)
Ruby Fixnum#power_of?
This is my quick solution:
Gotta love Ruby. I’ve seen others solve this iteratively/recursively dividing by i until less than or equal to i. Less than meaning not a power, and equal to meaning it is a power. This is an expensive operation, whereas this uses the magic of logarithms.
One problem is with the check at the end (whether the exponent of i is a whole number by checking if it’s exactly divisible by 1) failing. Floating point precision means that there will be a remainder of ~0.000000001, and it’ll give a false negative. This happens above 2^29, unfortunately. This could be ""fix’d"" by checking if the remainder is <= 0.000000001, but that’s a bit of a hack.
This is the best implementation I can come up with right now though. I’ve seen a great implementation for powers of two, like this:
1 2 3 |
def power_of_two?(i) i >= 0 and i & (i - 1) == 0 end |
Which, unfortunately, won’t work for bases other than 2. Feel free to improve my Gist above!
Relayer::start!
Relayer is a dead simple, high-performance, event-driven IRC library written in Ruby.
It uses IO#select to achieve asynchronous IO, and has been tested to handle many hundreds of concurrent IRC connections from one instance.
Wrote this a few weeks ago when I couldn’t find a nice multi-connection IRC library for Ruby, just getting round to releasing it now ;)
Sample usage:
1 2 3 4 5 6 7 |
echo = Relayer::IRCClient.new(:hostname => 'irc.esper.net', :nick => 'EchoBot', :channels => ['#echo']) echo.events.channel_msg do |irc, event| irc.message event[:channel], event[:message] end Relayer::start! echo |
Fork it on Github




