Re: [心得] 呼叫方法都是訊息傳遞(Message Passing)
※ 引述《markmcm (Markmcm)》之銘言:
: 標題: [心得] 呼叫方法都是訊息傳遞(Message Passing)
: 時間: Tue Mar 29 11:16:33 2011
:
: 正在讀這個文章
:
: http://rubylearning.com/blog/2010/11/03/
: do-you-understand-rubys-objects-messages-and-blocks/
請原諒我懶得閱讀了,直接回你的文章..
: send 這個方法能夠把訊息傳給物件的樣子,然後就會呼叫相關的方法。
: 有人知道這個有什麼關鍵性的意義嗎?想說有這樣子的設計一定有其原因。
:
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 163.29.185.99
: 推 chchwy:我記得訊息傳遞是從smalltalk傳下來的 03/29 11:33
yes, 這是來自 smalltalk 的特性,而 smalltalk 比 ruby 還純,
連 if then else 也是 method call. 寫成 ruby 的話可能像這樣:
condition.if(then_clause, else_clause)
class TrueClass
def if then_clause, else_clause
then_clause.call
end
end
class FalseClass
def if then_clause, else_clause
else_clause.call
end
end
: 推 ducksteven:dock typing 03/29 11:43
: → ducksteven:duck 03/29 11:43
: → poga:message跟duck typing並不一定相關吧? 03/29 13:00
: → JmeHsieh:run time vs comepile time,比較彈性,but 03/29 16:42
: → JmeHsieh:但是相對compile time能做的優化就比較少了 03/29 16:44
以上都不盡然...
: 剛剛讀了維基百科的解釋,正如 Ruby 跟 C 的不同一樣
: 一個是 message passing 一個是 call mechanism ,這兩個是對比。
: 這兩種模式有三個不同:
:
: 1. Message passing 通常要把所有變數都複製一份傳到倍呼叫的地方。
: Call 只需要把記憶體位置或者是 CPU regster 丟出去即可。
這個怪怪的,不明白是指什麼
: 2. 非同步類的 Message passing 不一定要等對方回復後才能繼續
: 但是 call 是一定要等對方結束。
這個比較像在說 actor model, 跟 ruby 的 method dispatching 沒什麼關係
: 3. Message passing 能夠提供 1對多的服務,
: 這也代表物件本身可能隨時都會發生更動。
這個也不知道在講什麼...
: 不過這些特性好像跟 Ruby 架構比較沒相關就是了。
: 至少我以為大部分的 Ruby 程式還是拿來當 call 寫所以也沒差?
: 主要還是因為都是 Message ,所以什麼都是用 reflection 來作
: runtime中途也才能隨便插入新的功能,就是 ruby 的強處。
: 但是我還是想問有什麼時候會用到上面說的三點嗎?
可以這麼說。但你上面列的三點則都怪怪的... 需要講更多一點
--
#!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》
# if a dog nailed extra legs that http://www.ptt.cc/bbs/Ruby/index.html
# walks like an octopus, and Welcome ~Ruby@ptt~
# talks like an octopus, then ◢█◣ http://www.ruby-lang.org/
# we are happy to treat it as █ http://www.ruby-doc.org/
# if it were an octopus. ◥ ◤ http://www.rubyforge.org/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.160.129
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章