[Ruby-talk]对Fixnum与的讨论
2004-10-07 22:12
399 查看
Bob Evans在应用中发现Fixnum#sort并不使用
<=>操作符,代码如下:
class Fixnum
@@count = 0
alias originalComparator <=>
def <=>(o)
@@count += 1
originalComparator(o)
end
def Fixnum.count
@@count
end
end
Running against it:
>array = [5, 4, 3, 2, 1]
=> [5,4,3,2,1]
>array.sort
=> [1,2,3,4,5]
>Integer.count
=> 0
Expected => 6 > result > 0
实际正是如此,这可以从Ruby的源代码中看出:
$ cd /v/build/ruby/ruby-1.8.1-2004.05.02/
$ less array.c
static int
sort_2(ap, bp)
...
if (FIXNUM_P(a) && FIXNUM_P(b)) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
if (TYPE(a) == T_STRING && TYPE(b) == T_STRING) {
return rb_str_cmp(a, b);
}
...
static VALUE
sort_internal(ary)
VALUE ary;
{
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
rb_block_given_p()?sort_1:sort_2);
return ary;
}
可见,Ruby确实没有使用<=>,而用最一般的
>,<进行比较。
<=>操作符,代码如下:
class Fixnum
@@count = 0
alias originalComparator <=>
def <=>(o)
@@count += 1
originalComparator(o)
end
def Fixnum.count
@@count
end
end
Running against it:
>array = [5, 4, 3, 2, 1]
=> [5,4,3,2,1]
>array.sort
=> [1,2,3,4,5]
>Integer.count
=> 0
Expected => 6 > result > 0
实际正是如此,这可以从Ruby的源代码中看出:
$ cd /v/build/ruby/ruby-1.8.1-2004.05.02/
$ less array.c
static int
sort_2(ap, bp)
...
if (FIXNUM_P(a) && FIXNUM_P(b)) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
if (TYPE(a) == T_STRING && TYPE(b) == T_STRING) {
return rb_str_cmp(a, b);
}
...
static VALUE
sort_internal(ary)
VALUE ary;
{
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
rb_block_given_p()?sort_1:sort_2);
return ary;
}
可见,Ruby确实没有使用<=>,而用最一般的
>,<进行比较。
相关文章推荐
- ATL实现Connection Point的一种简单的方法
- DirectX.Capture
- 使用C#开发COM+组件
- 纯编码实现数据库的建立或压缩
- 从数据库中动态选取下拉列表的方法
- 用ASP备份数据库
- thinking in c++ 卷2
- thinking in c++卷2
- thinkng in c++卷2
- 初学者的问题----csdn系列
- Internet EDI vs. VAN EDI
- How to build a struts application
- log4j使用示例
- Red Hat Linux 7.2汉化
- 面向对象语言基础 一
- Pragmatic Unit Testing in Java with JUnit 书评
- 从java线程中获得运算结果
- C#程序编码规范
- 翻译Sun帮助文件——javax.wireless.messaging(MessageListener)
- 自己写的一个删除目录的代码(自己感觉不错2004-09-12)