易截截图软件、单文件、免安装、纯绿色、仅160KB

ruby的类与模块(1)

class Point
@x = 1
@y = 2
def initialize(x,y)
@x,@y = x,y
end
end 
代码中的@x,@y为实例变量,实例变量只对self的环境起作用,因此initialize外面的@x=1,@y=2只对类本身起作用,而方法内部,的@x,@y是对对象的实例起作用的。
class Point
include Enumerable
def initialize(x,y)
@x,@y = x,y
end
def each
yield @x
yield @y
end
end

puts Point.new(1,0).all? {|x|x==0}

我们在类里可以定义数组[]方法,也可以定义each方法,定义了each方法,我们就可以混入Enumerable了
使用Enumerable的20多个方法,例如all?
class Point
include Enumerable
attr_accessor :x,:y
def initialize(x,y)
@x,@y = x,y
end
def each
yield @x
yield @y
end
def ==(o)
if o.is_a? Point
@x == o.x && @y ==o.y
else
false
end
end
def eql?(o)
if o.instance_of? Point
@x.eql?(o.x) && @y.eql?(o.y)
else
false
end
end
end
a = Point.new(1,2)
b = Point.new(1.0,2.0)
puts "1"+ a.eql?(b).to_s
puts "2" + a.==(b).to_s 
接下来添加eql? 和==两个方法,看下这两个方法有什么不同
eql?是更为严格的相等,是不做类型判断的


相关文档:

Fix Thrift 0.2.0 Installation with Ruby 1.9.1

When I try the command "gem install thrift" with Ruby 1.9.1, I got a compilation error with something related to a C function "strlcpy()".
Then I searched the web. It seems I am not alone and the community know it.
However I do not want to wait for official update, I want to ......

ruby 连接操作 sql2005


The following is improved version of the code created by David Mullet, from
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fields
attr_wr ......

Windows平台的ruby IDE 点评

转自 http://www.javaeye.com/topic/57474 
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......

Ruby on Rails 命令参考

1.1.Rails
1.1 创建一个Rails应用程序
$ rails app_name
可选项:
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定,scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录
1.2 API Docume ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号