作者godfat (godfat 真常)
看板Ruby
标题Re: [问题] 请问ruby跟其他语言比较的新功能 ?
时间Tue Sep 30 15:04:23 2008
※ 引述《tonyhome (天空在潮吹)》之铭言:
: : 推 tonyhome:Ruby是纯物件导向 所以他没有primitive data type 09/30 14:41
: : → godfat:那要看你怎麽定义 primitive 09/30 14:45
: Ruby是没有primitive data type的
: 这点我非常确定
: 没有所谓怎麽定义primitive的问题
: 至少我在任何一本Ruby的书和文章都没看过Ruby有primitive这回事
http://en.wikipedia.org/wiki/Primitive_type
你自己看看吧,没有定义就没有名词,只有口语了。
: 像是数字10 还有字元'a'
: 在Ruby都是个物件
: 而且Ruby的变数只有一种
: 就是reference variable
: 用来指向物件的变数
这不代表 Fixnum 就不是 primitive,
你无法写 10.dup, 也无法写 nil.dup, 这些操作在 C (or any other impl...)
都是例外。同时,当然你也可以扩充 Fixnum, 但就像在 Smalltalk:
[quote="wikipedia"]
Exceptions include Smalltalk, which permits primitive datatypes to be
extended within a program, adding to the operations that can be
performed on them or even redefining the built-in operations.
[/quote]
而你不管怎麽让 10.nil? 回传 true, 也无法阻止:
if 10
不成立。因为这也是在 C 写死的,直接判断是否是 NilClass 和 FalseClass 的
唯一 instance, nil 和 false.
你也无法写出能够 swap Fixnum 的 function, 但 Array/String 可以:
def swap a, b
tmp = a.dup
a.replace b
b.replace tmp
end
对 Array/String 有效,对 Fixnum 无效。你也没办法替 Fixnum 写 replace.
没有那麽单纯的,实作上还是有差。
--
Nobody can take anything away from him.
Nor can anyone give anything to him.
What came from the sea,
has returned to the sea.
Chrono Cross
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.128.121.85
1F:→ shelling:Fixnum 那样作应该是故意的而非办不到 ? 10/03 10:49
2F:→ shelling:如果不是这样设计 反而会成为语言的漏洞吧 10/03 10:49
3F:→ godfat:这本来就是设计问题啊 10/03 11:00