by agate - Published: 2009-07-19 [3:52 下午] - Category: 开发环境

今天在 Ubuntu上安装 Sinatra 后准备试试看是否成功, 但是在执行写好的 ruby 脚本的时候发现原来 Sinatra 是没有自带 Server 的. 需要安装 thin, mongrel, webrick 其中至少一个.(都是 rack 服务器好像)

由于 mongrel 和 webrick 都使用过了, 怀着好奇的心情今天试着装了一下 thin, 同时遇到了一些问题, 记录下来.

0. 不用说了 gem install thin

1. g++
我安装到一半就告诉我出错:

make: g++: Command not found
make: *** [rubymain.o] Error 127

我其实不清楚 g++ 和 gcc 的区别, 我觉得很奇怪, 一般不是都默认装了 gcc 么? 怎么安装的时候却找不到 g++ 呢? 怀着怀疑的态度我试着 apt 了一下 g++ 没想到真的有这么一个玩意. 只要安装好了就可以成功通过 gem 安装 thin

2. openssl
装完了当然是用用看咯. 我在命令行键入 thin 却被告知:

/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- openssl (LoadError)

可是 openssl 我的确装了啊, google 了一下原来还要装 libopenssl-ruby

DONE

Tags: [ , ] - Comments: Comments
by agate - Published: 2009-04-05 [5:13 下午] - Category: 开发环境

我一般不喜欢改动系统的总体配置, 所以一般我使用 gem 安装的时候都是不加 sudo 的. 于是我的大多数 gems 都是安装在 ~/.gem 下的(包括 rails). 今天装了 rails 2.3.2 似乎有点不爽(感觉). 于是想卸载. 但是在我执行:
$ gem uninstall rails
ERROR: While executing gem ... (Gem::InstallError)
  Unknown gem rails >= 0

弄了半天, 也查了是不是路径拼写错误. 还是没弄出来...
后来 google 了一下, 得知由于非默认安装位置(非 sudo 嘛). 所以需要指定 --install-dir 参数. 于是乎我就这么写了:
$ gem uninstall --install-dir ~/.gem/ruby/1.8
ERROR: While executing gem ... (Gem::InstallError)
  Unknown gem rails >= 0

还是一样噢... 无奈, 继续 google! 发现一个命令:
$ gem list -d rails
*** LOCAL GEMS ***
rails (2.3.2, 2.2.2)
  Author: David Heinemeier Hansson
  Rubyforge: http://rubyforge.org/projects/rails
  Homepage: http://www.rubyonrails.org
  Installed at (2.3.2): /home/agate/.gem/ruby/1.8
    (2.2.2): /home/agate/.gem/ruby/1.8

这样就十分方便地晓得了具体 gem 安装的路径了, 直接拷贝下来作为 --install-dir 参数即可.
$ gem uninstall --install-dir /home/agate/.gem/ruby/1.8 rails
Select gem to uninstall:
1. rails-2.2.2
2. rails-2.3.2
3. All versions
> 2
Successfully uninstalled rails-2.3.2

至此, 小问题解决.

PS. 你会说: 呀 "/home/agate/.gem/ruby/1.8" 和 "~/.gem/ruby/1.8" 不是一样么? 怎么第一次用 --install-dir ~/.gem/ruby/1.8 不行呢?
因为就是不行! 我也不知道, 他就是要绝对路径罢了...

Tags: [ , , ] - Comments: Comments
by agate - Published: 2009-04-05 [9:36 上午] - Category: 开发环境

今天在新的 Ubuntu 上面安装 rails 的时候碰到了这样的问题:

`require_frameworks': no such file to load -- net/https (RuntimeError)

原来是缺少 libopenssl-ruby library:
解决方案:
$sudo apt-get install libopenssl-ruby
本文作为 Ubuntu下部署rbuy on rails开发环境 补充

Tags: [ , , ] - Comments: Comments
by agate - Published: 2008-12-07 [8:18 上午] - Category: 开发环境

i feel uncomfortable while i using vim and having to move way up to reach Escape key. so i swaped the Escape and CapsLock keys by under steps:

first, open your terminal. and type:
$vim ~/.xmodmap

second, add under statements into ./xmodmap file.
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock

last, restart your X env.
(maybe you can use CTRL + ALT + BackSpace)

after these configurations, i found it's easier than move way up to reach Esc while using vim.

ps. thanks forrest, thanks leon! i found i fell in love with vim!!! it's really powerful!

Tags: [ , , ] - Comments: Comments
by agate - Published: 2008-11-22 [6:51 下午] - Category: 开发环境

最近工作一直有 WEB 前端的工作, 而且又在 Linux 下, 于是 Firefox 和 Firebug 是不可或缺的工具了! 这里不得不提起 Firebug! 这个东西过去只是当作 html 快速查看器和类似 IE 的 JavaScript 错误报警器来使用, 都不知道这个让众多 WEB DEVELOPER 所喜爱的开发辅助工具还有很多重要的特点和开发上的便利. 于是准备在这个日志里头记录下来:

1. console.log()
控制台日志, 可以在 Firebug 的 Console 标签页中打印出来.( 还被同事笑我这个 WIN 用户使用 alert 弹出消息调试... ) 这个功能有个很好的地方就是他不只是 print 出来一个 string 给你. 如果 log 的是一个 object 这类的东西, 你还可以用鼠标点击这条 log, 然后他会跳到 DOM 标签页给你跟踪这个 object 的具体数值.

2. 断点
这个是我怎么都没想到的... ( 可是我怎么就是没想到呢... ) 这个 Firebug 还可以在加载的 JS 中打上断点然后执行时可以 debug.

... updating ...

by agate - Published: 2008-11-07 [3:02 下午] - Category: 开发环境

奇怪... Ubuntu8.04 下我都没碰到这类问题, 倒是升级到 Ubuntu8.10 的时候 java 就出现乱码了~ 不说为什么, 因为我也不知道. 这里记录一下简单的解决方案:
1.
在 ${JRE_HOME}/lib/fonts/ 下建立个目录 fallback
比如我这儿就是
$cd /usr/lib/jvm/java-6-sun/jre/lib/fonts/
$mkdir fallback

2.
在 fallback 里弄个中文字体(拷贝或链接都可以)
比如我这就是
$ln -s /usr/share/fonts/truetype/arphic/uming.ttf /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/

3.
进入 ${JRE_HOME}/lib/fonts/fallback/ 执行 mkfontscale, 再把 jre/lib/fonts/fallback/fonts.scale 的内容加到 ${JRE_HOME}/lib/fonts/fonts.dir
我这儿就是
$cd /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/
$mkfontscale
$cd ..
$cat fallback/fonts.scale >> fonts.dir

DONE!

Tags: [ , , ] - Comments: Comments
by agate - Published: 2008-11-03 [12:32 下午] - Category: 开发环境

我是使用 Ubuntu 操作系统的, 其默认的终端编码方式是 UTF-8. 在我使用中文语言作为系统的语言环境后, 我在使用 VIM 自带的 Tutor 教程学习的时候就出现了乱码现象. 究其原因是因为默认中文的 Tutor 是以 GB2312 的编码方式进行编码的. 所以很简单的解决方式是:
$sudo cp /usr/share/vim/vim71/tutor/tutor.zh.euc /usr/share/vim/vim71/tutor/tutor.zh.euc_bak
$sudo gedit /usr/share/vim/vim71/tutor/tutor.zh.euc
另存为 --> 设置为 UTF-8 编码 --> 确认 --> Done

解决! 这里注意的是不同的操作系统可能 VIM 安装的路径以及版本不同, 具体的修改一下就好了!

Tags: [ ] - Comments: Comments
by agate - Published: 2008-10-21 [7:34 上午] - Category: 开发环境

怎么说呢~ 我还是比较喜欢视窗系统下的使用开发方式(具体原因是 Linux 和 Mac 对中文字体的渲染实在是不敢恭维). 于是目前我使用开发 web 程序的方式是使用 VMware, 在视窗系统下使用 PuTTY 和视窗共享来操作 Linux 系统, 并进行开发. 效果挺不错的. 本文书写的原因是 autotest 在远程调用时候比较不容易实现可视化的自动测试(出错有声音啊, 有红字啊~). 所以经过变通思考我决定采用 Firefox + Greasemonkey 这样的搭配来实现定时访问 autotest 生成的 html 结果, 从而在视窗系统下对远端 Linux 开发机进行自动化测试和提示.

先看看效果图:

你会问, autotest 生成的 html 并不能自动刷新啊, 这里我就想到了 Greasemonkey 这个强大的 Firefox 自定义扩展插件. 可以通过自己手动书写 JS 来扩展页面功能/样式.

于是我便写了一个简单的 JS 脚本, 提供 n 秒自动刷新, 有 failures 就发出报告声. 脚本如下:

// ==UserScript==
// @name           rspec results auto refresh
// @namespace      agatezone
// ==/UserScript==

// modify vars
var time_left = 10;
var path = "file:///E:/my_help_files/GreasemonkeyFiles/rspec_results_auto_refre";

// script begin ---------
// init
init();

// play error sound while has errors.
if (hasError()) {
	var sound = document.createElement('div');
	document.getElementById('reload').appendChild(sound);
	sound.innerHTML = "<embed name=\"pmsound2player\" src=\"" + path + "player.swf\" flashvars=\"sFile=" + path + "failure.mp3\" menu=\"false\" allowscriptaccess=\"sameDomain\" swliveconnect=\"true\" type=\"application/x-shockwave-flash\" width=\"0\" height=\"0\">";
}

// start timer
reloadTimer();

/////////////////////////////

// functions start ++++++++++++++++
function init() {
	var summary = document.getElementById('summary');
	var p = document.createElement('p');
	var t = document.createTextNode('');
	p.setAttribute('id', 'reload');
	p.appendChild(t);
	summary.appendChild(p);
}

function setTitle(x) {
	document.title = x;
	document.getElementById('reload').firstChild.nodeValue = x + "...";
}

function reloadTimer() {
	if (time_left <= 0) {
		setTitle('Reloading...');
		window.location.reload();
	} else {
		setTitle ('Reload in ' + time_left + ' second' + (time_left == 1 ? '' : 's'));
		setTimeout (reloadTimer, 1000);
		time_left --;
	}
}

function hasError() {
	var result_text = document.getElementById('totals').firstChild.nodeValue;
	var re = /(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/;
	var results = re.exec(result_text);
	if (results[2] != 0) {
		return true;
	} else {
		return false;
	}
}
// functions end ++++++++++++++++
// script end ---------

这里有两个文件 DOWNLOAD 主要作为错误提示音使用, 请解压开后放置到一个位置, 并且把这段代码里头的 path 修改为这两个文件存放的位置. 就比如我把这两个提示音文件放在:
c:\autotest_rspec\
下面, 那么 path 就改成:
file:///C:/autotest_rspec/
这样的网页地址形式.

这样便可以简单地在微软视窗操作系统下实现自动化测试远端开发机并给出结果提示.

灵感: Greasemonkey 太强大了! JavaScript 同样! Dom 模型更是了不起!

by agate - Published: 2008-10-17 [1:18 下午] - Category: 开发环境, 程序编码

羡不羡慕那些 rails 视频在 MAC OS X 下面帅气的自动跳出绿色和红色的自动测试提示呢? 当然如果你是 MAC 用户就不用羡慕了哈!(比如我就不羡慕嘿嘿) 但是大部分朋友都是 PC 呀! 没有 MAC 下的 Growl 作为 UI 提示工具怎么办呢? 没事啦! 这些我们都好解决, Ubuntu 下面我们可以用 rnotify 这个 ruby gnome 的消息提示工具. Windows 下我们可以用 ruby-snarl 这个消息提示工具啦! 当然还有 AutoIt 这类辅助工具! 这里我们先说 Ubuntu 这样的 GNOME 环境中的解决方案.

首先是安装 autotest, rspec, rspec_rails 三个插件
$sudo gem install autotest
$rails hello
$cd hello
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec
$./script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails
$./script/generate rspec

这样便安装好最基础的 BDD 环境了. 手动在你的 rails 目录下键入
$./script/autospec
这是这个自动测试不是在后台运行的, 因为他要显示信息嘛!
所以最小化这个终端窗口吧, 或者新开一个 Tab 来继续操纵你的 rails 应用.

就可以启动针对此 rails 应用的持续测试功能了. 让我们来试试看! 如何工作, 在终端中你的 rails 目录下键入:
$./script/generate rspec_scaffold post title:string
$rake db:migrate
$rake db:test:clone

看看你的那个自动测试窗口吧! (如果你的小机器不太快, 这个测试窗口还和原来一样, 那么稍等一会会, 他马上就运行咯!) 你大概会看到类似的结尾结果:

哈! 是不是很有成就感? (当时我满有的...) 呵呵, 但是这个只是在终端中看到的, 难不成要我们每次保存了新的东西都要光顾我们的终端信息么? Mac 下有 Growl 来做桌面 UI 气泡提示. 在 Ubuntu 这样的 GNOME 下我们可以使用 ruby-notify 来实现. 首先必须安装 ruby-gnome2 / libnotify-dev 和 ruby-libnotify 这些组件依赖. (其中 ruby-libnotify 需要手动安装)
$sudo apt-get install ruby-gnome2 libnotify-dev
以上安装好了 ruby-gnome2 和 libnotify-dev , 至于 ruby-libnotify 可以到其官方网站下载手动其源代码编译安装, 当前最新版本为 0.3.3, 在其下载页面我下好了 ruby-libnotify-0.3.3.tar.bz2 文件, 将其解压开好. 使用终端进入其目录中执行:
$ruby extconf.rb
$sudo make
$sudo make install

这样就安装好 ruby-libnotify 了! 其实就是用它来代替 Mac 下的 Growl 啦! 接着建立 ~/.autotest 文件并填入如下代码.

require 'rnotify'
require 'gtk2'

module Autotest::RNotify
  class Notification
    attr_accessor :verbose, :image_root, :tray_icon, :notification,
      :image_pass, :image_pending, :image_fail,
      :image_file_pass, :image_file_pending, :image_file_fail,
      :status_image_pass, :status_image_pending, :status_image_fail

    def initialize(timeout = 5000,
        image_root = "#{ENV['HOME']}/.autotest_images" ,
        verbose = false)
      self.verbose = verbose
      self.image_root = image_root
      self.image_file_pass = "#{image_root}/pass.png"
      self.image_file_pending = "#{image_root}/pending.png"
      self.image_file_fail = "#{image_root}/fail.png"

      raise("#{image_file_pass} not found") unless File.exists?(image_file_pass)
      raise("#{image_file_pending} not found") unless File.exists?(image_file_pending)
      raise("#{image_file_fail} not found") unless File.exists?(image_file_fail)

      puts 'Autotest Hook: loading Notify' if verbose
      Notify.init('Autotest') || raise('Failed to initialize Notify')

      puts 'Autotest Hook: initializing tray icon' if verbose
      self.tray_icon = Gtk::StatusIcon.new
      tray_icon.pixbuf = Gdk::Pixbuf.new(image_file_pending,22,22)
      tray_icon.tooltip = 'RSpec Autotest'

      puts 'Autotest Hook: Creating Notifier' if verbose
      self.notification = Notify::Notification.new('X', nil, nil, tray_icon)

      notification.timeout = timeout

      Thread.new { Gtk.main }
      sleep 1
      tray_icon.embedded? || raise('Failed to set up tray icon')
    end

    def notify(icon, tray, title, message)
      notification.update(title, message, nil)
      notification.pixbuf_icon = icon
      tray_icon.tooltip = "Last Result: #{message}"
      tray_icon.pixbuf = tray
      notification.show
    end

    def passed(title, message)
      self.image_pass ||= Gdk::Pixbuf.new(image_file_pass, 48, 48)
      self.status_image_pass ||= Gdk::Pixbuf.new(image_file_pass, 22, 22)
      notify(image_pass, status_image_pass, title, message)
    end

    def pending(title, message)
      self.image_pending ||= Gdk::Pixbuf.new(image_file_pending, 48, 48)
      self.status_image_pending ||= Gdk::Pixbuf.new(image_file_pending, 22, 22)
      notify(image_pending, status_image_pending, title, message)
    end

    def failed(title, message)
      self.image_fail ||= Gdk::Pixbuf.new(image_file_fail, 48, 48)
      self.status_image_fail ||= Gdk::Pixbuf.new(image_file_fail, 22, 22)
      notify(image_fail, status_image_fail, title, message)
    end

    def quit
      puts 'Autotest Hook: Shutting Down...' if verbose
      #Notify.uninit
      Gtk.main_quit
    end
  end

  Autotest.add_hook :initialize do |at|
    @notify = Notification.new
  end

  Autotest.add_hook :ran_command do |at|
    results = at.results.last

    unless results.nil?
      output = results[/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/]
      if output
        failures = $~[2].to_i
        pending = $~[4].to_i
      end

      if failures > 0
        @notify.failed("Tests Failed", output)
      elsif pending > 0
        @notify.pending("Tests Pending", output)
      else
        unless at.tainted
          @notify.passed("All Tests Passed", output)
        else
          @notify.passed("Tests Passed", output)
        end
      end
    end
  end

  Autotest.add_hook :quit do |at|
    @notify.quit
  end
end

然后呢, 这里有几个图片, 请你下载下来保存到 ~/.autotest_images 目录下(自己建立这个文件夹).

(注意哦! 文件名不要变哦! 分别为 pending.png pass.png fail.png)

打工告成! 你可以在 rails 工程目录里头执行:
$./script/autospec
不出意外的话你应该会看到如下结果

Tips:
1. ~/.autotest 这个文件可以定制 autotest 的运行.
2. ruby-libnotify 可以用于 GNOME 中作为消息提示.(注意哦! 用源代码编译安装哦!)

Tags: [ , , , , ] - Comments: Comments
by agate - Published: 2008-10-17 [10:00 上午] - Category: 开发环境

我原先的环境:

RUBYGEMS VERSION: 1.2.0
RAILS VERSION: 2.1.1

但是当我升级到 RUBYGEMS 1.3.0 的时候, 每当用命令行生成一些 rails 文件的时候就会报:

Gem::SourceIndex#search support for Regexp patterns is deprecated 2.1.1

十分不爽! 于是寻寻觅觅问题所在, 在鸟语网站上查了很多都是说是 RUBYGEMS 1.3.0 的问题, 只是一个提醒罢了, 无关痛痒的! 但是我看着就是十分不爽呀! 就想能不能降级到 RUBYGEMS 1.2.0 呢? 在 google 上找到了一个方法!
$sudo gem install rubygems-update -v 1.2.0
问题是我原来是从 RUBYGEMS 1.2.0 升级上来的, 于是我的系统里存在 RUBYGEMS 1.2.0 和 1.3.0 两个升级文件, 默认情况下执行 update_rubygems 会升级到最新版本(这不是我们期望的). 于是必须使用 gem uninstall 来卸最新的 1.3.0 rubygems.
$sudo gem uninstall rubygems-update
会列出一个列表选择需要卸载的序号后回车即可!

这样卸载之后再次执行:
$sudo update_rubygems
就可以降级回到原先的 RUBYGEMS 1.2.0 咯! 等到 rails 或者 rubygems 自己解决这个不爽的提示之后我们在升级吧!

Tips:
gem env
gem list

Tags: [ , , ] - Comments: Comments