ruby Sprintf KeyError

时间:2017-09-14 12:09:21

标签: ruby printf

当我在ruby中执行以下操作时:

template = "/home/%{:user}/%{:name}/%{:environment}/current"
values = {user: "firera", name: "firera_test", environment: "staging"}
template % values

我收到以下错误:

KeyError: key{:user} not found
        from (irb):3:in `%'
        from (irb):3

关键显然在那里,为什么说它无法找到它?

1 个答案:

答案 0 :(得分:2)

您错误地定义了模板。它应该看起来像:

template = "/home/%{user}/%{name}/%{environment}/current"
values = {user: "firera", name: "firera_test", environment: "staging"}
template % values
#=> "/home/firera/firera_test/staging/current"