Samstag, 19. Mai 2007

Testing with Rails

Jay Fields has labeled all his great posts about rails testing with: "RailsConf2007". Now we can find all relevant posts more easily. Thanks.
Source: http://blog.jayfields.com/2007/05/railsconf07-testing-presentation.html

Labels: , , ,

Mittwoch, 16. Mai 2007

The new Art of Flamewar

Please take the following not to serious. Flamewar can pull your energy off. I think the best thing we can do is respect other people independent from the technologies they are using. Sometimes it is hard to talk to people when they want to argue you into something. Encounter them with acceptance for there opinion. Do not try argue them into something. Instead explain your opinion only if they really want to know your opinion. Just my two cent.
Rails vs. Java


Rails vs. PHP



Source: http://www.railsenvy.com/

Labels: , ,

Samstag, 12. Mai 2007

Backup one file

I found my self often doing something like that:
sudo cp /boot/grub/menu.lst /boot/grub/menu.lst_bu_2007-05-12_13-20
With the following script all we need to do is:
sudo backup-one-file /boot/grub/menu.lst
And the result is a backup created in /boot/grub/menu.lst_bu_2007-05-12_13-21-30
#!/usr/bin/env ruby

require 'fileutils'

if ARGV.empty?
puts "Usage: backup-one-file <file-name>"
exit
end

source = ARGV[0]

unless File.exist?(source)
puts "Error: File #{source} does not exist!"
exit
end

target = source + "_bu_" + Time.now.strftime("%Y-%m-%d_%H-%M-%S")

begin
FileUtils.cp(source, target)
rescue Exception => e
puts "Could not copy to #{target}"
puts e.message
puts "Maybe you need to run as root!"
else
puts "Backup created in: #{target}"
end

Labels: , ,

Mittwoch, 9. Mai 2007

ri and ri_proxy

Yesterday i have written a little caching proxy script for ri. The script forwards any invocation to ri. The results are cached in a sqlite3 database. So that any subsequent calls are much faster, not fast as i wished but definitely faster.
#!/usr/bin/env ruby

require 'rubygems'

ROOT = File.dirname(__FILE__)
BASE_NAME = File.basename(__FILE__)

DB_FILE = File.join(ROOT, BASE_NAME + '.sqlite3')
DB_CONFIG = {:adapter => 'sqlite3', :timeout => 5000, :database => DB_FILE}

LOG_FILE = File.join(ROOT, BASE_NAME + '.log')

# speed up active record loading a bit
require 'active_support'
RAILS_CONNECTION_ADAPTERS = ['sqlite']

require 'active_record'
ActiveRecord::Base.logger = nil # Logger.new(LOG_FILE)
ActiveRecord::Base.establish_connection(DB_CONFIG)

class RiMethodEntry < ActiveRecord::Base
end

force = false
if !RiMethodEntry.table_exists? or force
ActiveRecord::Schema.define(:version => 1) do
create_table :ri_method_entries, :force => true do |t|
t.column :name, :string, :null => false
t.column :text, :text, :null => false
end
add_index :ri_method_entries, :name
end
end

ARGV.length > 0 or raise Exception.new("No arguments found!")
name = ARGV[0]
entry = RiMethodEntry.find_by_name(name)
if entry
puts entry.text
exit
end

args = ARGV.collect { |a| %{"#{a}"} }
text = `ri #{args.join(" ")}`
found = text.starts_with?('-')

puts text
exit unless found

RiMethodEntry.create(:name => name, :text => text)

Labels: , , ,

Donnerstag, 3. Mai 2007

Install ruby on debian sytems the apt way

If you want to install ruby through apt you normally get only the interpreter. If you want to install ruby with all it's tools, you need to take a look into the package description to find out what is missing.
apt-cache show ruby1.8
The output should be similar to the following:
Package: ruby1.8
Priority: optional
Section: interpreters
Installed-Size: 296
Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: akira yamada <akira@debian.org>
Architecture: i386
Version: 1.8.5-4ubuntu2
Depends: libc6 (>= 2.5-0ubuntu1), libruby1.8 (>= 1.8.5)
Suggests: ruby1.8-examples, rdoc1.8, ri1.8
Filename: pool/main/r/ruby1.8/ruby1.8_1.8.5-4ubuntu2_i386.deb
Size: 217212
MD5sum: d2cedcce835b84af193332a413344b89
SHA1: 40c93e9a5df7ea9ed2567f6c8d843282c69a800a
SHA256: 7e756450dbdc504f94f67d936594c28d48923557fb7d4c32d6e9bfe147123729
Description: Interpreter of object-oriented scripting language Ruby 1.8
Ruby is the interpreted scripting language for quick and easy
object-oriented programming. It has many features to process text
files and to do system management tasks (as in perl). It is simple,
straight-forward, and extensible.
.
This package provides version 1.8 series of Ruby.
.
On Debian, Ruby 1.8 is provided as separate packages. You can get
full Ruby 1.8 distribution by installing following packages.
.
ruby1.8 ruby1.8-dev ri1.8 rdoc1.8 irb1.8 ruby1.8-elisp
ruby1.8-examples libdbm-ruby1.8 libgdbm-ruby1.8 libtcltk-ruby1.8
libopenssl-ruby1.8 libreadline-ruby1.8
Bugs: mailto:ubuntu-users@lists.ubuntu.com
Origin: Ubuntu
Task: kubuntu-desktop
As you can see the full install of ruby and it's tools would be:
sudo apt-get install ruby1.8 ruby1.8-dev ri1.8 rdoc1.8 irb1.8 \
ruby1.8-elisp ruby1.8-examples libdbm-ruby1.8 libgdbm-ruby1.8 \
libtcltk-ruby1.8 libopenssl-ruby1.8 libreadline-ruby1.8
So far so good. Maybe you need to add the the universe repositories to your /etc/apt/sources.list to install all the packages.
It would be nice if debian could provide a meta-package for those people who want all the ruby tools anyway.

Labels: ,

Mittwoch, 2. Mai 2007

Installing ruby on rails on ubuntu 6.06 LTS by compiling nearly anything

#!/bin/sh

sudo apt-get install build-essential

cd /tmp

# download
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.bz2
wget http://rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz
wget http://www.zlib.net/zlib-1.2.3.tar.gz
wget http://www.sqlite.org/sqlite-3.3.17.tar.gz

# unpack
tar xf ruby-1.8.6.tar.bz2
tar xf rubygems-0.9.2.tgz
tar xf zlib-1.2.3.tar.gz
tar xf sqlite-3.3.17.tar.gz

# install
sudo apt-get install libreadline5 libreadline5-dev

cd /tmp/sqlite-3.3.17 &&amp; ./configure && make && sudo make install
cd /tmp/zlib-1.2.3 &&amp; ./configure && make && sudo make install
cd /tmp/ruby-1.8.6 &&amp; ./configure && make && sudo make install

cd /tmp/rubygems-0.9.2 && sudo ruby setup.rb
sudo gem install sqlite3-ruby -y
sudo gem install rails -y
sudo gem install mongrel -y

# create a rails test application
cd /tmp
rails rails_test --database=sqlite3
cd rails_test/
./script/generate model user name:string
rake db:migrate
./script/generate scaffold user
./script/server
If you need more you can look at the great deprec tools.
Update: A colleague told me to use "checkinstall". So we will not bypass the package management. Good point.

Labels: , ,

Montag, 30. April 2007

locate a line of code

Sometimes you want to know in which file and line a method is defined.
This snippet should do that job.

Labels: ,

Donnerstag, 26. April 2007

Really simple templates with ruby

Example:
template = 'Hello #{name}!'
name = 'Paul'
puts eval(%{"#{template}"}) # => Hello Paul!
name = 'Holger'
puts eval(%{"#{template}"}) # => Hello Holger!
Note that the template definition use a single qutoed string, so no variables are evaluated within the template string.
Also note that the template can't use unescaped double quotes, because the template evaluation uses them to delimit the template string.

Labels: ,

Mittwoch, 25. April 2007

Warum kann man die Programmiersprache Ruby empfehlen?

  • 100% objektorientiert(alles ist ein Objekt)
  • sehr dynamisch
    • monkey patching
  • konsistent(entwickelt nach dem Prinzip der geringsten Überraschung)
  • verlangt nur ein Minimum an syntaktischer Disziplin
  • integrierte DSL's lassen sich einfach implementieren
  • Funktionale Programmierung wird unterstützt
...die Liste läßt sich sicher noch verlängern. Welche Argumente gibt es noch?

Labels: ,