Bye Bye smbfs
Please, Von, please, next time you set up a samba mount, just use cifs and not smbfs. Save yourself a lot of headache.
Please, Von, please, next time you set up a samba mount, just use cifs and not smbfs. Save yourself a lot of headache.
Imagine you have a repository. At time t0 someone copies the code from the repository, starts another repository from there, and commences development. You also continue development. At time t you want to pull in the changes the other guy has made.
One approach could be you simply merge other guys code into yours with one big commit calculated from time t0. Now suppose you want to keep other guy’s history from t0 on, or even worse, you want to pull only some changes and you want to push some of your changes to other guy.
This is a true story. It all started with CVS. A webapp was developed, cloned and branched. Now there are 4 of them, mostly the same, but subtly different, and each with it’s own CVS repo. Each one at some point in time is identical to another, but the history behind those points in time are different, thanks to copy and commit cloning.
So after converting the CVS to Darcs, I came up with a way of tricking darcs into treating time t0 in each repository as the same t0, thus enabling cross-pollination, and here I describe that method.
First you find t0, and tag liberally. By liberally I mean twice. I’ll explain why later. Of course you put the same two tags on both repos, both at the same point in time. Then you do a darcs opt --checkpoint on each so you can do a partial get on them.
Now you have to decide which repo you’re going to keep, and which one you are bringing in. You probably want to keep the one with past history, and bring in the one that did copy and commit cloning. Do a darcs get --partial on each repo, keeping track of which is what. I like to use a for the destination, and b for the bad repo.
b think it’s a, you just replace b’s guts with those of a, but this only works for the partial at time t0. So we make a snapshot for reference, and then pull in b’s downstream stuff (because we can’t do this after b thinks it’s a).
Now go into b/_darcs and we’ll play around with these guts to make it think it’s a, but we want it to have it’s future patches first. So we take a snapshot before pulling the other stuff. I just made a temp directory and did cp -r inv* patches temp, these are the three things in the _darcs directory we care about. Here’s where the liberal tagging comes in, the partial at the later tag only depends on the first tag, so that makes your inventory and patches much simpler. Now you’re ready to pull in all the subsequent patches from the cloned branch.
Once that’s done, you go back into b/_darcs, this time you take anything in temp/patches and temp/inventories and remove the corresponding files from patches and inventories. Then you replace those files with the files from a/_darcs/{inventories,patches}. These two steps I accomplish like so:
ls temp/inventories | while read i; do rm inventories/$i; done
ls temp/patches | while read i; do rm patches/$i; done
for f in inventories patches; do cp ../../a/_darcs/$f/* $f; done
One last step remains. Now you edit inventory and replace that very first patch number, the one that’s a tag, with the one that’s in the inventory file from a. Now b effectively has the exact same past history as a, and has all it’s future patches, and now you can share between your repo and other guy’s repo freely.
One might think this is an uncommon situation, but I can’t help but think it’s more common than I think… Hopefully this guide will save others the headache of trying to wrap their minds around the darcs inner guts, as well as save me some headache should I ever encounter this again (which I’m pretty sure I will, in only a few weeks!).
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 yourconfig/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.
Been a while… So my blog was broken just now, not sure for how long. Not more than a month I think. The problem was in the acts_as_taggable plugin, which rumor has it is dead. For now I will still use it, and describe the fix in case it also is broken for you. The problem is in the join table. The geniuses decided to make the join table <tag class>_<taggable class>. This is backwards from the regular has_and_belongs_to_many convention in activerecord. Somewhere, somehow, this backwardsness was reversed. I had it using it’s default join table before (tags_posts) and it broke as now it’s looking for posts_tags. So I add an option to the call:
acts_as_taggable :join_table => :tags_posts
I suppose I could have changed the table name, but at least this way it will never (hopefully) break again. I’m not quite sure how this default behavior changed in the first place.
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.
IMMS was complaining to me that sox couldn’t read mp3 files to alalyze them. I wasn’t about to let this go on too long, I want IMMS to work to it’s full potential. I tried installing liblame0 and liblame-dev, I even tried libmad but all to no avail. Finally I did apt-cache policy sox. The installed version was some studio version from DeMuDi. I thought if any of the available packages would support mp3 it would be the one from demudi, alas this was not the case. I installed the version from ftp.easynet.fr (PLF) and now I have (readonly) support for mp3. Good enough for me.
I was trying to look up some unicode characters and google searches for "unicode this" and "unicode that" weren’t getting the kind of results I wanted. So instead I googled for "unicode lookup" and found this.
This site is just so perfect for a firefox quicksearch. So I added "http://www.fileformat.info/info/unicode/char/search.htm?preview=entity&q=%s" as a quicksearch bookmark, with unicode as the keyword. Now I’ll never have to fuddle around looking for unicode characters again.