ITeye博客

Ruby

共 29 篇文章

在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

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

[转]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

[转]浅谈Ruby on Rails中的include和extend

http://developer.51cto.com/art/200907/132919.htm 从模块引入方法、变量,使得编程变得简单,扩展性愈强,比以往的类的继承更灵活。这样的引入,仿佛将一个方法块,复制了一份放到了你所引用的类或者模块里面。你完全可以将多个互不相干的类中相同的方法拿出来写到一个模块中,这样可以使得代码精简,符合Ruby的设计初衷,而且,使得你的程序更有条理。 Ruby on Rails常见用法 通常引用模块有以下3种情况: 1.在类定义中引入模块,使模块中的方法成为类的实例方法 这种情况是最常见的 直接 include 即可 2.在类定义中引入模块,使模块中的方法成为类…

2011-12-30 02:20 · Ruby

[转]Ruby中的Rake任务详述

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1683808 Rake简介 Rake和Make及Ant Rake的意思是Ruby Make,一个用ruby开发的代码构建工具。Rake的英文意思是耙子,一种很朴实的劳动工具。真的是很贴切,Rake正是一个功能强大、勤勤恳恳的劳动工具。 但是,为什么Ruby需要Rake? Ruby代码不需要编译,为什么需要Rake?其实,与其说Rake是一个代码构建工具,不如说Rake是一个任务管理工具,通过Rake我们可以得到两个好处: 以任务的方式创建和运行脚本 当然,你可以用脚本来创建每一…

2011-12-29 03:47 · Ruby

[转]ruby中的闭包

原文: http://kenbeit.com/posts/87/ruby%E4%B8%AD%E7%9A%84%E9%97%AD%E5%8C%85 感谢作者的辛勤劳动。 # CLOSURES IN RUBY Paul Cantrell http://innig.net # Email: username "cantrell", domain name "pobox.com" # # 翻译: kenshin54 http://kenbeit.com # I recommend executing this file, then reading it alongside its output. #…

2011-12-26 10:42 · Ruby

ruby中实现闭包

ruby中实现闭包很简单 如果一个方法中返回一个procedure那就构成了一个闭包。 def func x = 0 ->{x += 1 ; p x} end proc = func proc[] #or proc.call() proc[] 输出: 1 2

2011-12-20 09:10 · Ruby

ruby中星号的使用

ruby中星号的使用 1、数字乘法 2 * 3 = 6 2、幂 10**2 = 100 (星号之间不能有空格) 3、字符串扩展 'foo' * 2 = 'foofoo' 4、数组的扩展 ]]*2 = ],1,2,]] 5、创建数组 *a = 1,3,"as" a = 6、数组参数传入时的前缀 a= testFun(*a) == testFun(1 , 2) != testFun() 7、还没发现..

2011-12-20 05:54 · Ruby

[转]ruby的include与extend

原文 http://www.cnblogs.com/rubylouvre/archive/2010/03/31/1701813.html 作者:司徒正美 ruby提供了强大的继承机制,有关继承的方法很多。最简单就是“<<”,但还可以对要继承的东西进行细分,是把它添加到类中呢,还是实例中,这正是本文要介绍的内容。 module Foo def foo puts 'heyyyyoooo!' end end class Bar include Foo end Bar.new.foo # heyyyyoooo! Bar.foo # NoMethodError: undefined method ‘f…

2011-12-20 05:09 · Ruby

[转]Ruby常用的内部变量

Ruby常用的内部变量 原文 http://www.cnblogs.com/rubylouvre/archive/2009/07/23/1529237.html 作者:司徒正美 在ruby程序中,经常会看到一些以$开头的变量,这些不是指我们自己在程序中设置的全局变量,而是指系统内部已经设置好的变量,他们代表了一些特定的意思,下面搜集了一些常用的内部变量,用一些简单的代码说明他们代表的意思: 局部域: 在某一个线程作用域内才能有效,下列也可看做是线程内的局部变量。 PS:这边讲解的几个变量都是于正则匹配相关的,正则匹配过程的代码是一样的,这里不重复输入正则表达式匹配的代码,只在第一个例子里给出…

2011-12-20 03:59 · Ruby

ruby与javascript面向对象编程的比较

原文: http://howtonode.org/object-graphs-3 作者分析了ruby与javascript两者在面向对象模式的区别,作者用图形表达这两者在面向对象模式上的区别,值得一看。这里只摘取重点部分,有兴趣的读者可看原文。 Ruby 先来看一个简单的字符串: animal = "cat" 对象图如下: Notice that every object has a class. Our string is of class String which inherits from the class Object. It's class String is of class …

2011-12-06 14:05 · Ruby

解决ruby中文乱码

在文件头加上#encoding=UTF-8,示例: #encoding=UTF-8 puts '你好,中国!' #在读取带有中文字符的文本文件时,要读取的文件必须以UTF-8编码保存。 File.open('c:\\test.txt') do |file| file.each_line{|line| puts line} end

2011-12-06 04:20 · Ruby

[转帖]ruby 中的 respond_to 和send 方法

Ruby中 respond_to? 和 send 的用法 原文: http://fujinbing.iteye.com/blog/1125990 如同其他的OO语言一样,在ruby中,通过给对象发送消息,来完成对象的功能,比如 str.upcase ,就是给str发送upcase的消息,点操作符(.),就是用来给对象发送消息的,str接受到消息,然后执行与消息对应的功能。 但是,某些时候,我们并不知道对象能响应哪些消息,比如下面的代码就会产生错误: obj = Object.new obj.talk undefined method 'talk' for # Object:0x1234567…

2011-12-06 03:58 · Ruby

[转帖]ruby 中的符号

原文: http://fujinbing.iteye.com/blog/1125897 "<<" 创建一个绑定到特定对象的class a="my string" class <<a def to_s "this is #{self}" #将原来的array中的to_s方法进行修改 end end puts a.to_s # this is my string 2."@" instance variable is simply a name preceded by an “at” sign (@) "@@" Class Variables “$” 全局变量 初学Ruby的时候,往往对字符串(S…

2011-12-06 03:55 · Ruby