{"id":15405671,"url":"https://github.com/zverok/fstrings","last_synced_at":"2025-04-17T01:53:44.210Z","repository":{"id":56847782,"uuid":"232328032","full_name":"zverok/fstrings","owner":"zverok","description":"Python-alike fstrings (formatting strings) for Ruby","archived":false,"fork":false,"pushed_at":"2023-02-25T17:41:27.000Z","size":14,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T05:51:17.622Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zverok.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-07T13:15:11.000Z","updated_at":"2023-12-14T12:25:43.000Z","dependencies_parsed_at":"2024-10-19T11:21:40.780Z","dependency_job_id":"39d3c780-db65-4f6a-bea8-4903fd070746","html_url":"https://github.com/zverok/fstrings","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.2857142857142857,"last_synced_commit":"92a4d5020ad5d48332e60e5cecbc24f97d430e61"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Ffstrings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Ffstrings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Ffstrings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zverok%2Ffstrings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zverok","download_url":"https://codeload.github.com/zverok/fstrings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248974120,"owners_count":21192086,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T16:18:02.690Z","updated_at":"2025-04-17T01:53:44.193Z","avatar_url":"https://github.com/zverok.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FStrings\n\nFStrings is an _experimental_ gem implementing Python-alike fstrings (formatting strings) in Ruby.\n\nThe idea is, in Ruby, we have two ways to insert some variable values in strings:\n\n1. String interpolation: `puts \"Foo #{value} bar\"`\n2. `String#%` (or `Kernel#format`, if you want): `puts \"Foo %.2f value\" % value`\n\nFirst is more convenient (with the variable name where it should be rendered), while the second is much more powerful, allowing to specify various formatting flags. `FStrings` tries to close this gap, with a bit of idea stealing (from the [Python](https://www.python.org/dev/peps/pep-0498/)) and a bit of dark magic ([binding_of_caller](http://github.com/banister/binding_of_caller)).\n\n## Showcase\n\nIn its basic form, FStrings formatting looks just like string interpolation (just using `{}` instead of `#{}`):\n\n```ruby\nrequire 'fstrings'\ninclude FStrings\n\nvalue = 5\nputs f\"Simple: {value}\"\n# =\u003e \"Simple: 5\"\n```\n\nBut it also allows to specify formatting flags, after `%` sign (the regular [Kernel#format](https://ruby-doc.org/core-2.7.0/Kernel.html#method-i-format)'s syntax works):\n\n```ruby\nputs f\"Formatted: {value%+i}\"\n# =\u003e \"Formatted: +5\"\n\nfloat = 1.2345\nputs f\"Formatted: {float%.2f}\"\n# =\u003e \"Formatted: 1.23\"\n```\n\nThat's mostly it! But not **all** of it :)\n\nFStrings also support **`x=` syntax** (borrowed from the recent [Python 3.8](https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging)), indispensable for `puts`-debugging:\n\n```ruby\nputs f\"Named: {value=%+i}\"\n# =\u003e \"Named: value=+5\"\n\n# Any expression can be interpolated this way:\nr = 12\nputs f\"Circle area: {Math::PI * r**2 = %.3f}\"\n# =\u003e \"Circle area: Math::PI * r**2 = 452.389\"\n```\n\nFStrings allows to define **custom formatters** for your own classes, and automatically define one for `Time` (it passes the format string to `strftime`):\n\n```ruby\nputs f\"Current time is {Time.now %H:%M (%b %d)}\"\n# =\u003e \"Current time is 15:00 (Jan 07)\"\n```\n\nTo define your own, just do this:\n\n```ruby\nPoint = Struct.new(:x, :y)\n# First argument is formatted value, second is format string\nFStrings.def_formatter(Point) { |val, str| str.gsub('%x', val.x.to_s).gsub('%y', val.y.to_s) }\n\npoint = Point.new(10, 20)\n\nputs f\"See, it works: {point %x;%y}\"\n# =\u003e \"See, it works: 10;20\"\n```\n\n(The formatting strings considered everything starting from the first `%` including it.)\n\n## Quirks and problems\n\nThe library is _new and experimental_. It is _probably_ helpful in debugging, but probably not advised for any production. The problems I can think of:\n\n* `binding_of_caller` and `eval` are used inside. It is black and unholy magic, obviously;\n* funny `f\"foo\"` syntax is used to make it look like Python's native fstrings, which could be repulsive for some. In fact, it is just `f()` method, you can use it without `include FStrings`, with just `FStrings.f()`;\n* fstrings-parser is not that mature; it is tested, but can break on more complicated strings (which you hopefully won't need for debugging);\n* probably, parsed strings should be cached, currently, they are not (so in a method which you call 2 mln times, could provide serious slowdown);\n* considering simplistic formatting string definition, statements using `%` can't be inspected (everything after `%` would be thought to be a formatting string).\n\n## Author \u0026 license\n\n* [Victor Shepelev](https://zverok.github.io)\n* MIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Ffstrings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzverok%2Ffstrings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzverok%2Ffstrings/lists"}