Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 1.22 KB

format-a-hash-into-a-string-template.md

File metadata and controls

35 lines (27 loc) · 1.22 KB

Format A Hash Into A String Template

The % method as defined by String (String#%) allows you to format (interpolate) an object or array of values into a string. That string needs to contain template markers for where the values should go.

Here is an example of folding an array of values into a string with %s format specifier:

> User = Struct.new(:id)
=> User
> user1 = User.new(123)
=> #<struct User id=123>
> "%s ID: %s" % [user1.class.to_s, user1.id]
=> "User ID: 123"

Or perhaps more usefully for a string that acts as a template, you can used named specifiers that correspond to hash keys:

> template = "You paid %{formatted_price} for %{product}. Enjoy your purchase!"
=> "You paid %{formatted_price} for %{product}. Enjoy your purchase!"

> data = { product: "Ruby Explained™", formatted_price: "$38.99" }
=> {:product=>"Ruby Explained™", :formatted_price=>"$38.99"}

> template % data
=> "You paid $38.99 for Ruby Explained™. Enjoy your purchase!"

source