5个使用Rails控制台的有用技巧

update: 2011/10/26

1. 只想测试不想,真修改数据库
ruby script\console --sandbox
Loading development environment in sandbox.
Any modifications you make will be rolled back on exit.
>>

2. reload! 代码修改后的更新

3. 测试controller的返回


app.https!
#Then to get a URL you can use the simple get method:

app.get('/my/path')
#If you want to put or post to a URL there are also methods for that. You can copy/paste the parameters exactly as they are displayed in your Rails production log:

app.post('/foo', {"this" => "that", "items" => ["bar", "baz"]})
app.put('/foo', {"this" => "that", "items" => ["bar", "baz"]})
#If you want to sent a custom header, you can add an optional third parameter:

app.post('/foo', {:this => "that", :items => ["bar", "baz"]}, {"X-Do-Something" => "yes"})



对于Rails控制台的使用,往往对于调试开发过程是相当重要的.对于还不知道rails控制台的读者而言也不要紧,你只要在项目的根目录输入
ruby script/console
并看到类似irb的场景就可以使用了,不同的是这个控制台还加载了Rails的运行时态环境,例如插件.

下面的介绍是大部分开发人员不太熟悉却十分有用的控制台技巧.

一. 下划线方法 _

下划线返回上一条命令的返回值.如果,你刚好查看了一个表达式的值,那么,你可以使用_方法,取得这个表达式的值,并赋值给一个变量.下划线方法的使用如下:

>> City.all
=> [#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">]


这时如果你忘了,把这个输出,赋值给你的一个变量.那么不要紧:

>> c = _
=> [#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">]


要注意的是,如果,这个值没有被"_"捕获,将在下一条新表达式的执行下被覆盖.

二. y 方法

实际上这个方法只是Kernel的y 方法.然而,它在这里却十分有用,特别是你要在irb的会话理,打印出一些符合格式输出的表达式的时候.比如YAML文件.使用如下:

>> { :foo => 'bar', :woo => { :ooo => 1 } }
=> {:foo=>"bar", :woo=>{:ooo=>1}}
>> y _---
:foo: bar
:woo:
:ooo: 1
=> nil


这个方法特别对于输出大容量的collectionRails Models时特别有用.

三. pp库

和上面的y方法类似,pp也是一个相当好的用于打印输出的库文件.如果你想要输出一个很复杂的collections数据,你只需要在你的irb会话理注明require pp.然后,你就可以看到它出众的效果了:

>> require 'pp'
=> true
>> pp City.all
[#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">,
#<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">,
#<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">]
=> nil


如果,你希望能够更容易的使用,接着看下面:

四 .irbrc

对于默认的linux和unix操作系统而言,.irbrc文件存放在用户的$HOME目录下,用来预先加载需要在irb里面使用的文件.在这里我们同样可以使用,当然,在Windows下的用户需要自己创建这个irbrc文件并且用IRBRC变量在evironment的中标识.

你可以在这个文件中放置任何你需要加载的ruby命令,示例如下:

require 'rubygems'
require 'irb/completion'
require 'pp'
require 'yaml'

def gvim(x)
IO.popen( 'gvim -', 'w') do |io|
io.puts x.to_yaml
end
end

def less(x)
IO.popen( 'less -', 'w') do |io|
io.puts x.to_yaml
end
end


如上,可以看到在这个文件里可以加载任何你要要加载的库文件.甚至,你也可以在这个文件里写你自己定义的方法.

五. 完全定制


在上面的.irbrc文件示例的基础上,其实,还有一些更高级的定制,当然,或许还需要一些,插件包的支持.你可以通过[url=http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/]下面的例子[/url]看到.
这里只看一个例子:

> 3.45.what? 3
3.45.truncate == 3
3.45.to_i == 3
3.45.prec_i == 3
3.45.floor == 3
3.45.to_int == 3
3.45.round == 3
=> ["truncate", "to_i", "prec_i", "floor", "to_int", "round"]
> 3.45.what? 4
3.45.ceil == 4
=> ["ceil"]
> 3.55.what? 4
3.55.ceil == 4
3.55.round == 4
=> ["ceil", "round"]


当然,条件是你需要在.irbrc的文件里添加类似如下的,代码和文件定义:
 class MethodFinder
def initialize( obj, *args )
@obj = obj
@args = args
end
def ==( val )
MethodFinder.show( @obj, val, *@args )
end
end

class Object
def what?(*a)
MethodFinder.new(self, *a)
end
end


用来实现类似的功能:
 >> "hello".what?  5
"hello".length 5
“hello”.size == 5
=> [“length”, “size”]

>> “foo”.what?(“bar”) "foobar"
"foo".<<("bar") “foobar”
“foo”.+(“bar”) "foobar"
"foo".concat("bar") “foobar”
=> [“<<”, ”+”, “concat”]



其它的这里就不详细描述了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值