[Ruby筆記]10. Ruby object return Boolean nil false #{}

Ruby中一切皆對象,任何對象都有一個布爾值返回值(Boolean value)

nil

  • puts "Hello world" 這個語句返回Booleannil,並且按照功能輸出字符串Hello world
PS C:\Users\Administrator\RubyCode> irb --simple-prompt

>> puts "Hello world"
Hello world
=> nil

>> if puts "Hello world"
>>    puts "You can't see this"
>> end
Hello world
=> nil

false or true

  • 1 == 2 返回Booleanfalse

  • if...end 代碼塊整體返回Booleannil

PS C:\Users\Administrator\RubyCode> irb --simple-prompt

>> if 1 == 2
>>    puts " One is equal to Two"
>> end
=> nil

>> 1 == 2
=> false

>> 1 != 2
=> true

extra #{}

#{} 裏面使用佔位符輸出浮點數值如下(還自動給四捨五入了):

PS C:\Users\Administrator\RubyCode> more ex2.rb
# string output
h_w = 'hello world'
puts "This is #{h_w}"

# number output
float_num = 1.230567
puts "#{"num is %.3f" %float_num}"


PS C:\Users\Administrator\RubyCode> ruby ex2.rb
This is hello world
num is 1.231

reference

《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
2.2. Crafting an object: The behavior of a ticket

どんだけ~
     η゛
  ∧ ∧(≡)
 (´∀`) /
⊂   ノ
 (つ ノ
 (ノ

http://emoji.vis.ne.jp/dondake.htm
發佈了64 篇原創文章 · 獲贊 12 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章