[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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值