Archive for the ‘technology’ Category »
Rails版本的avideo
大家都知道wordpress有个avideo插件用来让大家快捷贴各大视频网站的视频,在Rails里,我们可以这样实现。
创建一个Observer,内容如下(没办法发code,只能发个图片出来)

在config/environment.rb里加上启动observer的代码
config.active_record.observers = :video_observer
上面这代码就会在入数据库前将匹配视频快捷方式的代码转换为视频网站的代码。that all!
注:我测试时是过滤youku字段的,你如果要考虑body,应将相model.youku改为model.body
Gentoo当服务器的优劣
Gentoo作服务器的优点在于定制性极高,通过编译指定配置,少加载一些无谓的东西,性能可以很高。在国内还有douban这样的活招牌[豆瓣的 Web 服务器]。
那么为什么不选Gentoo作为服务器呢?
我个人认为Gentoo不是不好,不拿它当服务器是因为一般的用户的Linux技能要求比较高(punkid! ,Onion在这方面比较好)。并且作服务器,新手升级套件时碰到“地雷”挺难搞的,网上的性能极限配置例子罕有,自己摸索,成本较高。
服务器的性能不单是取决于选用何种操作系统,这涉及前端后端的设计,与数据库的规范化,使用的编程语言也会导致服务器性能的高低[Twitter Said To Be Abandoning Ruby on Rails]。如果为了追求性能而使用Gentoo,我想用这些时间来作相应的上述优化也能达到这个效果。但如果你想当一个纯正的Linux用户,Gentoo是一个绝对的选择。
相关讨论:
Why Gentoo Shouldn’t be on Your Server
Why Gentoo is a Great Server Distribution
我為什麼採用 Gentoo 當作我的系統.
rails2.0里使用scaffold
在Rails2.0中,scaffold从核心中移出,转变成插件,使用时,我们需要在所在项目里用下面的命令下载:
ruby script/plugin install scaffolding
配置好数据库之后,像往常一样用
ruby script/generate scaffold yourmodel [field:attribute field:attribute field:attribute]
然后
rake db:migrate
就可以像Rail1.2.3里使用了。代码变化相当大,我之前没用scaffold,现在用装上是为了看看Rails2中生成的scaffold是如何render渲染的,看了看,原来2.0中干脆不render了。
配置apache的端口vhost
注:我是在debian4.0 i386下测试通过的。
首先在用户的目录下创建存放程序的目录,放在public_html下,用域名作文件名更容易日后的管理。
cd ~
mkdir public_html
mkdir -p public_html/domain1.com/{public,private,logs,cgi-bin,backup}
创建测试文件
vim public_html/domain1.com/public/index.html
#请把"-"转换为相应的html符号 -html- -head- -title-domain1.com-/title- -/head- -body- -h1-domain1.com-/h1- -/body- -/html-
创建配置文件
sudo vim /etc/apache2/sites-available/domain1.com
NameVirtualHost *:8001 #特定的访问,不添加会报错
ServerName localhost
DirectoryIndex index.html
DocumentRoot /home/demo/public_html/domain1.com/public
ErrorLog /home/demo/public_html/domain1.com/logs/error.log
CustomLog /home/demo/public_html/domain1.com/logs/access.log combined
使配置文件生效,重新加载APACHE。
sudo a2ensite domain1.com
sudo /etc/init.d/apache2 reload
编辑ports.conf文件
vim /etc/apache2/ports.conf
添加
listen 8001
不要在Rails route使用符号
在Rails的路由里的控制器里使用符号(symbol)经常会导致一些莫名奇妙的出错,解决办法就是不用哦!
不要这样做:
map.connect '/pages/:slug',
:controller => 'pages',
:action => :show
这样比较好:
map.connect '/pages/:slug',
:controller => 'pages',
:action => 'show'
打造可复用的版权信息
通常我们会在网站或blog里放上自己的版权信息如:
© Copyright <%= current_year -%>. All Rights Reserved.
这个current_year辐助方法定义在application里的helper里。定义如下:
# add to application_helper.rb
module ApplicationHelper
def current_year
Time.now.strftime('%Y')
end
end
但这样,只能得到当年的时间,但下一年呢,再一下年呢,网站能赚钱了,运营十年,二十年,我们难道要每年都去修改代码?不,我们坚决不这样做,Rails就是don’t repeat yourself。我们来创建一个可复用的current_year方法吧。
def current_year
Time.now.year
end
def year_range( start_year = current_year )
[start_year, current_year].sort.uniq.join(’-')
end
将参数传给start_year变量,再将起始年和当年用join方法连接起来。如果不传递参数,则用current_year输出当年的年数。
使用
<%= year_range(2005) -%>
显示结果是 2005-2008。
这个helper已经被作者制作成gem了,可以从GitHub下载。
http://github.com/robbyrussell/year_after_year/
文章来源: DRY(a): Year After Year
go.6.cn是功能一个把长网址转为短网址进行跳转。其原理可以说是当用户插入数据时,查询数据库是否存在该网址,有则插入,没则新建并返回给该网址。
用Rails来实现这个功能,可以用find_or_create_by_来实现,代码如下。
def index
@ad = Ad.new
end
def save
ad = Ad.find_or_create_by_tu(params[:ad]['tu'])
flash[:notice] = ad.id
redirect_to(:action => ‘index’)
end
def page
@ad = Ad.find(params[:id])
if @ad.tu =~ /^http:\/\//
redirect_to(@ad.tu)
else
redirect_to(”http://#{@ad.tu}”)
end
end
end
view文件
<% if flash[:notice] -%>
127.0.0.1:3000/page/<%= flash[:notice] %>
<% end -%>
#把[和]去掉
Ad表结构
def self.up
create_table :ads do |t|
t.column :id, :int
t.column :tu, :string
end
add_index :ads, :tu, :unique =>true
#添加索引
end
很简陋的,仅供参考
typo的title调用
在typo里每个theme的layout/default.html.erb是这样来调用标题(title)的
<%=h page_title %>
而Page_title是一个helper方法,它在content_helper方法里定义如下:
def page_title
blog_name = this_blog.blog_name || "Typo"
if @page_title
# this is where the page title prefix (string) should go
(this_blog.title_prefix == 1 ? blog_name + " : " : '') + @page_title + (this_blog.title_prefix == 2 ? " : " + blog_name : '')
else
blog_name
end
end
是不是很奇怪Article控制器里使用
layout :theme_layout, :except => [:comment_preview, :trackback]
来指定模版,但调用Article_controller时却能使用Content_controller器的辅助方法呢?
是这样的,ContentController是Article_controller父类。你细心的话可以在Article_controller顶端看到这样一行代码
class ArticlesController < ContentController
还有两种调用title的基本方法
1、 实例变量
<%= @title || 'welcome' %>
2、 Content_for方法
在helper定义
def set_html_title(str="")
unless str.blank?
content_for :html_title do
"— #{str} "
end
end
end
layout里这样调用
<% set_html_title(@article.name) -%>
深入浅出Ruby的blocks, Procs和 methods
original from: understanding-ruby-blocks-procs-and-methods
author: eliben AT gmail.com
译者:g.zhen.ning
译注:此文章已得到eliben的同意翻译为中文,如果各位认位此文章有用,可以转载,译文的地址给不给出没所谓,但希望你能把原文的链接给出。
PS:此文我没校对,所以错译可能挺多,欢迎捉虫并进行修改。
简介
Rub借鉴了动态函数式编程的特性,如:闭包(closures)、高阶函数(high-order functions)、第一类函数(first-class functions),借此给程序员提供了一整套强大的特性。这些特性通过Ruby的代码块(code blocks)来实现。Proc对象和方法(都是对象—概念非常相近但有丝许差别。其实我对这个概念也感到非常困惑,极其难理解block,proc和method的分别,也不知道在哪种情况下用哪个来处理的效果比较好。另外,我虽也使用过Lisp和拥有多年Perl开发经验,但我不肯定Ruby的概念和其它编程语言的类似术语有什么关系,如Lisp的functions和Perl的subroutines。我阅读了大量新闻组的贴子后发现很多人都有这疑问,并且相当一部分新手被此折磨着。
在这篇文章中,我将展开我理解这些Ruby问题之旅,其中我参考了一些Ruby的书藉、文档和网站(comp.lang.ruby),我真诚地希望这篇文章能帮助到你。
Procs
我从Ruby文档中窥看到Procs被定义为:Proc对象是一组绑定了本地变量的代码块,,一旦绑定了,这代码可以在不同的上下文仍能访问这些变量。
一个实用的例子:
def gen_times(factor)
return Proc.new {|n| n*factor }
end
times3 = gen_times(3)
times5 = gen_times(5)
times3.call(12) #=> 36
times5.call(5) #=> 25
times3.call(times5.call(4)) #=> 60
Procs在Ruby里担任函数的角色。更准确的命名是函数对象,因为所有东西在Ruby里都是对象。如对象俗称-functors。Functor被定义为对象去invoked或者called,如果它是普通函数,通常这伴有相同的句法,这就是Proc。
从例子和上面的定义,显然Ruby的Procs可以作为closures。在Wikipedia,closure被定义为提交到词汇上正文变量的函数(function that refers to free variables in its lexical context)。注意它和Ruby定义的绑定到一组本地变量的代码块有多接近。
再论Procs
Procs在Ruby是first-class objects,因为他们可以在运行期(runtime)创建,储存在数据结构里,作为能数传递到其它函数并且作为其它函数的值返回。实际上,gen_times例子证明所有的这些例子,除了“作为参数传递到其它函数”,这个问题可以如下表述:
def foo (a, b)
a.call(b)
end
putser = Proc.new {|x| puts x}
foo(putser, 34)
这是一个创建Proc的简化符号-内核(kernel)方法lambda(不久将变为methods,但现在假设这kernel方法是全局函数的同族,这能在代码的任何地方被调用)[we’ll come to methods shortly, but for now assume that a Kernel method is something akin to a global function, which can be called from anywhere in the code]。使用lambda,之前例子的Proc对象创建能被重写为:
putser = lambda {|x| puts x}
实际上,lambda和Proc.new有两个微小分别。1、参数的检查,Ruby文档关于lambda的描述:除了当调用时Proc对象检查传递参数的个数。这个例子论证这一点:
lamb = lambda {|x, y| puts x + y}
pnew = Proc.new {|x, y| puts x + y}
# works fine, printing 6
pnew.call(2, 4, 11)
# throws an ArgumentError
lamb.call(2, 4, 11)
2、返回值的不同。Proc.new的返回值从封装(enclosing)方法(就如在block里返回一样,下面有详细的对比:)
def try_ret_procnew
ret = Proc.new { return "Baaam" }
ret.call
"This is not reached"
end
# prints "Baaam"
puts try_ret_procnew
当从lambda返回时更循从惯例,返回它的调用者。
def try_ret_lambda
ret = lambda { return "Baaam" }
ret.call
"This is printed"
end
# prints "This is printed"
puts try_ret_lambda
因此,我建议使用lambda来代替Proc.new,除非后者的行为严格要求(unless the behavior of the latter is strictly required)。两个参数的少一点意外(addition to being way cooler a whopping two characters shorter, its behavior is less surprising).
Methods
简单输出,method也是块代码,然后,它不像Procs,方法没有绑定到一组本地变量。正因为这样,他们绑定到一些对象并且可以访问它的实例变量。
class Boogy
def initialize
@dix = 15
end
def arbo
puts "#{@dix} ha\n"
end
end
# initializes an instance of Boogy
b = Boogy.new
# prints "15 ha"
b.arbo
有用的习惯用法是在方法发送消息。(A useful idiom when thinking about methods is sending messages.)。给接收者-拥有一些方法定义的对象,我们可以向它发送消息-通过调用这方法,是否有能数是可选的。在上面的例子,调用arbo是类似通过不带参数发送消息“arbo”。Ruby支持更直接发送习惯用法消息,通过包含send method在类object(object是Ruby类的父类)。所以下面这行代码都是arbo方法的调用:
# method/message name is given as a string
b.send("arbo")
# method/message name is given as a symbol
b.send(:arbo)
注意这方法能定义在“顶层”(top-level)区域,没有在任何类中。例子如下:
def say (something)
puts something
end
say "Hello"
这看来是“free-standing”,但不是。当方法如此被定义时,Ruby暗地里把它们挤进到Ojbect类中。但真的不要紧。and for all practical purposes say can be seen as an independent method. Which is,顺便说一下,一些编程语言(像C和Perl)他们称这为”function”。下面的Proc是,在许多地方有类似之处。
say = lambda {|something| puts something}
say.call("Hello")
# same effect
say["Hello"]
[]结构是Proc的上下文的同义字(The [] construct is a synonym to call in the context of Proc)
Methods,然而,它比procs更通用,支持更多重要Ruby特性,这我将在解释完什么是Block之后来阐述。
Blocks
Blocks和Procs的关系如此接近以致于许多新手为弄清他们的真正区别而头痛不已。我现在尝试用一些比喻来理解它(希望我的比喻不是那么差吧)。我感觉Block是未诞生的Procs(unborn Procs)。Blocks是其幼虫,Procs是成型后的昆虫。Block不是依靠自身生存-它准备的的代码是为了当它真正存活时而设的,并且只有当它绑定并且转换成Proc时,它才存活:
# a naked block can't live in Ruby
# this is a compilation error !
{puts "hello"}
# now it's alive, having been converted
# to a Proc !
pr = lambda {puts "hello"}
pr.call
就这样,这就是谜团所在,然后呢?不,不全是这样。Ruby的设计者,Matz留意到当传递Procs到methods(传递到其它Procs)很不错并且允许高阶函数和所有奇异功能的东西(stuff),有一个普遍案例比其他案例要重要—-传递一个单独的代码块到一个方法,这方法能从中做出有用的东西,例如迭代(iteration)。并且作为一个富有才干的设计者,Matz认为值得花时间去使这个特别案例(special case)变得更简单,更高效。
Passing a block to a method
无需怀疑的哪些至少花费过数小时在Ruby的程序员已经写出下面这些Ruby荣耀的例子(或者非常类似的代码):
10.times do |i|
print "#{i} "
end
numbers = [1, 2, 5, 6, 9, 21]
numbers.each do |x|
puts “#{x} is ” + (x >= 3 ? “many” : “few”)
end
squares = numbers.map {|x| x * x}
(注意:do |x| … end和{ |x| …} )
这些代码就是IMHO,Ruby的整洁、便于阅读、神奇、部分原因就在于此。幕后的情形非常简单,或者绝对可以用这种简单的方式来说明。或许Ruby并没有以我所描述的方式来实现,因为还存在其它有效的优化措施,但是这样的比方显然足够接近事实真相。
只要method调用时附加了block,Ruby会自动把此block转换为一个没有明确命名的Proc对象。不过,此method可以通过yield语句来访问该Proc对象。看下面这个例子来说明这一切。
def do_twice
yield
yield
end
do_twice {puts "Hola"}
这方法do_twice定义并且附加block调用。尽管这方法没有明确请求block到参数列中,但yield能调用此block。这也可以必为更明确的方法,使用Proc参数:
def do_twice(what)
what.call
what.call
end
do_twice lambda {puts "Hola"}
这和先前的例子是等同的,但使用yield来调用blocks更简洁并且当只有block传递到block是最优化的,想要明确的话,使用Proc的方法吧。下面这例子就是用参数来传递的:
def do_twice(what1, what2, what3)
2.times do
what1.call
what2.call
what3.call
end
end
do_twice( lambda {print "Hola, "},
lambda {print "querido "},
lambda {print "amigo\n"})
这对那些讨厌通过block传递,而用Procs来代替的朋友来说是很重要的例子。这基本原理:block参数是固定的,它会遍历(look through)整个方法来查看是否有calls在这里调用,当Proc明确立刻在参数列里发现。虽然这只是个人品味取向,但明白这二者的方法是有必要的。
&符号的使用 Read more »
Typo5.0.3利用Migrator.migrate进行初始化表
if RAILS_ENV != 'test'
begin
ActiveRecord::Base.connection.select_all("select * from sessions")
rescue
Migrator.migrate
end
end
config下的environment.rb文件里增加了上面这代码来实现第一次运行project创建表,不需要像以前安装要rake db:migrate。
配置数据库->script/server就完成了安装,有利于typo的大众化使用。
不过 robinlu提醒说这样启动多个project时可能会有问题。

