Re: [問題] 請問ruby跟其他語言比較的新功能 ?

看板Ruby作者 (godfat 真常)時間15年前 (2008/09/30 15:04), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串6/6 (看更多)
※ 引述《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

10/03 10:49, , 1F
Fixnum 那樣作應該是故意的而非辦不到 ?
10/03 10:49, 1F

10/03 10:49, , 2F
如果不是這樣設計 反而會成為語言的漏洞吧
10/03 10:49, 2F

10/03 11:00, , 3F
這本來就是設計問題啊
10/03 11:00, 3F
文章代碼(AID): #18uSzvlj (Ruby)
討論串 (同標題文章)
文章代碼(AID): #18uSzvlj (Ruby)