生活就是泪奔,哥特就是锅贴

2011年9月21日星期三

ActiveSupport中的class_attribute

看rails guide的时候遇到了Class#class_attribute,怎么使用参见guide和下面两篇文章,这里不做介绍
http://blog.obiefernandez.com/content/2010/04/tr3w-highlights-activesupport-class-class-attribute.html
http://ihower.tw/blog/archives/4878

在看源码的时候,发现第79行怎么都不能理解,为什么是
singleton_class.#{name}
而不是
self.class.#{name}
呢?

由于我看的是3.0.7的版本,于是又找到了最新的3.1.0的版本
看完之后更加迷惑,为什么要定义两个reader instance method,一个用singleton_class,一个用self.class呢?

查阅了相关资料,翻看了源码提交记录,我找到了答案。
其实本来在3.0.7版本中用self.class代替singleton_class是可以的,但为了支持在singleton_class上调用writer方法
klass = Class.new { class_attribute :setting }
object = klass.new
object.singleton_class.setting = "foo"
所以用了singleton_class。

由于object的singleton_class的superclass是klass,singleton_class自己又没定义过setting=(),所以singleton_class.setting=()还是会查找到klass里的定义并调用它。

实际使用的时候,大部分情况还是object.setting=()而不是object.singleton_class.setting=(),为每个object创建singleton_class开销较大,所以做了优化:object调用时直接查找self.class,避开了singleton_class。而针对singleton_class另做处理,由于方法查找时singleton_class优先于klass,所以
def #{name}
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
end
会覆盖
def #{name}
defined?(@#{name}) ? @#{name} : self.class.#{name}
end

最后的问题是,谁引入的对singleton_class的支持呢?翻提交记录的时候找到了答案

2010年3月14日星期日

2009年6月30日星期二

irb auto complete

在~/.irbrc中加入:

require 'irb/completion'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]

2009年6月15日星期一

2 issues of VirtualBox on Ubuntu 8.04

issue: "VirtualBox No suitable module for running kernel found"
solution:

sudo apt-get install virtualbox-ose-source
sudo m-a update
sudo m-a prepare
sudo m-a a-i virtualbox-ose
sudo /etc/init.d/vboxdrv restart


issue: "VirtualBox Kernel driver not install"
solution:

sudo /etc/init.d/vboxdrv start
sudo chmod 666 /dev/vboxdrv

2009年5月21日星期四

Vimperator

Vimperator是Firefox的一个addon,可以像使用Vim一样来使用浏览器。

以下是一些命令的备忘

CTRL-Z
Disable all Vimperator keys except <Esc> and pass them to the next event handler.
If you also need to pass <Esc> in this mode to the web page, prepend it with <C-v>.

CTRL-V
If you only need to pass a single key to a JavaScript form field or another extension prefix the key with <C-v>.
When Vimperator mode is temporarily disabled with <C-z>, <C-v> will pass the next key to Vimperator instead of the web page.

Opened with the specified search engine if the token looks like a search string and the first word is the name of a search engine.
The short name of a search engine is automatically guessed from its name. If you want to set a custom name, you can change it with :dialog searchengines.

T Works like t but preselects current URL in the :tabopen query.

:tabd[uplicate]
Duplicates current tab.

:wino[pen]
Just like :tabopen but opens the resulting web page(s) in a new window.

p
Open (put) a URL based on the current clipboard contents in the current buffer.

P
Open (put) a URL based on the current clipboard contents in a new buffer. Works like p but opens a new tab.

:ju[mps]
List all jumps aka current tab's history aka session history.

r
Force reloading of the current page.

R
Force reloading of the current page skipping the cache.

<C-c>
Stop loading the current web page.

ZQ
Quit and don't save the session.

ZZ
Quit and save the session.

g<C-g>
Print file information.

[count]gi
Focus last used input field. If there is no last input field, it focuses the first input field. When used with [count] it directly jumps to the [count]th input field.

zz
Set full zoom value of current web page. Zoom value can be between 30 and 300%. If it is omitted, full zoom is reset to 100%.

/{pattern}[/]<CR>
If "\c" appears anywhere in the pattern the whole pattern is handled as though 'ignorecase' is on. "\C" forces case-sensitive matching for the whole pattern.

:noh[lsearch]
Remove the search highlighting.

:buffers [filter]
B
Show a list of buffers (=tabs) matching [filter]. Without [filter] list all tabs.

:tab {cmd}
Execute {cmd} and tell it to output in a new tab. Works only for commands that support it, currently:
:tab help
:tab prefs[!]
:tab addons
:tab downloads

g0
Switch to the first tab.

g$
Switch to the last tab.

u
Undo closing of a tab.

ExtendedHint mode
{mode} can be one of:
;; to focus a link and hover it with the mouse
;? to show information about the element (incomplete)
;s to save its destination
;a to save its destination (prompting for save location)
;f to focus a frame
;F to follow a sequence of <CR>-delimited hints in background tabs
;y to yank its destination location
;Y to yank its text description

:bma[rk]
Add a bookmark.

:bmarks [filter]
List or open multiple bookmarks. Open the message window at the bottom of the screen with all bookmarks which match [filter] either in the title or URL.

m{a-zA-Z}
Set mark at the cursor position. Marks a-z are local to the buffer, whereas A-Z are valid between buffers.

'{a-zA-Z}
Jump to the mark in the current buffer. Marks a-z are local to the buffer, whereas A-Z are valid between buffers.

:marks
Show all location marks of the current web page.

q{0-9a-zA-Z}
Record a key sequence into a macro. Available macros are {0-9a-zA-Z} (uppercase to append). Type q to stop recording.

:mac[ros] [pat]
List recorded macros matching the optional regular expression [pat]. If no regexp is given, list all macros.

:dialog printpreview

<C-a>
Increment last number in URL

<C-x>
Decrement last number in URL

0
Scroll to the absolute left of the document

$
Scroll to the absolute right of the document

gU
Go to the root of the website

:messages
Display previously given messages

:restart
Force the browser to restart

:se[t] all
Show all options.

2009年3月29日星期日

fastri

sudo gem install fastri
fastri-server -b


然后就可以

qri string

如果显示大于一屏的话

qri string|less -r