[Ruby笔记]10. Ruby object return Boolean nil false #{}

来源:互联网 发布:2017淘宝双十一攻略 编辑:程序博客网 时间:2024/06/05 02:53

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">> endHello 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 outputh_w = 'hello world'puts "This is #{h_w}"# number outputfloat_num = 1.230567puts "#{"num is %.3f" %float_num}"PS C:\Users\Administrator\RubyCode> ruby ex2.rbThis is hello worldnum 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
0 0
原创粉丝点击