26th Ruby Kansai Workshop in Kyoto

Posted by stoyan

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:

“5 ways to run Ruby on Rails web applications”

by okkez-san

presentation slides

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).

“Agile web posting with Ruby”

by ujihisa-san

presentation slides

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

20th “Ruby for beginners” lesson

With a ‘little’ help from google my answers was:

FizzBuzz

(‘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 }

99 bottles of beer

I like Jeremy Voorhis’ implementation . Just modified it to handle 1 bottle situation:

#!/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

Posted by stoyan

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

Posted by stoyan

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
}

Throttler Ramaze helper

Posted by stoyan

After reading Rails Plugin: Throttler I created a Throttler Ramaze Helper .

  • What is it for? ”...The throttling plugin monitors the load of your server and allows you to disable certain features of your app when the load is too high…”
  • How to use it?
    • Copy throttler.rb to /{ramaze_gem_dir}/lib/ramaze/helper/throttler.rb
    • In your controllers code:
      class MainController < Ramaze::Controller
        helper :throttler
      
        def index
            # (optional): override the threshold when calling throttled?()
            # unless throttled?(10.00) ...
            unless throttled?
               "Hello World" 
            else
               "Overloaded: #{current_load}" 
            end
        end
      
      protected
         # (optional) set another threshold value
         def threshold
            5.00
         end
      end
      

Thanks a lot to Kashia from #ramaze for the help.

Later I start thinking “Hm, maybe that one is better to be Rack middleware ...”. Maybe next try…

Rails on GIT

Posted by stoyan

Semi-official Rails GIT repositories, maintained by Michael Koziarski :

$ git clone git://github.com/NZKoz/koz-rails.git

Update 13-Mar-2008: Steve’s git-svn mirror of the Rails repo (see the comments)

git clone git://git.sanityinc.com/rails.git

Merb on Joyent Accelerator (part 1)

Posted by stoyan

The Accelerator including almost everything by default – Ruby, gems etc. Adding the Merb framework was without any problems. Some tips:

Pre-requirements:

$  sudo gem install mongrel json json_pure erubis mime-types rspec hpricot mocha \
                    rubigen haml markaby mailfactory Ruby2Ruby --no-rdoc --no-ri

Switching to merb trunk:

$ svn co http://svn.devjavu.com/merb/trunk merb
$ cd merb/
$ rake gem
$ sudo gem install pkg/merb-0.4.2.gem --no-rdoc --no-ri

Swithing to merb_helpers trunk (for better form helpers):

$ svn co http://svn.devjavu.com/merb/plugins/merb_helpers
$ cd merb_helpers/
$ rake gem
$ gem list
$ sudo gem install pkg/merb_helpers-0.4.1.gem --no-rdoc --no-ri
There was problems with the datamapper installation. The do_sqlite3 gem compilation was successful. For do_mysql gem one however I needed:
cd /opt/local/lib/ruby/gems/1.8/gems/do_mysql-0.2.2/ext/
make clean
ruby extconf.rb --with-mysql-include=/opt/local/include/mysql \
                --with-mysql-lib=/opt/local/lib/mysql
make
cd ../
rake gem
gem install pkg/do_mysql-0.2.2.gem \
    --no-rdoc --no-ri -- \
    --with-mysql-include=/opt/local/include/mysql \
    --with-mysql-lib=/opt/local/lib/mysql

My coworker Jeffrey Gelens was pretty excited about merb+datamapper combo. He even submitted some composite indexes patch to their repository.

Joyent Facebook Developer Accelerator

Posted by stoyan

Got my own Joyent Facebook Developer Accelerator . Will try to put some Merb application on it. To read:

Rails 2.0 Preview Release

Posted by stoyan

Tried the new Rails version. After
$ rails rls20PR && cd rls20PR
$ rake rails:freeze:edge TAG=rel_2-0-0_PR
$ rake rails:update
for every rake task i’ve got:
rake aborted!
no such file to load -- active_resource

The problem

edge_rails requires ActiveResource, and the rake freeze:edge:rails don’t checkout it.

The solution:

$ svn co http://svn.rubyonrails.org/rails/trunk/activeresource vendor/rails/activeresource
$ script/about
...
Edge Rails revision       7712

Update 04-Oct-2007 : via s.ross post to the “Ruby on Rails: Talk” Google Group:

Do another rake rails:freeze:edge. I know it sounds strange, but the first one installs a lot of the edge version as well as the Rake tasks. The second time you do it, the new Rake task knows enough to grab ActiveResource.

So the right procedure is:
$ rails --version
Rails 1.2.3
$ rails rls20PR && cd rls20PR
$ rake rails:freeze:edge TAG=rel_2-0-0_PR
$ rake rails:update
$ rake rails:freeze:edge TAG=rel_2-0-0_PR
Update 05-Oct-2007 : Another way to have a fresh rails2 directory:
$ sudo svn co http://svn.rubyonrails.org/rails/tags/rel_2-0-0_PR /opt/rails2
$ ruby /opt/rails2/railties/bin/rails rls20PR
$ cd rls20PR/vendor
$ ln -s /opt/rails2 rails

Speedup Ruby on Linux

Posted by stoyan

Origin: discussion on comp.lang.ruby

...It appears that when compiled with —enable-pthread ruby will call sigprocmask MANY MANY more times than without. You can verify this with strace -c:
$ strace -c ruby -e '1.upto(100000) {|i| i.to_s}' 
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 99.65    1.462627           7    200006           sigprocmask

Solution:

  • From sources: the default is without pthreads, just do not add —enable-pthread to your compile options
  • For Gentoo:
    # USE="-ipv6 -threads cjk" emerge ruby
      

Update 2007/12/13: Replace the default ruby install on Ubuntu

$ sudo apt-get install ruby irb ri rdoc ruby1.8-dev
$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p110.tar.gz
$ tar xvzf ruby-1.8.6-p110.tar.gz && cd ruby-1.8.6-p110
$ sudo apt-get build-dep ruby1.8
$ ./configure --prefix=/usr
$ make
$ sudo make install
$ ruby -v
ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-linux]

18th Ruby/Rails meeting in Hirakata

Posted by stoyan

On 11-August-2007 there was a Ruby/Rails meeting in Hirakata . For the first time Osaka meeting is so far from Osaka. About 1.5 hour with train. It was soooooo hot walking from the station to the university :(

I put some notes about the event on my wiki . All presentations was very interesting. And the “nomikai” after that also ;)

I also gave a presentation about NginX . The slides are on SlideShare :

Junebug Wiki on BrightBox

Posted by stoyan

I installed JuneBug wiki on bbox . There was some bugs in the installation documentation so will put small notes here. The installation process is for Ubuntu Dapper box on BrightBox beta hosting. Adjust for your own situation:

# Ubuntu/Debian specific
apt-get install sqlite3 libsqlite3-dev

# the original document missing http://
# in front of the url
# ERROR: uri is not an HTTP URI
gem install sqlite3-ruby --source http://code.whytheluckystiff.net

gem install junebug-wiki -y

Question: Is it still needed to install custom sqlite3 version or the stock one from the gems repository will do the job?

Rubygems on Debian "one-click installer"

Posted by stoyan

Rubygems on Debian “one-click installer”

Before I mentioned Rails on Debian ‘one-click installer’ . Seems there is also a way to install ruby gems with apt-get on debian-like systems . It’s in japanese, so just a short translation:

Add to your /etc/apt/sources.list :

deb http://backports.mithril-linux.org sarge-backports main contrib non-free
deb-src http://backports.mithril-linux.org sarge-backports main contrib non-free

Add to your /etc/apt/preferences :

Package: rubygems
Pin: release a=sarge-backports
Pin-Priority: 900

Install rubygems:

apt-get update
apt-get install rubygems
The rubygems are installed in /var/lib/gems so we need to change the PATH environment variable in order to access gem command. Inside /etc/profile :
export PATH="/var/lib/gems/1.8/bin:$PATH" 

Install rails

gem install rails -y

Offtopic: ...No longer will we have to use RESTful URLs such as article/1;edit. We’re going back to the more normal-looking article/1/edit …

OpenID in Edge Rails

Posted by stoyan

So we got millions of people with an OpenID by virtue of their AIM account. Great. Now they just need a place or two to actually use it. —DHH

changeset 6245Added simple OpenID authentication Rails plugin wrapper for ruby-openid

See also: There is already a lot of OpenID providers:

Cookie Based Sessions in Current Rails

Posted by stoyan

Seems Cookie Based Sessions are the New Default in Edge Rails : ...Cookie-based sessions are just faster to retrieve and process than hitting the file-system on every request. Plus, I would imagine they scale considerably better as they’re persisted on the client side and have no server-side persistence requirements…

So I made a cookie-based sessions patch agains current Rails (actionpack-1.13.2). Usage (from inside your project):
rake rails:freeze:gems
cd vendor
wget http://zhware.net/code/ruby/ror/session_store-1.13.2.patch
patch -p0 < session_store-1.13.2.patch

14th Ruby Kansai Workshop

Posted by stoyan

Today (17-Feb-2007) was the 14th Ruby Kansai meeting in Kyoto. Some notes on Stikipad (and copy on my wiki ). For bad luck both Stikipad and JunebugWiki cannot do trackbacks, so I’ll use this diary to ping the event page.

Interesting conversations during and after the lessons.