Ruby rare string interpolation

irb(main):006> '%{amount}.2f' % { amount: 100.0 }
=> "100.0.2f"
irb(main):007> "%<amount>.2f" % { amount: 100.0 }
=> "100.00"

In Ruby, string formatting using %{} and %<> is part of the Kernel#sprintf method. These formatting options are explained in the Ruby documentation under the Kernel module. If just like me you were surprised by this code, check the docs for this one and much more.

Leave a Reply

Your email address will not be published. Required fields are marked *