All posts in Ruby

Downgrade Rubygems

First let’s list 2 truths here:

  • Rails 2.3.5 will fail with RubyGems 1.5.0 +
  • Rails 2.3.11 will fail with RubyGems 1.7.0 +

So what if you want to deploy or run an application which unfortunately still or initially written in rails 2, given the situation that latest RVM default install rubygems version 1.8.11?

It turns out to be not so painful, cause it’s as simple as just issue one command (take 1.4.2 for example):

gem update --system 1.4.2

Just like the China highway train, sometimes what you need to do is just to stop moving so fast.

ArchLinux, RVM, Ruby, Rails, Nginx, Passenger and You

没看错,就是ArchLinux,怎么,又回来了?其实我一直未曾离开…… i

好了不矫情了,let’s do business.

[Overview]
本文介绍如何在archlinux上快速搭建rails开发环境,其中有很多可能包括我个人的preferences,各位完全可以根据自行需要取舍。

[ArchLinux]
参照Arch自己的 Official Install Guide ii 安装,其实根本不用参照,整个安装wizard做的已经非常非常user friendly了,我觉得。

[RVM]
RVM 的安装依赖 git 和 curl:

$ sudo pacman -S git curl

装之:

$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

注意没有sudo,符合homebrew的philosophy,尽量不要root,何况我们还是作为development环境,rvm官网好像现在已经没有倾向性了,不过我印象中以前也是倾向于装在user的home目录。
Continue reading →

[Footnotes]
  1. 我有一个arch的liveusb,各种机子上一直保存着至少2+个arch的vm,虽然后来由于太占空间被转移到了移动硬盘,可就是一直舍不得删 []
  2. 最近老是official, official的… 你懂的 []

stdout customization

前面介绍完了deployment,接下来该轮到customization了,这个也同样重要。我目前所能想到的需要customize的地方主要有5个地方,都集中在两个文件,并没有想象中的复杂,且听我一一道来:

[page title]

Location: RAILS_ROOT/app/views/layouts/application.html.erb, line 6
<title>$ cat /dev/stdin >> /dev/stdout | tee stdout.samsonw.info</title>

change to whatever text you want.

[header title,sub-title]

Location: RAILS_ROOT/app/views/activities/index.html.erb, line 2, 3
<div id="title" class="grid_24">$ cat /dev/stdin >> /dev/stdout | tee stdout.samsonw.info</div>
<div id="sub_title" class="grid_24">Don't Panic: fatal system error, core dump file not generated ...</div>

change to whatever text you want.
Continue reading →

stdout deployment

首先值得一提的是为了支持HostMonster的fastcgi,我人为copy了几个以raild -D生成的项目结构里面的dispatch文件以及create了一个.htaccess文件,如果你不用fastcgi(通常情况下你不需要,而且也不推荐),你可以简单的删除它们(以下RAILS_ROOT是指rails app的根目录):

RAILS_ROOT/config.ru
RAILS_ROOT/public/dispatch.cgi
RAILS_ROOT/public/dispatch.fcgi
RAILS_ROOT/public/dispatch.rb
RAILS_ROOT/public/.htaccess

[Development Environment]
Assume你的os已经装好ruby,如果没有请自行google,这里不再赘述。

首先update一下rubygems,最近rubygems貌似打了鸡血,更新极其频繁… 且bug具多…如果出了问题建议rollback到1.4.2…
[Note: 由于我使用rvm,所以以下command都没有sudo,根据你的情况你可能需要加上sudo]

$ gem update --system

Continue reading →

stdout on github

stdout, aka LoserLogs or LoserStream is finally on github, check out at https://github.com/samsonw/stdout .

The code is based on rails 2.3.11 instead of rails 3, quite a surprise, isn’t it? Actually I was planning to, but after some research, it turns out my shared host provider HostMonster only support rails 2 at the time of this writing, shame on them.

It was always quite enjoyable to figure this and that out in the process of curating that site, except the deployment experiences on HostMonster. To my surprise, they’re still using Fastcgi to support rails application, again, shame on them.

Last but not least, the project is called “LoserLogs” and copyrighted by “Losers” corporation, cause I and along with some of my friends are all having the “loser” faith in our heart, this is NOT a joke

RubyGems 1.5.2 Manual Upgrade

今天想更新RubyGems,突然发现 gem update ––system 貌似不work了:

$ gem -v
1.5.0
$ gem update --system
Updating RubyGems
ERROR:  While executing gem ... (NoMethodError)
    undefined method `version' for nil:NilClass

turns out 这是 RubyGems 1.5.0 和 1.5.1 的一个bug,1.5.2 fix了这个issue。

Manual的Upgrade可以绕过 reinstall 的麻烦和繁琐,且更低碳环保:
Continue reading →

gem list

响应蛋疼之号召,gem list一下,不过估计有点outdated,因为本人最近好几个月没碰ruby了,不过anyway,show了再说:

$ gem list --local | wc -l
147

为了有点视觉效果,特地把vertical scrollbar给干掉了,xd:
Continue reading →

Ruby gem geoip and Rack::GeoIPCountry

wikipedia上对于geolocation的解释是这样的:

Geolocation is the identification of the real-world geographic location of an Internet-connected computer, mobile device, website visitor or other.

检测的方法貌似也有很多:

Geolocation can be performed by associating a geographic location with the Internet Protocol (IP) address, MAC address, RFID, hardware embedded article/production number, embedded software number (such as UUID, Exif/IPTC/XMP or modern steganography), invoice, Wi-Fi connection location, or device GPS coordinates, or other, perhaps self-disclosed information.

(班门弄斧一下先,呵呵呵呵)

ruby里面取到geo信息的一个相对方便的办法是使用rubygem geoip,虽然本质上还是使用maxmind的data,但是免去了编译和安装其c library的麻烦。
Continue reading →

List performance: ruby 1.8 and 1.9 (Part 2)

接上篇: List performance: ruby 1.8 and 1.9 (Part 1)

有了之前的准备和热身,真正跑起步来似乎轻松不少,先贴上我们用来benchmark的code:

require 'benchmark'

n = ARGV.empty? ? 10 ** 6 : 10 ** ARGV[0].to_i
puts "n=#{n}"
Benchmark.bmbm do |x|
  x.report do
    list = []
    n.times do
      list < < 0
    end
  end
end

之前用来benchmark loop performance的code结构上基本没有区别,这里不再赘述,具体的解释亦可参照前文。
Continue reading →

List performance: ruby 1.8 and 1.9 (Part 1)

(本文数据较多,在rss reader里面可能可读性不是很好,可考虑直接在线阅读)

昨天看到pipitu这篇blog,觉得蛮有意思,文章比较了python和java的list performance,只可惜没有include ruby 1.8和ruby 1.9,不然结论会更有悬念一点,呵呵.

由于无法营造一致的软硬件环境,也不愿意重复pipitu关于python和java部分的工作,这里就只好先简单benchmark(如果可以算是的话)一下ruby 1.8和1.9里的list (array) performance了,也许pipitu以后有空更新include ruby也说不定:)

先看一下我们用的ruby的版本:

$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9]
$ ruby1.9 -v
ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin9]

还有os:

$ uname -a
Darwin macbook.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386

Continue reading →