易截截图软件、单文件、免安装、纯绿色、仅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?是更为严格的相等,是不做类型判断的


相关文档:

21本经典ruby rails电子书


Beginning Ruby from Novice To Perfessional

Head First Rails

Agile Web Development with Rails 3rd Edition(new)

Agile Web Development with Rails 3rd Edition

Agile Web Development with Rails 3rd Editio ......

windows 下ruby的安装

1,安装ruby
下载地址: http://rubyinstaller.org/download.html
2,安装rails
使用命令下载:在命令行运行gem install rails --remote
2,初步了解ruby的文章
http://www.bcbbs.net/html/29671.html
http://www.bcbbs.net/dev/List64.aspx ......

Ruby声音


转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......

Windows平台的ruby IDE 点评

转自 http://www.javaeye.com/topic/57474 
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号