Working on an AES implementation in ruby, I’ve had many occasion to do:
n.times {|i| some calculations based on i}
I’m sure many of you have used the Integer#times method much to your glee. I’m sure many of you have also had the occasion where you realize that you want those resultant calculations to end up in an array of some sort. At this point you have two options: create an array before hand and << those results in, or change your iterator to (0...n).map {|i| ...}. Both options are severely lacking in taste, IMO. So I came up with a third option. Enter Integer#things.
ary = n.things {|i| ...}
Notice how the syntax here remains consistent with the semantics. Before you wanted to do something n times. Here you want the same, but you want to store the results, you want n results, or n things.
Don’t get me wrong, I love Range and it certainly has it’s place. I would never argue that 26.things { make a letter } is a better approach than ('a'..'z') { just have your letter already }. Like I said, it’s about semantics, do you really want that range or is it more about the number of things? And as we all know deep down, the syntactics should match the semantics wherever possible.
So here is my implementation for Integer#things and I really hope something like this becomes included in future ruby.
class Integer
def things &b
b ||= proc {|i| i}
ary = []
times do |i|
ary << yield(i)
end
ary
end
end
As I was creating a new rails app, I used this previous post of mine to set up the in memory database. Well it’s time for an update. Apparently now the rails config/environments/*.rb are run in the context of Rails::Initializer. So instead of creating that context as in the previous post, you simply do:
alias initialize_database_old initialize_database
def initialize_database *args
initialize_database_old
load "#{RAILS_ROOT}/db/schema.rb"
end
Here’s another rails fix. This one is for SQLite3 >= 3.3.8. SQLite decided to return sql strings for the default values shown in table definitions. This broke rails, the rails people got mad and won’t introduce a workaround. Well here’s my workaround.
sqlite_fix.rb:
module ActiveRecord
module ConnectionAdapters
class SQLiteAdapter < AbstractAdapter
def columns table_name, name=nil
table_structure(table_name).map do |field|
/^'?(.*)'?$/.match field['dflt_value']
field['dflt_value'] = $1
SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'] == "0")
end
end
end
end
end
Include it at the end of
environment.rb. That’s it!
I wanted to get that nifty :memory: db thing working for the test database in Rails. There are a sparse few articles floating around telling you how to do this. All of them, IMHO, are kinda ugly, or at least not-so-elegant. Here’s my answer, which took a lot of digging around in the inner workings of rails to come by.
First, do what everyone else tells you, put this in your
config/database.yml:
test:
adapter: sqlite3
database: ":memory:"
Once you got that, you have to have some way of loading the schema into your memory db every time you run the test. I found that the best place to do this is in
config/environments/test.rb. If you have an even better place, please let me know.
At first I thought I could just
load "#{RAILS_ROOT}/db/schema.rb" and be done with it. Alas, at this point there is no established connection to the db. So we have to modify things in such a way that this cute little command will get run just after establishing the db connection, but before the db is accessed. I did it like so:
module Rails
class Initializer
alias initialize_database_old initialize_database
def initialize_database
initialize_database_old
load "#{RAILS_ROOT}/db/schema.rb"
end
end
end
Then don’t forget to
rake db:schema:dump before running
rake test.
There’s probably other things you could do, like use ActiveRecord::Base.establish_connection instead of Initializer.initialize_database.
I break the above code block into lib/memdb.rb and simply require that inside of config/environments/test.rb. I tried to just add a metaclass to the config variable you see in test.rb but that is apparently some kind of impostor, because it didn’t work. Anyway, armed with this knowledge someone could probably make a pretty sane plugin to cleanly bring you :memory: testing.
As I was exploring gtk in ruby I found myself frustrated with the inability to create nested widgets in equally nested code. Such as:
$main = Gtk::Window.new
$main << Gtk::HBox.new do |b|
b << Gtk::Button.new("click me")
b << Gtk::VBox.new do |v|
... etc etc
end
end
No problem, eigenclasses to the rescue! I just add this simple thing (to a lib or just the beginning of my file):
class << Gtk::Widget
def new *args, &block
x = super
yield x if block
x
end
end
That’s all, now you can use good ol’ ruby blocks to make
GUI programming less obscene.
I once again have a working IMMS system. (Finally!) I’m using MPD for the player, my own imms.rb to talk to IMMS, and a history/queue wrapper client to turn MPD’s playlist into—you guessed it—a history (before current song) and queue (after current song).
This has the downside that your playlist isn’t a comprehensive playlist. So if you use a client where you would select a song you wanted to hear from a playlist you might not find it. Not to mention that would also mess up the IMMS behaviour.
My solution is mpremote.rb (which also needs livesearch.rb) to quickly find songs from the library and enqueue (enter) or jump-to (END) any song or group of songs I feel like listening to.
Eventually I’ll make a qt or gtk version of mpremote to be more like xmmsfind_remote (if you still use xmms I highly recommend it). Hans and I are also currently conspiring to make an even better MPD that will queue and possibly talk to imms itself.
Argh, for probably the 3rd time now I found myself trying in vain for some time to hunt down that blasted mkmf.rb. For future reference, and for anyone else searching, here it is.
On debian and debian based distros (ubuntu, demudi, etc) to get mkmf.rb for building those neato goodies, just apt-get install ruby-dev.
I was looking for something else, but stumbled across this. (praise google!) It’s pdf slides for a pretty awesome looking presentation on rails and ajax. Just looking through the slides opened my eyes to some pretty neat stuff I had no clue about. It’s got some funny pictures, too. It would be nice to see the actual presentation that went with the slides.
My favorite lesson from it: I didn’t have a clue about rjs templates and returning javascript for ajax calls. I’m excited to put these to use.
I use darcs in my rails apps. The combination of the generator creating several files at a time and not including the logs in the repo makes adding the new files cumbersome after a generate.
No worries! Just add ^log(/|$) to the _darcs/prefs/boring file. Now you can darcs add -r * after generates and rest easy.
There it is! I got the rss up and running. http://von.fugal.net/blog/rss gives you a nice feed of the 10 most recent entries. You can also get a topic-wise feed by simply adding / to the end, or even a list of topics seperated by comma. e.g. von.fugal.net/blog/rss/linux,web
Next task, fix the design!