Strings And Printings 字符串与输出

简单的输出

Ruby中的简单输出类似Python的print,使用puts进行输出。

puts "Hello World!"
puts "Hello Again"
puts "I like typing this."
puts "This is fun."
puts 'Yay! Printing.'
puts "I'd much rather you 'not'."
puts 'I "said" do not touch this.'

带变量的输出

类似PHP中{$foo}这样的输出方式

PHP
<?php
$foo = "World";
echo "Hello {$foo} !";
Ruby
foo="World"
puts "Hello #{foo} !"
puts "There are #{10} types of people." # 带数字的

带有,号的输出

Ruby中puts输出可以用,号分割,输出结果为各段换行显示。

puts "Hello", "World", "!"
# Output as follows:
#Hello
#World
#!

格式化输出

类似Python(%)、PHP(sprintf),Ruby也可以进行占位符类似的格式化输出。

hello = "Hello"
world = "World"

puts "%s World !" % hello # 单个变量
puts "%s %s !" % [hello, world] # 多个变量
puts "%d %s %.2f" % [12, 23, 123.124124] # 数字

与Python的不同在于,Python中多个变量是用()圆括号表示多个变量

print "%s %s !" % ("Hello", "World")

格式化输出的高级用法

可以指定一个变量为规定好的格式,然后进行格式化输出,一般日志应该是这样指定格式的。

formatter = "%s %s %s %s"
puts formatter % [true, false, true, false]
puts formatter % [1, 2, 3, 4]

字符串连接

Ruby的字符串连接用法类似Python中的字符串连接,用+号进行连接。

puts "Hello" + " " + "World"
puts "." * 10

Heredoc

Ruby中Heredoc是由<<进行界定的。

puts <<HEREDOC
I'm a programmer.
HEREDOC

但是,这样的写法不利于对齐,于是Ruby 2.3中增加了一种新的写法<<~

def hello
    puts <<-HEREDOC
        I'm a programmer. 
    HEREDOC  # 这时候输出会多出空格
end

def world
    puts <<~HEREDOC.
        I'm a programmer.
    HEREDOC # 据说这时候完美去掉空格
end

具体各种可以参见下面的栗子。

def hello
	puts <<HEREDOC
		I know I know
		You will like it.
HEREDOC # 默认的方式

	puts <<-HEREDOC
		I know I know
		You will like it.
	HEREDOC # 可以关键词对齐的方式

	puts <<~HEREDOC
		I know I know
		You will like it.
	HEREDOC # 内容去除缩进的方式
end

hello
其他

Heredoc的用法非常灵活,具体可以参见Ruby China的写法。