赞 | 273 |
VIP | 0 |
好人卡 | 3 |
积分 | 495 |
经验 | 40966 |
最后登录 | 2024-5-13 |
在线时间 | 1916 小时 |
Lv5.捕梦者
- 梦石
- 10
- 星屑
- 39475
- 在线时间
- 1916 小时
- 注册时间
- 2010-11-14
- 帖子
- 3319
|
ruby这么OOP的语言不来点花式写法?
class MyClass
include Comparable
attr_accessor :str, :int
def initialize(str, int)
@str, @int = str, int
end
def <=>(obj)
self.to_i <=> obj.to_i
end
def to_i
self.int
end
def to_s
"[#{str.inspect}, #{int}]"
end
end
a = ('元素a'..'元素e').to_a.zip((1..5).to_a.shuffle!).map { |e| MyClass.new(*e) }
puts a
puts a.sort
|
|