Wednesday, September 30, 2009
The Billion Dollar Gram | Information Is Beautiful
The Billion Dollar Gram | Information Is Beautiful:
Billions spent on this. Billions spent on that. It̢۪s all relative, right?
Wednesday, August 12, 2009
Muni platform, Embarcadero Station
Muni platform, Embarcadero Station
Sunday, March 1, 2009
MySQL-Style Output for the Rails Console
While poking around the database in my rails console, I often found myself jumping to the mysql console just so I could get an easier-to-digest view of the data. I finally had enough of this silliness and wrote up a quick function to dump of set of ActiveRecord objects in the mysql report format.
Grab it from GitHub and stick it in your
>> report(records, :id, :amount, :created_at)
+------+-----------+--------------------------------+
| id | amount | created_at |
+------+-----------+--------------------------------+
| 8301 | $12.40 | Sat Feb 28 09:20:47 -0800 2009 |
| 6060 | $39.62 | Sun Feb 15 14:45:38 -0800 2009 |
| 6061 | $167.52 | Sun Feb 15 14:45:38 -0800 2009 |
| 6067 | $12.00 | Sun Feb 15 14:45:40 -0800 2009 |
| 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
+------+-----------+--------------------------------+
5 rows in set
Grab it from GitHub and stick it in your
.irbrc.
Thursday, August 21, 2008
Checking for STDIN in ruby
A colleague was asking today how he could see if there's data waiting on
That doesn't work in Linux, though. After some digging, I found this post, which lead to:
Which works on both platforms, but is (a) ugly (catching an exception), and (b) requires you to actually try to read from
In playing around with that, though, I noticed that
I'm not sure how reliable that is on other platforms (it won't work on Windows—fcntl is a Unix-y thing). If I'm understanding fcntl.h correctly, a return value of 2/32770 (0x8002) seems to indicate that the io stream in question is open for writing. I'm not sure if that is intended or just a side-effect. Anyone know?
STDIN in ruby (on Linux). On OS X, this is pretty straightforward:
if $stdin.stat.size > 0
puts "got something: #{STDIN.read}"
else
puts "nada"
end
That doesn't work in Linux, though. After some digging, I found this post, which lead to:
require 'fcntl'
flags = STDIN.fcntl(Fcntl::F_GETFL, 0)
flags |= Fcntl::O_NONBLOCK
STDIN.fcntl(Fcntl::F_SETFL, flags)
begin
puts "got something: #{STDIN.read}"
rescue Errno::EAGAIN
puts "nada"
end
Which works on both platforms, but is (a) ugly (catching an exception), and (b) requires you to actually try to read from
STDIN.In playing around with that, though, I noticed that
STDIN.fcntl(Fcntl::F_GETFL, 0) returned 0 if there was something on STDIN, and something non-zero (2 in OSX and 32770 in Linux) if STDIN was empty. So now the code is simple again:
require 'fcntl'
if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0
puts "got something: #{STDIN.read}"
else
puts "nada"
end
I'm not sure how reliable that is on other platforms (it won't work on Windows—fcntl is a Unix-y thing). If I'm understanding fcntl.h correctly, a return value of 2/32770 (0x8002) seems to indicate that the io stream in question is open for writing. I'm not sure if that is intended or just a side-effect. Anyone know?
Friday, March 21, 2008
Leopard External Monitor Annoyance Solved...aka Hide All Apps on a Mac
One of the more annoying things about Leopard is that when you plug in an external monitor that is set up to be your primary, your open applications don't move over to the monitor. I was having to manually drag everything over. Then I realized that if I hid all my apps before plugging in the monitor, when I brought them up again, they'd be in the right place.
So now the question was: is there an easy way to hide all your apps without cycling through them all? It took a bit of digging, but it turns out there is. Cmd-option-click on an app in the Dock and it will hide all other apps. So I tried Cmd-option-click on the desktop itself and...voilà!
Update: Technically, you're Cmd-option-clicking on the Finder, so if you have any Finder windows open, they won't hide. Bummer.
So now the question was: is there an easy way to hide all your apps without cycling through them all? It took a bit of digging, but it turns out there is. Cmd-option-click on an app in the Dock and it will hide all other apps. So I tried Cmd-option-click on the desktop itself and...voilà!
Update: Technically, you're Cmd-option-clicking on the Finder, so if you have any Finder windows open, they won't hide. Bummer.
Wednesday, February 27, 2008
ConditionsConstructor
I hadn't posted this before because it seemed kind of trivial and silly, but I used it again the other day and decided maybe I should share the joy after all.
Far too often I find myself jumping through hoops building ActiveRecord
Far too often I find myself jumping through hoops building ActiveRecord
find conditions arrays dynamically depending on what arguments were passed in to the method. So I cooked up a simple little class to clean that up a bit:
# class to construct conditions for AR queries
# cc = ConditionsConstructor.new('foobar > ?', 7)
# cc.add('blah is not null')
# cc.add('baz in (?)', [1,2,3])
# cc.conditions
# # => ["foobar > ? and blah is not null and baz in (?)", 7, [1, 2, 3]]
class ConditionsConstructor
def initialize(str = nil, *args)
@conditions_strs = str ? [str] : []
@conditions_args = args
end
def add(str, *args)
@conditions_strs << str
@conditions_args += args
end
def conditions
[@conditions_strs.join(' and ')] + @conditions_args
end
end
Subscribe to:
Posts (Atom)