ITeye博客

2012 年文章归档

共 14 篇文章

在Rails中使用Pry

Pry可看成是IRB的加强版。支持语法高亮等特点。 1、在Gemfile中添加: group :development do gem 'pry' end 运行bundle install。 2、在config/environments/development.r文件最后面添加: silence_warnings do require "pry" IRB = Pry end 保存即可。 运行rails console进入pry终端。 pry项目地址: https://github.com/pry/pry

2012-02-07 06:43 · Ruby · Ruby on Rails

camping 一个小巧的ruby web framework

https://github.com/camping/camping/blob/master/book/02_getting_started 直接看文档就知道了。 源代码不大4k左右值得研究。 Camping is a web framework which consistently stays at less than 4kB of code.You can probably view the complete source code on a single page. = Getting Started Start a new text file called nuts.rb. Here…

2012-02-04 04:01 · Ruby

Markaby (Markup as Ruby)

http://markaby.rubyforge.org/ Markaby is a very short bit of code for writing HTML pages in pure Ruby. It is an alternative to ERb which weaves the two languages together. Also a replacement for templating languages which use primitive languages that blend with HTML. Using Markaby as a Rails plugin…

2012-02-04 03:58 · Ruby

Ruby的一些疑问

1、网点看到一断程序, def m1(a) puts 'invoke m1' puts a end def self.m1(a) puts 'invoke self.m1' a = 20 super end m1 10 运行结果如下: invoke self.m1 invoke m1 20 为何先运行self.m1,这里为何可以使用super?还弄不明白,先记下来。 2、下面的代码在rails源代码中看到的,不明白&:to_s的写法。 (1..10).map(&:to_s) 3、 def who person = "Matz" yield("rocks") p city #=>undefin…

2012-01-26 01:01 · Ruby

小巧强大的编辑软件SublimeText 2

小巧压缩包不到10M,可以通过安装插件扩展功能。还支持VIM模式,通过安装https://github.com/Kronuz/SublimeCodeIntel 插件后,能比较好的支持代码自动完成功能。 一些技巧:会不断更新 1、常用快捷键 F7或Ctrl + b 运行当前文件,ruby文件可以直接运行。 Ctrl + d 选取当前光标所在词组,Ctrl+d+d查找当前光标所在词组,并标亮 删除一行Ctrl+Shift+K 复制一行Ctrl+Shift+K,Ctrl+K+K 方法定位 ctrl+r 文件定位 ctrl+p 命令定位 ctrl+shift+p 自动补全 Tab 打开一个视图alt…

2012-01-12 02:50 · 综合

RSpec测试框架

#参考http://www.slideshare.net/ihower/rspec-7394497 #入门,框架 require 'rspec' class Order #自定义类 end describe Order do #一个类别 #可以Nested加入想要测试的方法是哪个 describe "#amount" do #通常用#开头表示实例方法,.开头表示类方法 context "when user is vip" do #可以再nested加入不同情境 before(:each) do #每段it之前执行,before(:all)整段describe执行一次 @user = User…

2012-01-10 12:59 · Ruby

[转]Ruby - DUP vs CLONE

http://railsblogger.blogspot.com/2009/03/ruby-dup-vs-clone.html Ruby - DUP vs CLONE Both DUP & CLONE can be used to create shallow copy of an object. Both copies the instance variables of obj. But we need to be selective in their usage. Few difference between these are CLONE copies both FROZEN and …

2012-01-09 12:57 · Ruby

[转]eval, class_eval, instance_eval和binding

http://www.cnblogs.com/rubylouvre/archive/2011/08/28/2112321.html 前些天写html生成器的时候用到了erb,在生成html的时候是这么一句: html=tpl.result(binding) binding这个变量(Kernel的一个方法 T_T)有点古怪,就搜了下。它表示了ruby的当前作用域,没有任何对外可见的成员函数,唯一的用途就是传递给eval作第二个参数。因而可以这样: def test_binding magic='brother Chun is PURE MAN' return binding end eval …

2012-01-09 12:10 · Ruby

[转]Ruby中的binding

http://kkito.cn/index.php/blog/getArticle/82/ruby_binding 之前在看erb如何parse的时候发现使用了binding这个东东,没有怎么在意,后来发现很多地方都用到了这个binding。 仔细查了一下手册 Objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. The variables, methods, value of…

2012-01-09 11:50 · Ruby

[转]ROR 中的Nesting resources

http://weblog.jamisbuck.org/2007/2/5/nesting-resources The RESTful routes feature in Rails makes it really, really simple to nest resources within each other. Just give a block to the “map.resources” call, and define further resources on the value yielded to that block: map.resources :accounts do |…

2012-01-06 09:25 · Ruby on Rails

[转]Method visibility in Ruby

From: http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby A common point of confusion to even experienced Ruby programmers is the visibility of public, protected, and private methods in Ruby classes. This largely stems from the fact that the behavior of those keywords in Ruby is differe…

2012-01-04 12:37 · Ruby

[转]Skinny Controller, Fat Model

不错的文章,出处: http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model When first getting started with Rails, it is tempting to shove lots of logic in the view. I’ll admit that I was guilty of writing more than one template like the following during my Rails novitiate: <!-- app/views/people/i…

2012-01-04 11:47 · Ruby on Rails

[转]ActiveRecord教程系列2

出处: http://syue.com/Software/Language/Ruby/740.html (十一、并发处理) 在学习Rails中的并发处理的处理前,我们先简单了解下并发处理的概念。 在有多个处理同时访问同一个数据库的应用程序中,可能会出现这样的情况,因为一个处理更新了数据库中的行,而使得另一个处理中持有的数据变得陈旧了。例如,A和B先后从数据库中提取了相同的数据,并都做了修改,这时B先将自己的修改更新会数据库,稍后,A将自己的修改更新回数据库,这时将会覆盖B所作的修改,当B再次提取数据库后,看到的是A修改的结果,而不是自己的。 一个解决办法就是将更新的表或者行进行锁定,防止其他…

2012-01-04 02:06 · Ruby on Rails

[转]ActiveRecord教程系列1

出处: http://syue.com/Software/Language/Ruby/713.html (一、ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数据等。 Active Record使用基本的ORM模式:表映射成类,行映射成为对象,列映射成对象的属性。与很多大量使用配置的ORM库不同,Active Record最小化了配置。想象一下,有一个使用Active Record的程序把Mysql数据库中的orders表转换到类,通过制定的ID查找…

2012-01-03 13:56 · Ruby on Rails