Archive for February, 2010

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 →

关于Mac的Safe Sleep和Hibernate

据说Apple早在2005年10月发布Mac OS X 10.4.3的时候就已经加入了safe sleep这个功能,但是GUI界面上却至今始终对用户透明,用户在不装任何3rd party tools的情况下永远只能看到sleep而没有hiberate之类的选项。

但是此sleep又并非完全等同于我们平时常说的”suspend to RAM”(2005.10之前可能相同),它背后其实也做了”suspend to disk”,也就是PC上的hibernate的工作,实为明修栈道,暗渡陈仓。

这样做的好处也成了后来Apple命名此技术的offical name —— “safe sleep”,因为它解决了单纯”suspend to RAM”下电源耗完或故障导致的RAM信息丢失问题:其会在电源不足的情况下自动切换到”suspend to disk”,着实”smart and safe”。

但有趣的是,苹果似乎从来没有高调宣布过这项当时可能不新不revolutionary但确实对用户而言很实用的功能,for some “unknown” reason。

在大多数的情况下,用户可能并不会察觉这背后的不同,事实上我觉得Apple要不是考虑到了切换到”suspend to disk”后restore回来用户会等很久的user experience问题,而加入了这个progress bar, 很少真的会有用户察觉。

到了10.5,safe sleep似乎终于有了offcial的解释,但是总感觉好像简单了点,不知道你和我是否感觉一样,低调么也不带这样的…
Continue reading →

未成年先别優

今天偶然看到的 —— 好孩子的軟體設計心得(機車版),看着标题和表达习惯的第一感觉是貌似可能在墙外,不过还没验证过,如果是的话大家know what to do。

话说台湾人翻译起来有时还真的蛮有意思的,意思老有双关甚至三关,关键在于你的想象力,呵呵,摘录一句本来不好笑的经典名言但我看着就是老想笑的:

未成年就這麼優,是一切邪惡的根源
Premature optimization is the root of all evil

看完这句,你又想到了什么evil?

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 →

Update-29

1. Again, Back to Normal -> abnormal

2. 不管normal还是abnormal,牙痛还是牙肿,life还是要goes on,update还要不断,虽然精彩常断……

3. 春节回家亚克西,除夕春晚亚克西,holiday week亚克西,全靠党的政策亚克西;什么亚克西,什么亚克西啊,党中央的政策亚克西

4. 手机号码换了,这里也不怕再啰嗦一遍的…… 至于获得详情的方法这里不再啰嗦,不是什么都可以啰嗦的,看官需要培养好眼力,鄙人则需要锻炼好听力…… 经验和实践同时表明,换号码的成本不小,以后3G慢慢普及,移动TD众望所归杯具了之后,应该已经可以携号转网了吧,我可不想再折腾了……

5. crasher, which means uninvited guest, you don’t like it, i don’t like it, but if some day somehow i happens to be that beastly person, i wish it only resides in computer security category.

6. I like bread because bread TALK but people usually don’t.

ruby’s “python -m SimpleHTTPServer”

If I remembered right, the command below used to be the most voted one on commandlinefu.com:

$ python -m SimpleHTTPServer

the “SimpleHTTPServer” module is written in python and it maybe called “the python way”, but that’s not our topic today. what we really want to do here is just making a ruby equivalent for this popular command, using rack.

first, make sure you have rack gem installed on your system (if you’ve installed rails 2.x before, you may already have it):

$ sudo gem install rack

confirm installation (Rack 1.0 should also work for our example):

$ rackup --version
Rack 1.1

alright, preparation is done, let’s start cooking. only a rackup file will be sufficient, for consistency, let’s just call it simple_http_server.ru .

#!/usr/bin/env rackup
#\ -E deployment

use Rack::ContentLength

app = Rack::Directory.new Dir.pwd
run app

Continue reading →

pvz on iphone/itouch, now is the time

pvz on itunes

ps: jailbreak == DO NOT upgrade at the first time, be careful when itunes prompts you with a new firmware version (3.1.3) available dialog.

All right

alright, all right…

happy chinese new year and happy valentine’s day~

although they don’t have chinese new year, they do have the 2nd one, hehe, so… well, not bad news at least, xd

btw, what’s more important to them, work or love? hard to tell? alright, all right, let’s see~