Ben*_*Ben 1 ruby messageformat
我有一个格式的字符串:
Hello {0}, today is {1} and the time is {2}. Will you be eating {3}?
给定一个数组["sir", "Monday", "12:00", "tacos"],该字符串应格式化为:
Hello sir, today is Monday and the time is 12:00. Will you be eating tacos?
Ruby有没有某种内置方法来做到这一点?还是有宝石?或者我必须更换字符串?
编辑:我想补充一点,我不允许编辑这些字符串.因此类似sprintf的解决方案不起作用.
使用内核#sprintf:
sprintf("Hello %s, today is %s and the time is %s. Will you be eating %s?", *["sir", "Monday", "12:00", "tacos"])
Run Code Online (Sandbox Code Playgroud)
要么
"Hello %s, today is %s and the time is %s. Will you be eating %s?" % ["sir", "Monday", "12:00", "tacos"]
Run Code Online (Sandbox Code Playgroud)