不可以在 /news/new 的 view 裡面寫 form_for @news ~"~
2008年7月12日 星期六
2008年6月3日 星期二
operate existed tables in rails
Just need specify table name and primary key in Model. All attribution in tables will appear automatically.
class ModelName < ActiveRecord::Base
self.table_name = "table_name"
self.primary_key = "primary_key"
end
標籤:
programming,
rails,
ruby
2008年5月24日 星期六
tree implement in ruby
Just for fun, but it work well.
01 #!/usr/bin/env ruby
02 #
03 #
04
05
06 def tree(dirname, indent = " |")
07 puts dirname + "/" if indent == " |"
08 dir = Dir.open dirname
09 for f in dir
10 if f == "." || f == ".."
11 next
12 elsif File.directory? dirname+"/"+f
13 puts indent + "-" + f + "/"
14 tree( dirname+"/"+f, indent+" |")
15 else
16 puts indent + "-" + f
17 end
18 end
19 end
20
21 tree (ARGV[0]? ARGV[0] : ".")
標籤:
programming,
ruby
2008年5月23日 星期五
closure tip
closure is so fun.
well design for meta programming.
note 1:
pass yield values.
note 2:
Every object in array call the same function.
well design for meta programming.
note 1:
01 #!/usr/bin/env ruby
02
03 class Hash
04 def find_all
05 temp_hash = {}
06 each {|key,value| temp_hash[key] = value if yield(key,value) }
07 temp_hash
08 end
09 end
10
11 square = { 0 => 0, 1 => 1, 2 => 4, 3 => 9 }
12
13 new = square.find_all { |key,value| key > 1 }
14 puts new.inspect
pass yield values.
note 2:
01 #!/usr/bin/env ruby
02
03 class Person
04 attr_accessor :name
05 def initialize(name)
06 @name = name
07 end
08 end
09
10 def command(obj,method)
11 obj.each {|x| yield x.send(method)}
12 end
13
14 family = []
15
16 %w(shelling sherry appollo).each do |name|
17 family.push Person.new(name)
18 end
19
20 command family, :name do |x|
21 puts "hello, I am #{x}."
22 end
Every object in array call the same function.
標籤:
programming,
ruby
2008年5月17日 星期六
sample scheme
hello, world! here
define function
link1: The Scheme Programming Language 3/e
link2: Scheme Tutorial
#!/usr/bin/guile -s
!#
(write "hello, world!")define function
(define square (lambda (n) (* n n) ) )
(write (square 3) ) ; output 9link1: The Scheme Programming Language 3/e
link2: Scheme Tutorial
標籤:
programming,
scheme
2008年5月12日 星期一
self.pm
some convenient usage I prefer. :p
self.pm++
package PackageName;
use self;
use strict;
my @args;
sub new {
@args = args;
for (@args) {
blah~!
}
bless {}, self;
}
1;
self.pm++
標籤:
perl,
programming
2008年4月26日 星期六
learning perl review ch2

scalar:
1.233, 2E-3, 3e45
# exponential float
123_123_234
# integer with underscore
0377, 0xff, 0b1111
# octal => 0, hexadecimal => 0x, binary => 0b
"hello,"."world!"
# concatenate with .
"hello" x 3
# repeat 3 time with x
"20fred" * 3
# ignore string when transform
$str = $str . "blah"; === $str .= "blah";
$int = $int ** 3; === $int **= 3;
# binary assignment operator
${word}blah~
# insert variable with dereference brace
eq == | ne != | < lt | > gt | <= le | >= ge
# string comparison operator
# return with \n at line end
defined($var)
# return true or false
initial parameters (perl -M)
use warnings;
use diagnostics;
標籤:
note,
perl,
programming
2008年4月25日 星期五
boring comparison
show complex data structure in perl and ruby
perl
ruby
perl
#!/usr/bin/env perl
use warnings;
use Data::Dumper;
$a = ["a", "b", "c", { "key" => "value" } ];
print Dumper $a;ruby
#!/usr/bin/env ruby
a = ["a", "b", "c", { "key" => "value" } ]
puts a.inspect
標籤:
perl,
programming,
ruby
2008年4月19日 星期六
has_many() function note
if has_many :order
order.delete_all just set foreign key null
order.destroy_all clear all sub record
order.delete_all just set foreign key null
order.destroy_all clear all sub record
標籤:
programming,
rails,
ruby
2008年4月17日 星期四
rss feed in rails
Note for the Way to create Atom and RSS 2.0 feed.
Suppose that the model we want show in feed is Post
Then write the view.
app/view/feed/rss.rxml
app/view/feed/atom.rxml
app/view/layout/application.rhtml add
$ ./script/generate controller feedSuppose that the model we want show in feed is Post
class FeedController < ApplicationController
def rss
@rss = @@post
end
def atom
@atom = @@post
end
private
@@post = Post.find :all,
:order => "update_at", :limit => 10
endThen write the view.
app/view/feed/rss.rxml
01 xml.instruct! :xml, :version=>"1.0"
02 xml.rss(:version=>"2.0"){
03 xml.channel{
04 xml.title("The site title")
05 xml.link(url_for("/"))
06 xml.description("The site description")
07 xml.language('en-us')
08
09 @rss.each do |rss|
10 xml.item {
11 xml.title rss.title
12 xml.link ""
13 xml.description rss.content
14 xml.pubDate rss.update_at
15 xml.guid ""
16 xml.author rss.declarer
17 }
18 end
19 }
20 }app/view/feed/atom.rxml
01 xml.instruct! :xml, :version=>"1.0"
02 xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
03 xml.title("The site title")
04 xml.link(url_for "/")
05 @atom.each do |atom|
06 xml.entry {
07 xml.title(atom.title)
08 xml.summary(atom.content)
09 }
10 end
11 endapp/view/layout/application.rhtml add
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'rss' }, { :title => "RSS 2.0" } %>
<%= auto_discovery_link_tag :rss, { :controller => '/rss', :action => 'atom' }, { :title => "Atom" } %>
標籤:
programming,
rails,
ruby
2008年4月15日 星期二
rails helper block
Write for memo.
To set a helper in the form
<% helper_name do %>
<div>blah</div>
<% end %>.
The definition is
By the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
To set a helper in the form
<% helper_name do %>
<div>blah</div>
<% end %>.
The definition is
def helper_name(&block)
content = capture(&block)
concat("what we want write before block".block.binding)
concat(content,block.binding)
concat("what we want write after block",block.binding)
endBy the way, partial is the way do the same thing once. if the outer html is used frequently, the way to define a helper as above will be better.
標籤:
programming,
rails,
ruby
2008年2月17日 星期日
the end of winter recess
這個寒假比預期的廢了很多,預定的事項,想讀的書,沒一件做完的,再來,三月一日就是研究所考試了,看著別人即將踏入考場,自己摸著鼻子甸甸自己剩下不到一年的時間儲備,真是難以說話。雖然沒有完成預定的事項,不過練習 perl/ruby 之餘還是寫了些 bot,cwbbot 是個簡單的 ruby 天氣預報機器人,提供他 twitter 的帳號和密碼,丟進 crontab,就可以每天自動取得明日溫度。
2008年2月15日 星期五
land mine in file name extension of ruby
和 perl 不同, ruby 的 third party module package, 也就是 gems, 副檔名和一般 script 都一樣是 ".rb", ruby 搜尋 module 的 path 似乎又包含了 ".", 所以如果使用的 script 檔名和 script 引用的 gems 內的檔名相同的話, 就會衝突 :/
標籤:
programming,
ruby
2007年12月29日 星期六
gem update on debian
日前升級了 debian 上的 gem,隨即發生無法使用的狀況,檢查之後發現,debian 內建的 rubygems 套件安裝位置在 /var/lib/gems/,而gem 原生的安裝位置在 /usr/lib/ruby/gems/,因此升級後會發生找不到正確 gems 的狀況。
因為 gems 的目錄配置方式不符合 FHS,debian 在 3.x 就一直遲遲不發佈 rubygems 套件,即使目前 4.x 發佈了,在目錄的相容性上問題也很多。
目前看來要安裝 gems 又不用通過官方安裝包條件的最好方法就是,先安裝內建的 rubygems,執行 gem update --system 之後,會得到一個 /usr/bin/gem1.8,然後移除 rubygems 和 libgems-ruby1.8,最後將 /usr/bin/gem1.8 重新命名成 /usr/bin/gem,就可以得到原生的 gem。
因為 gems 的目錄配置方式不符合 FHS,debian 在 3.x 就一直遲遲不發佈 rubygems 套件,即使目前 4.x 發佈了,在目錄的相容性上問題也很多。
目前看來要安裝 gems 又不用通過官方安裝包條件的最好方法就是,先安裝內建的 rubygems,執行 gem update --system 之後,會得到一個 /usr/bin/gem1.8,然後移除 rubygems 和 libgems-ruby1.8,最後將 /usr/bin/gem1.8 重新命名成 /usr/bin/gem,就可以得到原生的 gem。
2007年12月23日 星期日
jifty screencast
今天發現一個還不錯的 jifty 教學影片,位在
http://www.crium.univ-metz.fr/docs/devel/jifty/screencast.html
感覺上和 rails demo movie 差不多,晚點再來看看,不然 CPAN 上的 tutorial 實在不是我小小弱者看得懂的阿XD。
OS: CPAN 上的 tutorial 連基本的 MVC 架構都沒解釋清楚,實在也太把大家都當作強者了 Orz...
http://www.crium.univ-metz.fr/docs/devel/jifty/screencast.html
感覺上和 rails demo movie 差不多,晚點再來看看,不然 CPAN 上的 tutorial 實在不是我小小弱者看得懂的阿XD。
OS: CPAN 上的 tutorial 連基本的 MVC 架構都沒解釋清楚,實在也太把大家都當作強者了 Orz...
2007年12月6日 星期四
CGI::UploadEasy
今天研究了一下 CGI::UploadEasy 模組,這是 CGI 上傳模組中最簡單的一個,只要在網頁內設定好上傳檔案用的 input 欄位,然後最後加上一行
這樣會自動判斷有多少個檔案要上傳,還有上傳檔案的 mime-type,最後可以用 $upload 物件內的方法來取得上傳檔案的參數。
不過這個模組不是很適合我目前希望的方式,他能夠指定的參數只有目錄,中文也有問題,另外兩個 CGI::Upload 和 CGI::Uploader 模組則是功能太複雜了,過兩天有時間的話自己動手作一個上傳模組好了。
my $upload = CGI::UploadEasy->new(-uploaddir=>"上傳資料夾",-maxsize=>"負數視為不設限,單位為KB",-tempdir=>"暫存資料夾");
這樣會自動判斷有多少個檔案要上傳,還有上傳檔案的 mime-type,最後可以用 $upload 物件內的方法來取得上傳檔案的參數。
不過這個模組不是很適合我目前希望的方式,他能夠指定的參數只有目錄,中文也有問題,另外兩個 CGI::Upload 和 CGI::Uploader 模組則是功能太複雜了,過兩天有時間的話自己動手作一個上傳模組好了。
標籤:
perl,
programming
2007年11月27日 星期二
example ruby
今天在我的 Debian 上面裝了 ruby 來嚐鮮,感覺相當有趣,就如同作者所希望的 ruby 這顆紅寶石給人輕快簡單的感覺,相較於 perl 這個穩重的珍珠,各有所擅。
要建制一個 Rail application 的環境需要下的指令有
這還會自動安裝 libreadline 等等 lib
其中
然後再下
就可安裝 Rails,不過還未包含 Mongrel 伺服器,等等再研究須不需要現在安裝
要建制一個 Rail application 的環境需要下的指令有
sudo aptitude install ruby ri rdoc irb rubygem
這還會自動安裝 libreadline 等等 lib
其中
- ri 可以用來查詢 module
- irb 是 interactive ruby
- rubygem 是 ruby 的 package management framework,安裝完後有 gem 指令可用
- rdoc 可以用來生成 doc
然後再下
sudo gem install rails --include-dependencies
就可安裝 Rails,不過還未包含 Mongrel 伺服器,等等再研究須不需要現在安裝
2007年7月27日 星期五
萬惡的 perl GD 和 Authen::Captcha module
原來是需要 libgd2-xpm-dev 這個套件,快記下來免得忘記了
標籤:
debian,
perl,
programming
2007年7月19日 星期四
install svk on debian
如果要從 cpan 安裝的話會發現缺少 SVN::Core,但並不是 cpan 裡面搜尋到的那個 SVN::Core,而是要安裝附屬於 SVN 的 library,在 debian 裡面分別是 libsvn-perl,libsvn-simple-perl,libsvn-mirror-perl,用 aptitude install 安裝三個套件後在進入 cpan command line 下 install SVK,就會順利完成安裝。
也可以用套件安裝,會簡單很多,一行 aptitude install svk 就結束了。不過我在用 cpan 安裝完後才發現有這個套件,來不及了:p,不過從 cpan 裝的好處是更新快,不用等 debian打包新的套件,也是不錯的選擇。
以目前 aptitude show svk 的內容來看該套件只有到 1.08-2 版,cpan 上的則已經到 2.0.0.1,如果兩個版號是相對應的,那麼 cpan 上的版本看來快很多了:)。
也可以用套件安裝,會簡單很多,一行 aptitude install svk 就結束了。不過我在用 cpan 安裝完後才發現有這個套件,來不及了:p,不過從 cpan 裝的好處是更新快,不用等 debian打包新的套件,也是不錯的選擇。
以目前 aptitude show svk 的內容來看該套件只有到 1.08-2 版,cpan 上的則已經到 2.0.0.1,如果兩個版號是相對應的,那麼 cpan 上的版本看來快很多了:)。
2007年7月18日 星期三
become CAPN maintainer
Gugod 幫我在 pause.perl.org 申請了一個帳號,就這樣變成寥寥可數的台灣 cpan maintainer ,真是科科。
標籤:
perl,
programming
訂閱:
文章 (Atom)

