puts sleep(1.5) && 'ARTHUR: Look! There\'s the old man from scene twenty-four!'
puts sleep(2.5) && 'BEDEVERE: What is he doing here?'
puts sleep(1.5) && 'ARTHUR: He is the keeper of the Bridge of Death. He asks each traveller five questions'
我希望亚瑟部分得到这个:
puts sleep(1.5) && 'ARTHUR'.brown+ ': He is the keeper of the Bridge of Death. He asks each traveller five questions'
因此它会自动添加'.brown+ '
部分
对bedevere自动添加'.red+ '
部分
答案 0 :(得分:1)
您可以使用colorize gem将ANSI colors应用于控制台输出:
# test.rb
require 'colorize'
puts 'Arthur'.red + ' Look! There\'s the old man from scene twenty-four!'
puts 'Bedevere'.blue + ' What is he doing here?'
puts 'Arthur'.red + ' He is the keeper of the Bridge of Death. He asks each traveller five questions'
输出:
答案 1 :(得分:0)
您可以将红宝石gsub与多个游行者一起使用,替换:
/ARTHUR|BEDEVERE/
匹配器将在输入中查找其中一个单词
哈希'ARTHUR' => 'ARTHUR .brown+', 'BEDEVERE' => 'BEDEVERE .red+'
将用于替换
所以,它看起来像这样:
phrase1 = "ARTHUR: Look! There\'s the old man from scene twenty-four!"
phrase1.gsub(/ARTHUR|BEDEVERE/,'ARTHUR' => 'ARTHUR .brown+', 'BEDEVERE' => 'BEDEVERE .red+')
# > "ARTHUR .brown+: Look! There's the old man from scene twenty-four!"
phrase2 = "BEDEVERE: What is he doing here?"
phrase1.gsub(/ARTHUR|BEDEVERE/,'ARTHUR' => 'ARTHUR .brown+', 'BEDEVERE' => 'BEDEVERE .red+')
# > "BEDEVERE .red+: What is he doing here?"