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?是更为严格的相等,是不做类型判断的
相关文档:
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 ......
学了一个学期的C语言,看了一个星期的ruby,我才发现为什么老师说C是最基础的,假如没有一个学期的C基础,那ruby我也不用看了。
Ruby和C语言有许多的相同点和不同点,在学习ruby时,有时可以用C里面的思维来理解,就像ruby里面的方法其实就跟C的函数如出一 ......
最近由于学习使用linux下的C开发,需要查询Linux C函数参考,就经常上http://man.chinaunix.net/develop/c&c++/linux_c/default.htm查看,描述得比较详细而且还有例子。
网上还有许多各种技术的网页格式的参考材料都非常强大,可惜很多时候都没有网。于是就想写个脚本可以把文档下载,像android开发者文档一样弄到本地 ......
转 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 ......
转自 http://www.javaeye.com/topic/57474
Windows平台的ruby IDE 点评
在MacOS平台几乎没有什么争议性,大家都用TextMate。但是Windows平台可供选择和使用的IDE很多,却各有各的长处和短处。基于我用过的所有ruby IDE点评一下。windows平台的RoR IDE主要分为两类:一类是重量级的全功能IDE,例如Eclipse,Netbeans ......