Install merb-dev

Trying to install the development version of merb , i've got:

no such file to load -- extlib/tasks/release

Seems the extlib gem is too old for merb. Fix:

$ git clone git://github.com/sam/extlib.git
$ cd extlib
$ rake gem
$ sudo gem install pkg/extlib-0.9.4.gem

The rest of installation process:

$ sudo gem install ruby2ruby --version '= 1.1.8'
$ sudo gem install ParseTree --version '= 2.1.1'
$ sudo gem install sake erubis mime-types
$ sake -i http://merbivore.com/merb-dev.sake
$ mkdir ~/merb_src && cd ~/merb_src
$ sake merb:clone
$ sake merb:install:all

Your first application:

$ merb-gen app my_app --flat && cd my_app

TinyURL Ramaze Application

Created tinyurl ramaze application - everything in one file - model, view, controller. Required gems: sqlite3-ruby, sequel, validatable, ramaze.Even have an API:

$ curl -O turl.rb http://zhware.net/code/ruby/ramaze/turl.rb.txt
$ ruby turl.rb
# browse http://localhost:7000/
# shorten url
$ curl http://admin:secret@localhost:7000/_api?turl=http://zhware.net/code/ruby/
# restore the original url
$ curl http://admin:secret@localhost:7000/_api?url=abc
# number of hits for given turl
$ curl http://admin:secret@localhost:7000/_api?hits=abc

Do not forget to change the BASE_URL and LOGINS values. By default the database file (sqlite3) will be created in the same directory with turl.rb. If this is not acceptable, change the DB_FILE line.

Update: the turl project is already on github

26th Ruby Kansai Workshop in Kyoto

On 17-May-2008 there was 26th Ruby Kansai Workshop in Kyoto. A lot of fun like always :) The chatlog from the presentations is available on lingr.

I gave a presentation "Ruby off Rails" about developing web applications in Ruby, but without Rails - Sequel, Rack, Ramaze etc. talk. The presentation slides:

The sources from the talk (plus some extras - rack middleware examples) are available on Google sites for download.
There was no time to speek about Tamanegi - my feeds aggregator in Ramaze, but maybe on some other meeting...

There are also another two presentations:

Benchmarking CGI, FastCGI, mod_ruby, mod_rails and mongrel_cluster. Received very good documentation with configs for all mentioned deployment options.
Okkez-san is using Rabbit - very cool presentation tool, written in Ruby! Just needed ruby-gnome. Will try it soon maybe.
Some notes:

  • ebb is still a pain to install
  • mod_ruby is not bad deployment option
  • webrick overperforms fastcgi for simple installs (still a valuable option for intranet for example)
  • thin is fast

I think thin is faster because of the underlaying EventMachine TCP stack implementation in C. The others are just pure Ruby-based, so there is nothing to compare for simple requests (ab benchmarks).

There are sooooo many web services around. Can the posting to them be automated, using Ruby-based tools?

!!! Big Fat Warning: The presented tools will make you very 'productive'. Maybe your friends will stop following you. Use on your own risk! ;)

Main working horse - www:mechanize ruby port:

gem install mechanize

So far so good. But can you post directly from VIM? Seems there is vimscript - i way to customize vim in different languages. Try:

vim --version

and if you see +ruby you can use ruby in vimscripts like:

# ~/.vim/plugins/some_plugin.vim
...
VIM.evaluate(’..’) evaluate %[sdfsds]
...

And even better: there are already a lot of ready ruby-based VIM plugins in the CodeRepos vim repository

There was also the 20th "Ruby for beginners" lesson after that. With a 'little' help from google my answers was:

  • FizzBuzz variation ('Aho','Bow' and also say 'Aho' for numbers that contain '3'):
 puts (1..100).map { |i| (s=(i%3==0?'Aho':'')+(i%5==0?'Bow':'')+(i.to_s =~ /3/?'Aho':''))==""?i:s }
#!/usr/bin/env ruby

def expensive
  @expensive ||=
    begin
      n = 99
      buf = ""
      begin
        buf << "#{n} bottle#{n>1?'s':''} of beer on the wall\n"
        n -= 1
      end while n > 0
      buf << "no more bottles of beer"
    end
end

puts expensive

'X-Sendfile' for Rack, take 2

In the spirit of “Fork me if you like me" I forked the Rack project on github and applied my ‘X-Sendfile’ related changes. Now you can have download acceleration with:

use Rack::Static, :urls => ["/files"], :root => "public",
    :extra => { 'X-Sendfile' => 'yes' }

I just made a small change to pass the :extra parameter down to Rack::File and adding ‘X-Sendfile’ related headers for nginx, apache and lighttpd. :extra parameter can also contain others, non x-sendfile related headers (like cache-control etc.):

use Rack::Static, :urls => ["/css","/images"], :root => "public", :extra => { 
  'Cache-Control' => 'max-age=86400, public',
  'Expires' => (Time.now + 86400).utc.rfc2822
}

I also added some examples for rackup and middleware to the examples/ directory. Merged the josh’s daemonize fork too, but today (11-May-2008) it was merged to the master branch.

Rack::XStatic Middleware - 'X-Sendfile' for Rack

After I found the Rails X-Sendfile plugin, decided to code something similar for Rack (and Ramaze). The result: Rack::XStatic middleware. Example usage: xfile.ru rackup file. Start it with:

rackup -s thin -p 7000 xfile.ru

For using it with nginx 'X-Accel-Redirect':

# inside your .ru rackup file:
use Rack::XStatic, :urls => ["/down", "/files"], :root => "public",  :extra => { 
  'X-Sendfile-Type' => 'nginx',
  'X-Accel-Limit-Rate' => '1024',
  'X-Accel-Charset' => 'utf-8'
}
# inside your nginx.conf file:
location ~ ^/(down|files)/ {
  internal;
  root /full/path/to/rack/public;
}

'X-Sendfile-Type' can be also 'lighttpd', 'apache' or just 'yes' for adding only the 'X-Sendfile' header
Rack::XStatic also allow me to add custom headers to the response:

use Rack::XStatic, :urls => ["/css","/images"], :root => "public", :extra => { 
  'Cache-Control' => 'max-age=86400, public',
  'Expires' => (Time.now + 86400).utc.rfc2822
}

Pushing and Pulling Branches on Github

Original article

What worked for me (tamanegi/open-uri branch):

# create local branch
$ git checkout -b open-uri

# pushing branch
$ git push origin open-uri

# pulling remote branch
$ git branch -d open-uri
$ git branch -f open-uri origin/open-uri
$ git checkout open-uri
$ git pull

# merge and delete
$ git checkout master
$ git merge open-uri
$ git push origin :open-uri