# It Couldn't Be Easier
- sudo gem install dancroak-twitter-search -s http://gems.github.com
- require "rubygems"
- require "twitter_search"
- TwitterSearch::Client.new( "twittb0t" ).query( :q => "carldr" ).each do |t|
- puts t.text
- end
- sudo gem install dancroak-twitter-search -s http://gems.github.com
- require "rubygems"
- require "twitter_search"
- TwitterSearch::Client.new( "twittb0t" ).query( :q => "carldr" ).each do |t|
- puts t.text
- end
- class PermaStore < Hash
- def self.load
- if File.exists?( "data.dump" )
- File.open( "data.dump", "r" ) do |f|
- return Marshal.load( f )
- end
- end
- return PermaStore.new
- end
- def []=( key, value )
- super
- save
- end
- def delete( value )
- super
- save
- end
- protected
- def save
- File.open( "data.dump", "w" ) do |f|
- Marshal.dump( self, f )
- end
- end
- end
Do whatever you will with it. It’s not thread safe, but that’s easily fixable if you’re so inclined.
Nope, not the usual boring text encoding stuff, but this :
- #!ruby -Ku
- module Kernel
- def ∑( *args ) # Sum of
- args.inject( 0 ) { |i, j| i += j }
- end
- def √( number ) # Root of
- Math.sqrt( number )
- end
- end
- puts √(49)
- puts ∑(1,2,3,4,5)
Seriously cool. Idea stolen from here.
Gosu is a 2D game development library for the Ruby and C++ programming languages, available for Mac OS X, Windows and Linux.
- require 'rubygems'
- require 'gosu'
- class MyWindow < Gosu::Window
- def initialize
- super(640, 480, false, 20)
- self.caption = 'Hello World!'
- end
- end
- w = MyWindow.new
- w.show
I can’t wait to find the patience to do something cool with this.
If you’ve ever been frustrated by the lack of control that most mail clients give you over your signatures, then Siggy Stardust is for you. It’s basically an SMTP proxy, which simply adds signatures to the end of any mails you send according to rules that you set up. The rules are written in pure Ruby, and so you can do pretty much anything you want within them.
For example, here is my current rules file at Teh Office. If all the recipients of a mail are within 29degrees, then a personal signature is appended. If any of the recipients are external to the company, then the business signature gets added. My personal signature tries to include the song I’m listening to at the moment, either on my own computer, or on the company jukebox. It does this by querying either iTunes or last.fm.
- require 'rubygems'
- require 'rbosa'
- require 'scrobbler'
- rules do
- smtp_server( "localhost" )
- smtp_port( 10025 )
- track = nil
- track_string = nil
- begin
- # Try and get currently playing information from either iTunes or last.fm.
- itunes = OSA.app( "iTunes" )
- if itunes.player_state == OSA::ITunes::EPLS::PLAYING
- track = itunes.current_track
- else
- user = Scrobbler::User.new( "29degrees" )
- track = user.recent_tracks.first
- end
- rescue
- puts [ $!.class, $!.to_s, $!.backtrace ]
- end
- if track
- track_string = "\"%s\" by %s off \"%s\"" % [ track.name, track.artist, track.album ]
- personal_sig = <<-EOT
- --
- Now listening to : #{track_string}
- Siggy Stardust r78
- EOT
- else
- personal_sig = <<-EOT
- --
- ASCII a silly question, get a silly ANSI.
- Siggy Stardust r78
- EOT
- end
- business_sig = <<-EOT
- --
- Regards,
- Carl Drinkwater,
- Director.
- http://29degrees.co.uk - Bespoke Web Application Development
- +44 (0) 161 953 6669 - +44 (0) 776 043 1463
- 29degrees Ltd is registered in England and Wales under company
- number 05952987. VAT Reg Number GB897293655. Registered office
- St. George's House, 215/219 Chester Road, Manchester, M15 4JE.
- EOT
- if recipients.all_in_domain?( "29degrees.co.uk" )
- append_sig( personal_sig )
- else
- append_sig( business_sig )
- end
- end
(Does anyone else think heredocs are ugly?)
Siggy tries to be intelligent when adding signatures to multipart emails. For me, the best results have come by adding the signature to the end of the last text/plain part of the email, and so that’s what Siggy does. It doesn’t attempt to do anything with text/html emails, because it shouldn’t need to.
If you want to play, download Siggy Stardust here. Run it by doing something like :
- ruby ./server.rb rules.rb
You’ll need to re-configure your mail client’s SMTP setting to use localhost and port 1234. By default, Siggy will proxy through to port 25 on the local machine, but you can change that by using the smtp_server and smtp_port methods as in my rules file.
The only other real helper functions are demonstrated in the final lines of my rules file :
- if recipients.all_in_domain?( "29degrees.co.uk" )
- append_sig( personal_sig )
- else
- append_sig( business_sig )
- end
You will probably need to write more to do the things you want, you’ll need to do that in server.rb.
To use the ‘now playing’ functionality, you’ll need the appropriate Rubygems installed. The iTunes lookup uses RubyOSA, and is only available on OS X.
Lastly, the rules file is reloaded each time you send an email through Siggy, so you don’t need to restart the server if you edit your rules.
The two files comprising Siggy Stardust, rules.rb and server.rb, are placed in the public domain. However, note that the gzipped-tar archive you can download above comes packaged with TMail, and is licensed under the LGPL.
A bit of Friday fun. Golfr saves you all the hassle of having to type out full method names by letting you use as few characters as you need. Look at the examples to see how it can save you valuable keystrokes.
- module Golfr
- def method_missing( method, *args)
- endings = Regexp.new( /[!?=]?$/ )
- ending = method.to_s =~ endings ? $& : ""
- ( look_for = method.to_s )[ endings ] = ""
- if found_method = ( methods.sort.select{ |m| m =~ /^#{look_for}/ }.first ||
- methods.sort.select{ |m| m =~ /#{look_for}$/ }.last )
- found_method[ endings ] = ""
- STDERR.puts "Calling #{found_method}#{ending} for #{method}" if $DEBUG
- send ( found_method + ending ).to_sym, *args
- else
- raise NoMethodError, "#{method}"
- end
- end
- end
- class Object
- include Golfr
- end
Then have fun!
- require 'golfr'
- a = []
- $><"Hello\n" # Matches '<<'
- a.uns "Cheese" # Matches 'unshift'
- a.ft [ "Pickle" ] # Matches 'unshift'
- puts a.insp # Matches 'inspect'
- a.fl! # Matches 'flatten!'
- puts a.pect # Matches 'inspect'
- puts a.n? # Matches 'nil?'
- puts "carl".c # Matches 'capitalize'
- Thread.a = true # Matches 'abort_on_exception='
Set $DEBUG=1 if you want to see what methods are actually being called, and feel free to do what you want with the code. It’s all yours.
This week, I was moving my iTunes library off an external HDD to a network share and somehow managed to completely badger it up. All the songs were still showing in iTunes and they were all on disk in their original places, but something had happened to their location in the iTunes database and were showing with an exclamation, indicating iTunes couldn’t find them.
After fixing these by trying to play the songs and manually locating them on disk, I was still finding songs which weren’t indicated by iTunes as missing, but actually were. So, I quickly wrote the below script to list all the broken tracks in my library. Unfortunately, there doesn’t seem a way to set the location automatically.
So yeah, I don’t know if this will be useful to anyone else, but here is the code …
- require "rubygems"
- require "rbosa"
- library_playlist = OSA.app( "iTunes" ).sources.find{ |s| s.name == "Library" }.library_playlists.first
- # Songs which are broken have a location of 'nil', but still have the
- # rest of the information.
- library_playlist.file_tracks.reject{ |t| t.location }.each do |track|
- puts '"%s" by %s, off "%s"' % [ track.name, track.artist, track.album ]
- end
- a = [ 1, 2, 3, 5, 6, 2, 3, 1, 4, 5, 4 ]
- puts a.inject( 0 ) { |c, i| c ^= i }
Find the unpaired number in an array. I can’t really think of a situation you’d need to do this, but it’s a really, really elegant solution and that’s why I’m posting it. It’s left to the reader to work out how it works. From.
- 40.times{puts((0..7).map{rand(?{).chr=~/[^\W_]/?$&:redo}*"")}
All in 61 bytes. Can you make it any shorter?