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

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

by Tom Stuart
November 2017
Beginner to intermediate
344 pages
7h 2m
Chinese
GoTop Information, Inc.
Content preview from 深入理解運算原理|從簡單的機器到無所不能的程式
夠用的 Ruby
|
9
>> "hello #{'dlrow'.reverse}"
=> "hello world"
如果內插的運算式傳回的物件並非字串,則會自動傳送 to_s 訊息給該物件,並且預期
傳回的字串能用在它的位置。我們能以此來控制內插的物件會如何呈現:
>> o = Object.new
=> #<Object>
>> def o.to_s
'a new object'
end
=> nil
>> "here is #{o}"
=> "here is a new object"
檢閱物件
只要 IRB 需要顯示物件,以下類似的情況就會發生:
inspect
訊息會傳給此物件,而
且應該會傳回它自己的字串表示法。Ruby 裡的所有物件都能判斷
#inspect
的預設實作
物,但藉由提供我們自己的定義,我們可以控制物件在主控台的顯示方式:
>> o = Object.new
=> #<Object>
>> def o.inspect
'[my object]'
end
=> nil
>> o
=> [my object]
列印字串
每個 Ruby 物件(包括
main
)皆可使用
#puts
方法,而且它可以將字串列印到標準輸
出:
>> x = 128
=> 128
>> wh ile x < 1000
puts "x is #{x}"
x = x * 2
end
x is 128
x is 256
x is 512
=> nil
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