My attempt to beautify Ruby code in TextMate

Posted by kain Sat, 13 Sep 2008 12:26:00 GMT

This is based on Paul Lutus work, version 2.4

#!/usr/bin/env ruby

=begin
/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
=end

PVERSION = "Version 2.4, 08/25/2008"

# Modified to work as a TextMate command by Claudio Poli (http://www.icoretech.org)

$tabSize = 2
$tabStr = " "

# indent regexp tests

$indentExp = [
  /^module\b/,
  /^class\b/,
  /^if\b/,
  /(=\s*|^)until\b/,
  /(=\s*|^)for\b/,
  /^unless\b/,
  /(=\s*|^)while\b/,
  /(=\s*|^)begin\b/,
  /(^| )case\b/,
  /\bthen\b/,
  /^rescue\b/,
  /^def\b/,
  /\bdo\b/,
  /^else\b/,
  /^elsif\b/,
  /^ensure\b/,
  /\bwhen\b/,
  /\{[^\}]*$/,
  /\[[^\]]*$/
]

# outdent regexp tests

$outdentExp = [
  /^rescue\b/,
  /^ensure\b/,
  /^elsif\b/,
  /^end\b/,
  /^else\b/,
  /\bwhen\b/,
  /^[^\{]*\}/,
  /^[^\[]*\]/
]

def makeTab(tab)
  return (tab < 0) ? "" : $tabStr * $tabSize * tab
end

def addLine(line,tab)
  line.strip!
  line = makeTab(tab)+line if line.length > 0
  return line + "\n"
end

def beautifyRuby

  comment_block = false
  program_end = false
  multiLine_array = []
  multiLine_str = ""
  tab = 0
  source = STDIN.read
  dest = ""
  source.split("\n").each do |line|
    if(!program_end)
      # detect program end mark
      if(line =~ /^__END__$/)
        program_end = true
      else
        # combine continuing lines
        if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
          next
        end

        # add final line
        if(multiLine_str.length > 0)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
        end

        tline = ((multiLine_str.length > 0) ? multiLine_str:line).strip
        if(tline =~ /^=begin/)
          comment_block = true
        end
      end
    end
    if(comment_block || program_end)
      # add the line unchanged
      dest += line + "\n"
    else
      comment_line = (tline =~ /^#/)
      if(!comment_line)
        # throw out sequences that will
        # only sow confusion
        while tline.gsub!(/\{[^\{]*?\}/,"")
        end
        while tline.gsub!(/\[[^\[]*?\]/,"")
        end
        while tline.gsub!(/'.*?'/,"")
        end
        while tline.gsub!(/".*?"/,"")
        end
        while tline.gsub!(/\`.*?\`/,"")
        end
        while tline.gsub!(/\([^\(]*?\)/,"")
        end
        while tline.gsub!(/\/.*?\//,"")
        end
        while tline.gsub!(/%r(.).*?\1/,"")
        end
        # delete end-of-line comments
        tline.sub!(/#[^\"]+$/,"")
        # convert quotes
        tline.gsub!(/\\\"/,"'")
        $outdentExp.each do |re|
          if(tline =~ re)
            tab -= 1
            break
          end
        end
      end
      if (multiLine_array.length > 0)
        multiLine_array.each do |ml|
          dest += add_line(ml,tab)
        end
        multiLine_array.clear
        multiLine_str = ""
      else
        dest += addLine(line,tab)
      end
      if(!comment_line)
        $indentExp.each do |re|
          if(tline =~ re && !(tline =~ /\s+end\s*$/))
            tab += 1
            break
          end
        end
      end
    end
    if(tline =~ /^=end/)
      comment_block = false
    end
  end

  STDOUT.write(dest)
  # uncomment this to complain about mismatched blocks
  # if(tab != 0)
  #   STDERR.puts "#{path}: Indentation error: #{tab}"
  # end
end

beautifyRuby

Discover your REXML version in your Ruby installation 1

Posted by kain Tue, 26 Aug 2008 05:17:00 GMT

ruby -vrrexml/rexml -e 'p REXML::VERSION,PLATFORM'

DoS vulnerability in REXML

Posted by kain Mon, 25 Aug 2008 21:24:00 GMT

http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/

Passenger (aka mod_rails) PATH on Leopard 1

Posted by kain Sat, 12 Jul 2008 17:48:00 GMT

 Passenger uses a little subset of your PATH.

So if you have installed ruby, gem and misc binaries in other paths you cannot access them via your passenger rails application.

Here comes a solution, create an executable script ruby_with_env, I wrote it in my /usr/local/bin:

#!/bin/bash
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/graphviz/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin:/usr/local/git/bin/
:$PATH"
/usr/local/bin/ruby $*

 Now point the file to your PassengerRuby directive in your apache configuration file and restart.

no such file to load -- sqlite3/database 6

Posted by kain Sun, 06 Jul 2008 21:53:00 GMT

[kain@kain-osx:/usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.2]$ sudo find . -perm 0662 -exec chmod 664 {} \;

Update: Ruby Enterprise Edition is ahead of all: link

Another update: This is now fixed http://weblog.jamisbuck.org/2008/8/27/sqlite3-ruby-1-2-3