published 2 months ago
(21.07.2008 00:19)
$KCODE = 'u'
require 'rubygems'
require_gem 'activerecord'
require 'rest_client'
class MyModel < ActiveRecord::Base
end
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:database => "hello_world",
:encoding => 'UTF8'
)
MyModel.find(:all).each do |record|
p RestClient.post('http://localhost:5984/hello_world', record.to_json)
end
Posted in ruby, coding | Tags activerecord, couchdb, json, mysql, rest, ruby | no comments | no trackbacks
published 3 months ago
(22.06.2008 16:23)

taken at the Rails Konferenz 2008 by patrick lenz.
Posted in ruby, coding, mac | Tags 2008, konferenz, mac, rails, ruby | no comments | no trackbacks
published 12 months ago
(07.10.2007 14:02)
duck typing is actually a simple concept. the best explanations i have come across (from wikipedia 1,
2):
»Suppose you see a bird walking around in a farm yard. This bird has no label that says ‘duck’. But the bird certainly looks like a duck. Also, he goes to the pond and you notice that he swims like a duck. Then he opens his beak and quacks like a duck. Well, by this time you have probably reached the conclusion that the bird is a duck, whether he’s wearing a label or not.” (Immerman 1982, p. 102)«
So, in programming, duck typing is a style of dynamic typing in which an object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class, or implementation of a formal interface.
The ruby mailing list has a great post called ”How to duck type? - the psychology of static typing in Ruby”, explaining the rationale and why duck typing is a good thing (in ruby). an excerpt:
»Many people coming to Ruby from a statically-typed language are somewhat
afraid of Ruby’s dynamism, or “don’t get it(TM)”. David Black and I (edit: Tim Bates)
believe that this is in part because it is thought that the uncertainty
and changeability built into Ruby are dangerous and one wants to find
shelter from them.«
Posted in ruby, coding | Tags duck, ruby, typing | no comments
published about 1 year ago
(18.04.2007 13:37)
»Because if itself is an expression, you can get really obscure with statements such as:«
if artist == "John Coltrane"
artist = "'Trane"
end unless nicknames == "no"
from the original pickaxe by dave thomas.
Posted in ruby, coding | Tags coding, expressions, madness, ruby | 1 comment
published about 1 year ago
(18.03.2007 11:20)
update: here’s the same for PHP’s XML Parser.
a quick comparison of the two libraries available for processing XML in ruby shows dramatic performance differences.
am i missing something, is there a fundamental flaw in the test? of course REXML is pure ruby, while libxml is C; but can the difference really be so huge?
loading an xml file
| file size |
libxml |
REXML |
factor |
| 10KB |
0,83 |
39,17 |
47,0 |
| 100KB |
6,67 |
306,56 |
46,0 |
| 1.6MB |
71,88 |
3954,21 |
55,0 |
simple xpath expression
| file size |
libxml |
REXML |
factor |
| 10KB |
0,12 |
124,68 |
1004,7 |
| 100KB |
0,67 |
678,11 |
1016,8 |
| 1.6MB |
6,21 |
22578,18 |
3633,6 |
the test code
def benchmark
start = Time.new.to_f
10.times { yield }
puts ((Time.new.to_f - start) / 10) * 1000
end
doc = nil
File.read('products.xml')
require 'rubygems'
require 'xml/libxml'
benchmark do
doc = XML::Document.file("products.xml")
end
benchmark do
doc.find('//articles/article/shortdesc').each do |node|
end
end
require "rexml/document"
benchmark do
doc = REXML::Document.new File.read("products.xml")
end
benchmark do
doc.elements.each("//articles/article/shortdesc") do |node|
end
end
Posted in ruby, php, coding | Tags benchmark, libxml, rexml, ruby, xml | no comments
published over 2 years ago
(21.05.2006 10:49)
rails_conf << phillip

i’ll be in london for a good week in september, mainly to attend the first european conference held on ruby on rails.
i’m excited about meeting the people behind ruby/rails in person, and to experience the rails community! from what i’ve learnt by reading blogs, books, documentation, emailing, viewing videos and listening to podcasts, and visiting sites created in rails i believe they are the most “human” and broad-minded of all programming communities i know. so i believe it will be a pleasant and fun experience, too. beside the obvious – getting a much deeper insight into the tech side of things and learning about rails’ future.
another reason i’m going is to commit myself more to the technology. having been a php guy all my developer life, today my brain says “use rails, it’s superior”, but habit and some lazy parts of my brain sometimes still respond “hey why bother – you already know one very powerful web development environment really well, and have invested a lot in it”. i believe the experience of attending a rails conference will provide a breakthrough and convince all of me, and only a warm and fuzzy feeling will be left.
since i’ve been to london only once, i’ll stay there more than a week. the conference is on the 14th/15th, but i’ll be in town from the weekend before the conference to the weekend after it (9th-17th). i’m looking forward to being in london again, too. i’m really curious – the last time i went was in the early 90s.
Posted in ruby, travel, coding | Tags community, development, london, php, rails, ruby, thoughts, travel, web
published over 3 years ago
(28.02.2006 20:31)
EDIT (11.11.2007): Dir#glob (aliased as: Dir[]) could also be used.
find all files in a directory matching some pattern
list = Dir.new('.').entries.select { |f| f =~ /_\d{2}.(jpg|gif)$/ }
=> ["11-029_42.jpg", "1209-180_32.jpg", "1210-180_32.jpg", "1218-180_32.jpg"]
list.size
=> 4
Posted in ruby, coding | Tags ruby
published over 3 years ago
(31.01.2006 23:25)
this chunk of code is completely useless, apart from the fact that it let me practise how ruby does arrays, objects, constants, observers and singletons. and how to pass a variable number of arguments to a function method and set defaults for arguments that weren’t passed. plus it had me thinking about “what-goes-where” and coupling in object oriented programming.
maybe not so useless after all!

from the ingenious flickr group –Stick Figures In Peril–
require 'observer'
require 'singleton'
class StickFigure
MALE = 0
FEMALE = 1
attr_reader :name
def initialize(attrs)
@name = attrs[:name] ||= 'Anonymous Coward'
@sex = attrs[:sex] ||= MALE
@is_lazy = attrs[:is_lazy] ||= false
@in_peril = false
end
def in_peril!
@in_peril = true
puts @name + ' is in peril!'
end
def to_s
puts "\nHi! I'm " + @name + ' and I am ' + (@is_lazy == true ? 'a little' : 'not') + ' lazy.'
end
alias :introduce_self :to_s
end
class CoffeeDrinkingStickFigure < StickFigure
def have_coffee
puts @name + ' has a cup of coffee.'
pot = get_coffee_pot
pot.pour_coffee
if pot.needs_refill?
make_more_coffee unless @is_lazy
end
return_to_desk
end
def return_to_desk
puts @name + ' returns to ' + (@sex == FEMALE ? 'her' : 'his') + ' desk.'
get_coffee_pot.notify_observers(get_coffee_pot, self)
end
def make_more_coffee
puts @name + ' refills the coffee pot.'
pot = get_coffee_pot
pot.refill
end
private
def get_coffee_pot
CoffeePot.instance
end
end
class CoffeePot
include Singleton
include Observable
attr_reader :cups_left
CUPS_WHEN_FULL = 2.5
def initialize
@cups_left = 0
self.refill
self.add_observer(BigBrother.new)
end
def pour_coffee
@cups_left -= 1
changed
end
def refill
@cups_left = CUPS_WHEN_FULL
changed
end
def is_empty?
@cups_left == 0
end
def needs_refill?
@cups_left < 1
end
end
class BigBrother
def update(pot, who_got_coffee)
if pot.needs_refill?
puts 'The coffee pot needs a refill and ' + who_got_coffee.name + " didn't refill it!\n"
who_got_coffee.in_peril!
else
cup_string = pot.cups_left == 1 ? 'is 1 cup' : 'are ' + pot.cups_left.to_s + ' cups'
puts "There #{cup_string} of coffee left."
end
end
end
stick_figures = [
CoffeeDrinkingStickFigure.new(:name => 'Miss Piggy', :sex => StickFigure::FEMALE),
CoffeeDrinkingStickFigure.new(:name => 'Bert'),
CoffeeDrinkingStickFigure.new(:name => 'Kermit', :is_lazy => true),
CoffeeDrinkingStickFigure.new(:name => 'Ernie', :is_lazy => true)
]
stick_figures.each do |stick_figure|
stick_figure.introduce_self
stick_figure.have_coffee
end
Posted in ruby, coding, photography | Tags flickr, oop, ruby