2
这是我一直想弄清楚的一段时间,但无法弄清楚。如何在Ruby中有条件地格式化字符串?
比方说,我有这四个要素:
name = "Mark"
location = ""
time = Time.now
text - "foo"
如果我用str#%
格式化,像这样:"%s was at the location \"%s\" around %s and said %s" % [name,location,time.to_s,text]
,我将如何避免空白和孤儿词的问题? (例如,“Mark在位置”,“2011-12-28T020500Z-0400左右”和“foo”)
是否还有其他技术可以使用?
是的,它会是空白的。我确实安装了Active Support,所以我可以放在'require'active_support/core_ext/object/blank''中。 – Mark 2011-12-28 16:11:35
而这正是我一直在想的。 做一些类似'parts <<“和”if!location.blank?“的工作将会很好。 – Mark 2011-12-28 20:19:20