Skip to Content
深入理解運算原理|從簡單的機器到無所不能的程式
book

深入理解運算原理|從簡單的機器到無所不能的程式

by Tom Stuart
November 2017
Beginner to intermediate
344 pages
7h 2m
Chinese
GoTop Information, Inc.
Content preview from 深入理解運算原理|從簡單的機器到無所不能的程式
302
|
9
因此只能得到大致的具體答案。在這種情況,抽象結果
Sign::NEGATIVE
告訴我們,任何
-1
-2
-3
等具體的數值都可能是
6 * -9
的答案,但它的答案一定不是
0
或任何像
1
500
的正數。
請注意,因為 Ruby 的值是物件(以它們搭載其運算的資料結構),因此根據我們提供
用作引數的是具體(
Fixnum
)還是抽象(
Sign
)物件,我們可以使用相同的 Ruby 運算
式來執行具體或抽象的計算。這是以特定方式相乘 3 個數值的
#calculate
方法:
def calculate(x, y, z)
(x * y) * (x * z)
end
如果我們以
Fixnum
物件呼叫
#calculate
,這樣的計算將由
Fixnum#*
完成,而且我們會
得到具體的
Fixnum
結果。如果我們改以
Sign
實體呼叫它,就會使用
Sign#*
運算,並且
產生
Sign
結果。
>> calculate(3, -5, 0)
=> 0
>> calculate(Sign::POSITIVE, Sign::NEGATIVE, Sign::ZERO)
=> #<Sign zero>
只要藉著將具體引數換成它們的抽象對應物,並且不需修改即可執行剩餘的程式碼,就
可讓我們有少許的機會在真正的 Ruby 程式裡執行抽象解譯。
這種技術讓人聯想到用在自動化單元測試的
測試替身
test double
,例如
虛設
常式
[
stub
]
替身
[
mock
])。測試替身是將特定的替代物件注入程
式碼,以此作為控制和驗證其行為的方式。使用更逼真的物件作為測試資 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JSON實務手冊

JSON實務手冊

Tom Marrs

Publisher Resources

ISBN: 9789864766000