deferred until inspiration hits

Textmate environment and ruby (again)

Posted by Chris Roos Tue, 22 Aug 2006 23:53:00

I’ve recently upgraded the version of ruby on my machine and came up against problems similar to this. The problem previously was that textmate wasn’t finding ruby at all. This time, textmate was finding the wrong version of ruby.

I found the following script useful in determining exactly which ruby binary textmate was using.

#!/usr/bin/env ruby

require 'rbconfig'
puts Config::CONFIG['bindir']

Simply paste that into a textmate document, select it and hit Ctrl-r. The path of the ruby binary should print right there in the window.

To see far more info than you’ll ever likely need, replace the line starting puts above, with the following..

Config::CONFIG.sort.each do |key, value|
    puts "#{key} = #{value}"
end

Tags , , ,

TextMate and "env: ruby: No such file or directory"

Posted by Chris Roos Fri, 28 Jul 2006 23:06:00

So, I was coming up against this problem in TextMate just recently.

It’s a bit of a strange error in that you get seemingly conflicting output from TextMate.

In a new TextMate document, I could run (by using the well handy, and very new to me, Ctrl-r key combo)..

/usr/bin/env ruby --version
=> ruby 1.8.4 (2005-12-24) [i686-darwin8.7.1]

/usr/bin/which ruby
=> /usr/local/bin/ruby

#!/usr/local/bin/ruby

puts "hello world" 
=> hello world

However, if I ran..

#!/usr/bin/env ruby

puts "hello world again" 
=> env: ruby: No such file or directory

Hmm, so /usr/bin/env ruby --version works, but when using the very same as the shebang interpreter we get an error. Odd huh.

Well, apparently not; if we read the textmate docs about shell commands.

Although I, in no way, understand this completely; I blindly followed the instructions on that page and got TextMate back to health.

In summary for those that don’t wish to read the docs..

# mkdir ~/.MacOSX/
# cat >> ~/.MacOSX/environment.plist
{ PATH = "/usr/local/bin"; }

Note You’ll want to change the path to the location of the binaries you want found.

Then a quick log out and back in, and all is well with TextMate. Yay.

Tags , ,

Textmate command to display Active Record column attributes

Posted by Chris Roos Fri, 14 Jul 2006 08:18:00

I find myself constantly visiting either the console or the mysql command line to check out the attributes (columns) of active record objects.

I know there is a plugin by Dave Thomas that places the table structure within the model code itself. To be honest I’ve never tried this and it might well suffice (although I’m not 100% convinced I like the idea).

Anyhoo, I just spent some time whilst on the train hacking together a basic textmate command that displays a tooltip containing the table structure. It’s very basic (though you wouldn’t have thought it based on the amount of time it took me together1); but seems to work ok.

The code is below. You’ll need to add a ruby command in the ruby textmate bundle, setting the intput to ‘selected text’ or ‘nothing’, the output to ‘show as tooltip’ and preferably a hotkey that isn’t one of the ones you use every day (however tempting that is).

#! /usr/local/bin/ruby

project = ENV['TM_PROJECT_DIRECTORY']
word = ENV['TM_CURRENT_WORD']

require "#{project}/config/boot"
require "#{project}/config/environment"

klass = Object.const_get(word) rescue nil
if klass and klass.class == Class and klass.ancestors.include?(ActiveRecord::Base)
  columns = klass.columns_hash

  data = []
  data += [%w[column primary sql_type default]]
  data += [%w[------ ------- -------- -------]]
  data += columns.collect { |col, attrs| [col, attrs.primary.to_s, attrs.sql_type.to_s, attrs.default.to_s] }

  STDOUT << data.inject('') do |output, array| 
    output + array.inject('') { |row_str, value| row_str + value.ljust(20) } + "\n"
  end
elsif klass and klass.class == Class and not klass.ancestors.include?(ActiveRecord::Base)
  STDOUT << "#{word} is not an Active Record derived class"
else
  STDOUT << "#{word} was not recognised as a class"
end

It relies on you having your entire rails project (the folder right above app, config, script etc) open in textmate (in order to find the config files). With that, just click on the name of an active record class (it has to be the actual class name at present, it’d be cool if it also worked based on the class definition you are currently in too) and hit your hotkey of choice (best to use the one you assigned to this command). Hey presto, a tooltip containing your table structure.

One odd thing I’ve noticed is that it never recognises standard class constants. It should display ‘this is not an active record derived class’ but instead always displays ‘was not recognised as a class’. Oh well.

Anyone find this remotely useful or want to finish off what I’ve started?

[1] I spent ages trying to develop within the textmate bundle editor before realising it’d be a helluva lot quicker and easier to develop in textmate proper. Hmmm.

Tags , , , ,