programing ruby中的一个例子
class Song
    include Comparable
    
def initialize(name,artist,duration)
        @name
=name
        @artist
=artist
        @duration
=duration
    end
    
def <=>(other)
        
return self.duration <=> other.duration
    end
end

song1
=Song.new("1","1",254)
song2
=Song.new("2","2",255)

p song1 
<=> song2
p song1 
> song2
p song1 
== song2
p song1 
< song2


这里例子的问题在哪里? other.duration,实例变量不等于外部可访问的属性,所以这个例子无论是ruby或是ironruby都会报告一个错误
运行这个例子你会发现
test2.rb:9:in `<=>': undefined local variable or method `duration' for #<Song:0x
000005c @name="1", @artist="1", @duration=254>:Song (NoMethodError)
        from :0:in `Initialize##1'


对比ruby 的错误提示,ironruby还有待改进
test2.rb:10:in `<=>': undefined method `duration' for #<Song:0x2b75a64 @duration
=254, @artist="1", @name="1"> (NoMethodError)
        from test2.rb:17


这书居然弱智的将collection 集合翻译为收集,真是无语

另外,这书虽然是很多人都推荐的,但个人感觉,这书的组织实在不怎么
posted on 2008-06-11 11:58  jjxstudio  阅读(188)  评论(0编辑  收藏  举报