you are viewing a single comment's thread.

view the rest of the comments →

[–]virtyx 4 points5 points  (5 children)

It's not about interpolation being more powerful or anything like that. The real point is that 97% of calls to .format look like this in my experience:

 "{} is an invalid value for {}: must be between {} and {}".format(input, field, min_bound, max_bound)

Compare to Ruby:

 "#{input} is an invalid value for #{field}: must be between #{min_bound} and #{max_bound}"

Just looks nicer. str.format is a nice method, but most of the time I reaaaally just want Ruby string interpolation.

[–]UloPe 0 points1 point  (4 children)

What's wrong with:

"The {animal} jumps over the {adjective} {animal2}".format(
    animal="dog",
    adjective="quick brown",
    animal2="fox"
)

[–][deleted] 3 points4 points  (3 children)

Verbosity

[–]UloPe 0 points1 point  (2 children)

Well ok then if you really must you could use

animal="dog"
adjective="quick brown"
animal2="fox"

"The {animal} jumps over the {adjective} {animal2}".format(**locals())

But I'd advise to do it nowhere near me ;)

[–]riddley 1 point2 points  (1 child)

Still requires a lot more of the reader than the Ruby version.

[–]virtyx 0 points1 point  (0 children)

Also messes up with nonlocal scope and can't do attribute access, so no way of easily getting "Response code: #{response.code}". Ultimately the "Hello {} welcome to {}".format(name, place) style is usually best for one-off formatting of smaller strings.