道喜技术日记 .^. 天天红玉世界

Rails Ruby MacOSX 。。。创新来自于刻苦的实践和勤奋的思考... www.hhtong.com


苹果世界:获得CPU信息(两则)

Gudao Luo2011-07-23 at 11:33发表的

2011-07-23-macosx-info

苹果世界:如何快速重新命名文件名或文件夹名(两则)

Gudao Luo2011-07-16 at 11:14发表的

2011-07-16-macosx-tricks
  • 如何快速重新命名文件名或文件夹名?
    1. 选中所要修改的文件名或文件夹名;
    2. 按下回车键;
    3. 按向左键或向右键;
    4. 重新命名这文件名或文件夹名;
    5. 再次按下回车键;

Ruby代码世界:尽可能使用Ruby语言方法”<<“

Gudao Luo2009-01-19 at 09:50发表的

尽可能使用Ruby语言操作方法”<<“
  • 说明
    •  下面第一段代码使用了变量“i”。在Ruby代码中,基本上不需要使用这种变量,即使需要也可以使用“each_with_index”。而第一段代码连这种需要也没有。
    • 下面第二段代码是使用方法“<<”改进后代码清单。注意使用这种方法时,一定要清楚方法“<<”之后的对象是什么。在Rails 2.2.2版本中因为使用此方法不当,产生了错误。参见: Rails2.2世界:国际化功能问与答(二)
下面参考链接的部分代码
#rows
rows=[]
i=0
@issues.each do |issue|
rows[i] = [issue.id.to_s, issue.tracker.name, issue.status.name,
issue.priority.name, issue.assigned_to ? issue.assigned_to.name : '',
format_date(issue.updated_on),
@project == issue.project ? issue.subject : "#{issue.project.name} - #{issue.subject}"]
i=i+1
end
使用方法“<<”改进后代码清单
#rows
rows=[]
@issues.each do |issue|
rows << [issue.id.to_s, issue.tracker.name, issue.status.name,
issue.priority.name, issue.assigned_to ? issue.assigned_to.name : '',
format_date(issue.updated_on),
@project == issue.project ? issue.subject : "#{issue.project.name} - #{issue.subject}"]
end

Rails技巧系列:你使用过script/runner吗?

Gudao Luo2009-01-18 at 15:30发表的

你使用过script/runner吗?
  • 说明
    • Rails命令script/runner可以执行Rails软件Ruby文件或者Ruby代码。
    • 结合工具 crontab 可以实现Rails项目的定时任务的执行。

Rails技巧系列:代码redirect_to(@object)够用了吗

Gudao Luo2009-01-17 at 11:56发表的

代码redirect_to(@object)够用了吗
图片页面HTML代码简化清单
<div id="tabs">
<ul>
<li><a href="#tabs-1">公司</a></li>
<li><a href="#tabs-2">员工</a></li>
<li><a href="#tabs-3">集装箱</a></li>
</ul>
<div id="tabs-1">Company</div>
<div id="tabs-2">Employee</div>
<div id="tabs-3">Container</div>
</div>
  • 说明
    • 代码(一)是通过Rails命令scaffold自动生成代码,其中有一行代码“redirect_to @container”。这一行代码返回到页面,而不能够返回到该页面的某一个标签位置,如上图所示类似页面。如何办?
    • 代码(二)中代码“redirect_to container_path(@container, :anchor => “tabs-3”) ”就是解决这个问题的办法。
代码(一)
  # POST /containers
# POST /containers.xml
def create
@container = Container.new(params[:container]) respond_to do |format|
if @container.save
flash[:notice] = 'Container was successfully created.'
format.html { redirect_to @container) }
format.xml { render :xml => @container, :status => :created, :location => @container }
else
format.html { render :action => "new" }
format.xml { render :xml => @container.errors, :status => :unprocessable_entity }
end
end
end
代码(二)
  # POST /containers
# POST /containers.xml
def create
@container = Container.new(params[:container]) respond_to do |format|
if @container.save
flash[:notice] = 'Container was successfully created.'
format.html { redirect_to container_path(@container, :anchor => "tabs-3") }
format.xml { render :xml => @container, :status => :created, :location => @container }
else
format.html { render :action => "new" }
format.xml { render :xml => @container.errors, :status => :unprocessable_entity }
end
end
end

Rails技巧系列:为什么不要使用函数puts?

Gudao Luo2009-01-16 at 22:54发表的

为什么不要使用函数puts?
使用函数puts与函数logger.info的区别
  # GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
#函数puts
puts @user.to_yaml
# 函数logger.info
logger.info("#{Time.now} - #{@user.to_yaml}")
#
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
  • 说明
    • 很多人喜欢使用函数puts,获取调试信息。这个问题类似于Java语言人们使用System.out.println(),获取输出内容。一旦使用了函数puts,无法保证网络服务器对于源代码输出信息的顺序!参见上图作了标记的两行输出记录。尽管在上面代码中,函数puts和logger.info是一前一后,但是输出信息的顺序与代码的顺序是不同的。
    • 建议放弃使用函数puts,获取输出信息的方法。
    • Rails框架已经集成了logger软件包!
    • 什么是logger? What is logger?