Hi, I'm Chris Moyer's Blog.

Tweet :: Command-Line Twitter Broadcast with Readline Support

So, after giving Allan some feedback about the latest less project, Get More Honey, we chatted a bit and it came out that I'm a PHP guy, with a bit of a leaning towards CakePHP. But, I'll fully admit that I love the ruby language, have played with it quite a bit, but simply never had an opportunity to write a rails app. People pay me to write PHP code. ;)

But, it did inspire me to fire up vim and write something in ruby. (Some day I'll dust off RubyKnight, the chess engine I wrote in ruby after reading Behind Deep Blue) Here it is:

#!/usr/bin/ruby

# Written by Chris Moyer (chris@inarow.net // http://inarow.net/)

# Do with it whatever you want.  That's public domain, right?

require ‘readline'
require ‘rubygems'
require ‘twitter'

user = ARGV0
pass = ARGV1

if !user or !pass then
puts ‘usage: tweet username password'
exit
end

$0 = “tweet #{user} xxxxxxxx”
twitter = Twitter::Base.new(user, pass)

while line = Readline::readline('(tweet)> ') do
Readline::HISTORY.push(line)
if line.length > 140 then
puts “#{line.length – 140} characters too long, try again.”
else
begin
twitter.post(line)
puts “Tweeted!”
rescue
puts “Failed to tweet! (#{$!})”
end
end
end

puts “\nBye!”

It solves my problem of reading twitter via gtalk, and not wanting to open twitter.com to post… while hating when I underestimate the length of my message and being trunctaed. I can just let this run in window of my screen session, and tweet away.

So, I'd love to hear from the rubyists out there:

  • What aspects of this are not good, idiomatic ruby practice?


Back to Blog Home »