-
-
Save 0x1eef/455633d8e0193b20ffe6c7cb5918a154 to your computer and use it in GitHub Desktop.
An implementation of Python's formatted strings in pure Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "bundler/inline" | |
| gemfile do | |
| gem "binding_of_caller" | |
| end | |
| require "binding_of_caller" | |
| module Kernel | |
| def f(template, vars = binding.of_caller(1)) | |
| template.gsub(/\{.+\}/) { vars.eval(_1[1..-2]) } | |
| end | |
| end | |
| name = "foo" | |
| print(f"hello {name}") | |
| print(f"{f\"{6+7}\"}") | |
| ## | |
| # hello foo | |
| # 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment