Ruby-向puts添加一个类
我希望亚瑟的角色能做到:Ruby-向puts添加一个类,ruby,Ruby,我希望亚瑟的角色能做到: 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
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'
所以它会自动添加“.brown+”部分
并且在bedevere中自动添加“.red+”部分您可以使用gem应用控制台输出:
puts sleep(1.5) && 'ARTHUR'.brown+ ': He is the keeper of the Bridge of Death. He asks each traveller five questions'
输出:
您可以将ruby与多个游行者、替代品一起使用:
/ARTHUR | BEDEVERE/matcher将在输入中查找其中一个单词
哈希'ARTHUR'=>'ARTHUR.brown+','BEDEVERE'=>'BEDEVERE.red+'将用于替换
所以,它看起来是这样的:
# 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'
文本没有类。您正在考虑HTML。puts对HTML一无所知。请将所有相关信息添加到您的问题中,并使用所有相关的库和技术正确标记问题,并确保详细解释您想要完成的任务。”亚瑟·布朗是纯Ruby中的一个错误,没有任何要求。
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?"