Rails 3 console and “no such file to load”

Posted on October 10, 2010, under Uncategorized.

If you happen to load gems in your ~/.irbrc , make sure to add a gem dependency on them in your Rails apps’ Gemfile.

For example, I just started using Awesome Print in IRB. However, when I ran rails console , this error occurred:

no such file to load -- ap

After searching Google, I learned that Awesome Print needs to be listed in my Gemfile . Easy enough:

## ~/.irbrc
require 'rubygems'
require 'ap'

## ~/src/rails_3_app/Gemfile
group :development do
  gem 'awesome_print'
end

PostgreSQL grants on all tables

Posted on September 8, 2010, under GNU/Linux.

As much as I love PostgreSQL, its system of managing user privileges is a pain in the ass. PostgreSQL has no equivalent to MySQL’s method of granting a privilege on all of the tables in a database, including tables that you create in the future.

In MySQL, it’s as easy as:

GRANT SELECT ON db_name.* TO user@hostname;

The best that I’ve been able to achieve with PostgreSQL is this abomination:

$ cat generate_grants.txt
SELECT 'grant select on '||schemaname||'.'||tablename||' to user_name;'
FROM pg_tables
WHERE schemaname IN ('public')
ORDER BY schemaname, tablename;
$
$ cat generate_grants.txt | psql -U nickh -W -d database \|
grep '^ grant' >grants.txt
$
$ head -2 grants.txt
 grant select on public.table1 to user_name;
 grant select on public.table2 to user_name;
$
$ cat grants.txt | psql -U nickh -W -d database

*shudder*

Thanks to Ben Williams’ blog post for the initial query to generate the GRANT statements.

Deploy a specific revision with Capistrano

Posted on August 6, 2010, under Coding.

Just about every website that I release, be it at work or at home, is stored in a Git repository, and deployed using Capistrano.

I’ve been debugging this PHP application at work that was written by a 3rd-party. After cleaning up the XSS and SQL injection vulnerabilities in it, we found a new bug. It’s minor, yet important.

While tracking down the offending line, I needed to deploy various revisions of the website. It turns out that it’s really easy to do this with Capistrano:

$ cap -S revision=3f30b6de3c55a8347e5f3de3b43193591e6c7322 beta deploy

One thing I noticed was the need to provide the full SHA-1 object name (AKA revision ID, commit ID). Normally with Git, you can provide the first few characters of a commit ID. For example:

$ git checkout 3f30b6de

instead of

$ git checkout 3f30b6de3c55a8347e5f3de3b43193591e6c7322

I guess Capistrano’s a bit more picky!

Answers are for losers

Posted on April 12, 2010, under Other.

What do you want: questions, or answers? Most people want answers. There’s a problem you need to solve, and the answer’ll let you continue working. That’s fine. That’s valid.

But is it interesting?

I was talking with @ghuber about a new idea I had. Without going into details, it’d pigeon-hole me into That Answer Guy.

How do you solve X? Here you go! The answer’s ABC.

BOR-fucking-ING. If you’re That Answer Guy, you’re giving away valuable resources, but not actively facilitating much further discussion or innovation. Your community’ll appreciate the answer. Kudos to you. Thanks, Teach!The Answer Man movie poster

But it doesn’t make you interesting.

Instead, you want to ask questions. Lots of questions. Oddball questions. Edge-case questions. What happens if I turn this to 11? What if I mix lolcats with quantum theory?

Think of it this way: Who do you want presenting at a conference? Who do you want to shoot the shit with over a beer? Who thinks of wacky stuff? The guy who knows how to do X when you have Y, or the guy who asks interesting questions and ponders new ways of doing things?

The latter, damn it!

That Answer Guy is useful and appreciated. He definitely is. But the role’s limited. It confines you. It defines what questions you ask, and what problems you solve.

Ask more questions. Write more software. Pursue tangents. And talk about it.

If Plasma dies, here’s what to do

Posted on January 11, 2010, under GNU/Linux.

Every now and then, the Plasma desktop in KDE 4 crashes on me. Usually, it restarts itself. Sometimes, it doesn’t.

If you’re cursed with the latter scenario, there’s an easy solution.

Note: As avocadohead kindly mentioned in the comments, KDE >= 4.3 has renamed “plasma” to “plasma-desktop”. So if you’re using KDE 4.3 or later, replace “plasma” with “plasma-desktop” in the commands below.

By default, KDE sets the keyboard shortcut for “Run Command” to ALT+F2. So hit ALT+F2, and type this, and hit enter:

kbuildsycoca4 && kquitapp plasma && kstart plasma

Plasma should pop back to life.

That command is actually three commands:

  1. kbuildsycoca4 rebuilds KDE’s system configuration cache.
  2. kquitapp plasma ensures that Plasma’s no longer running, instead of just, say, hung.
  3. kstart plasma starts Plasma.

It took me a while to figure this out. Thanks to this post, I learned about kstart. From what I’ve read, it seems that running kquitapp plasma and kstart plasma from the CLI works for some people. Unfortunately, they fail for me on the CLI:

[nickh@chameleon ~] kquitapp plasma
<unknown program name>(7282)/: "Application plasma could not be found using service org.kde.plasma and path /MainApplication."
[nickh@chameleon ~]
[nickh@chameleon ~] kstart plasma
Qt: Session management error: Could not open network socket
kstart(7394) main: Omitting both --window and --windowclass arguments is not recommended
[nickh@chameleon ~] <unknown program name>(7397)/ checkComposite: Plasma has an argb visual 0x9ca7300 71303169
<unknown program name>(7397)/ checkComposite: Plasma can use COMPOSITE for effects on 0x9ca6c18
plasma(7398): KUniqueApplication: Cannot find the D-Bus session server

plasma(7397): KUniqueApplication: Pipe closed unexpectedly.

[nickh@chameleon ~]

Matching Printable Characters

Posted on November 30, 2009, under Coding, GNU/Linux, Uncategorized.

Who doesn’t love regular expressions? They’re fucking awesome. I use’em at least 10 times per day.

Sometimes, there’re restrictions on what patterns you can use. I wanted to change a regex for validating passwords. Originally, it allowed letters, numbers, and a seemingly random collection of special characters. How lame is this?:

^[A-Za-z0-9]{1}[A-Za-z0-9_\\.\\!\@\#\-]{0,255}$

Not only are the allowed special characters arbitrary, but they’re escaping characters in a character class, and using “{1}”.

I tried changing the regex to this:

^[[:print:]]{0,255}$

Unfortunately, that wasn’t considered “valid” by the system in question. Luckily, there’s a fairly concise alternative:

^[\x20-\x7E]{0,255}$

If you find a system that lacks support for POSIX character classes, check out this Wikipedia article.

Displaying Your ViM Colour Scheme

Posted on November 16, 2009, under GNU/Linux.

I posted this mostly because I’m always forgetting it, and also because it’s a pain in the ass to search for on Google. I usually try “:echo g:colorscheme”, which fails miserably.

If you’re playing around with colour schemes in ViM and want to figure out which one is being used at the moment, simply type:

:echo g:colors_name

When Gnome and Firefox Are Dead Slow

Posted on November 14, 2009, under GNU/Linux.

I’ve had Ubuntu Karmic Koala installed on my MacBook Pro Core2Duo for 2-3 weeks now. Almost everything’s been working perfectly. However, whenever I’d start a second Gnome session and run Firefox, the machine would slow to a crawl, and X’s CPU usage would skyrocket..we’re talking >80% here.

I scoured Google for all sorts of things:

  • Gnome second session is slow
  • Gnome X is slow
  • Gnome Xorg is slow
  • Gnome Firefox slow
  • X second session is slow
  • Etc.

I found several other reports of this, but no solutions.

Eventually, I stumbled upon this blog post, which purports to have solved the problem. I gave it a shot, and holy shit, it worked!

Just to be verbose, here’s what I did:

  1. Open /etc/X11/xorg.conf .
  2. Add this line to the “Device” section:
    Option "AccelMethod" "XAA"

    So my “Device” section now looks like this:

    Section "Device"
      Identifier  "Configured Video Device"
      Option      "AccelMethod" "XAA"
    EndSection
  3. Save the file and log-out of Gnome.
  4. Switch to tty1 by hitting CTRL+ALT+F1 .
  5. Restart GDM:
    $ sudo /etc/init.d/gdm restart

Copying Between GNU Screen buffers

Posted on November 10, 2009, under GNU/Linux.

GNU Screen is one of those tools that makes you think “How the %&@# did I live without this for so long?!” If you work on command lines regularly, GNU Screen is a must.

In my desktop environment, I run Konsole and Yakuake, with a different Screen session in each[1]. A few days ago, I noticed that I was using the mouse to copy text between these Screen sessions. How inefficient is that?

It turns out that there’s a really easy way to copy text between different Screen sessions.

Note: I use CTRL-J as my escape command.

Copy some text to the first Screen’s buffer, and type ^J:writebuf . Now switch to your other Screen session, and type ^J:readbuf . Your second Screen session has just grabbed what you copied in your first Screen session. Paste it wherever you want, and Bob’s your uncle.

[1] These Screen sessions are different on purpose. My Yakuake session is for “throw-away” tasks that don’t necessarily pertain to any tasks that I’m currently working on.

Linux Mint Is Weird

Posted on October 15, 2009, under GNU/Linux.

I have a MacBook Pro. I bought it back in 2007 when I was living in Australia. It’s awesome. I love it, especially the keyboard. Unfortunately, Mac OS X has pissed me off way too many times. So I jumped ship to Linux Mint 7.

Out of the box, Mint 7 supports my Mac’s Atheros wifi card and ATI video card. That’s something that not even Ubuntu managed to do. Needless to say, I was impressed.

After installing and configuring most of Mint 7, things were looking pretty good. As some of you know, I customize KDE heavily. Shortcuts, panels, virtual desktops, colour schemes, etc. Once all of that’s configured as I like it, I don’t touch’em: they’re perfect (for me).

Mint 7 ships with KDE 4 though, which uses Plasma. Trying to get Plasma configured just right is like trying to fine-tune a space ship: it sounds like fun, but it just Ain’t Gonna Happen (tm).

While trying to configure Plasma, I ran sudo aptitude safe-upgrade, and went to bed. I awoke to hell. Well, desktop hell. None of my panels were showing, the Plasma “cashew” was missing, and right-clicking on the desktop did nothing. Fark!

To make a long story short, Steely in #LinuxMint.com on irc.spotchat.org saved me. He corrected my sources.list , and after running mintupdate, life was peachy!

Apparently, one is not supposed to use aptitude’s safe-upgrade tool, because it’ll install packages that Linux Mint doesn’t want you to.

How/why/what/?!?

<steely> mint will fix packages or omit items until they get what they consider a stable base

<steely> mint uses updates levels 1 to 5 and mintupdate is set to use levels 1 to 3 by default, 4 and 5 has kernel and driver and other package updates

<steely> if you use mintupdate you will rarely have problems but if you use the terminal it ignores levels and can introduce packages that conflict with mint's patches / fixes

So there you have it. When running Linux Mint, use mintUpdate, not aptitude, to perform upgrades.

In case anyone else is plagued by this problem, the solution is to comment out this line in /etc/apt/sources.list :

deb http://ppa.launchpad.net/kubuntu-ppa/ppa/ubuntu jaunty main

and uncomment this line:

deb http://archive.ubuntu.com/ubuntu/ jaunty-backports main restricted universe multiverse

Next, use mintUpdate to upgrade the rest of your packages, reboot, and pray to whichever god(s) you believe in.