{"id":15353794,"url":"https://github.com/westonganger/fast_try","last_synced_at":"2026-01-07T06:12:27.071Z","repository":{"id":66556470,"uuid":"134458379","full_name":"westonganger/fast_try","owner":"westonganger","description":"FastTry is a simple method wrapper to the safe navigation operator in Ruby","archived":false,"fork":false,"pushed_at":"2018-10-04T00:09:42.000Z","size":10,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-13T02:03:33.040Z","etag":null,"topics":["ruby","safe-navigation","try"],"latest_commit_sha":null,"homepage":"","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/westonganger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2018-05-22T18:26:28.000Z","updated_at":"2018-10-04T00:09:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e14eaca-baba-4459-beca-f4ad1cdc47ec","html_url":"https://github.com/westonganger/fast_try","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.1428571428571429,"last_synced_commit":"2e24026bb7439b7e91a018178564d274fcad2585"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Ffast_try","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Ffast_try/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Ffast_try/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Ffast_try/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/westonganger","download_url":"https://codeload.github.com/westonganger/fast_try/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245908428,"owners_count":20692106,"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":["ruby","safe-navigation","try"],"created_at":"2024-10-01T12:15:16.060Z","updated_at":"2026-01-07T06:12:22.051Z","avatar_url":"https://github.com/westonganger.png","language":"Ruby","readme":"# FastTry \u003ca href='https://travis-ci.org/westonganger/fast_try' target='_blank'\u003e\u003cimg src='https://api.travis-ci.org/westonganger/fast_try.svg?branch=master' border='0' alt='Build Status' /\u003e\u003c/a\u003e \n\n\u003ca href='https://ko-fi.com/A5071NK' target='_blank'\u003e\u003cimg height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /\u003e\u003c/a\u003e \n\nRuby 2.3 introduced the safe navigation operator `\u0026.`, however I find that its syntax is too discrete an can be easy to miss when briefly scanning code. Instead I much prefer the syntax of `try` as it seems way more obvious and intentional.\n\nFastTry is a simple method wrapper to the safe navigation operator. By default it is configured as the `try` method.\n\n\u003cu\u003eFeatures / Goals:\u003c/u\u003e\n\n- Utilize the safe navigation operator `\u0026.` while improving readability\n- Improve performance over other implementations such as the `ActiveSupport#try` method\n- Do not worry about most obscure syntax limitations if they cannot be avoided. Speed and simplicity is key.\n- The method provided should behave very similarly to the safe navigation operator. If you need to retain the exact behaviour of ActiveSupport `try`, simply set your own custom method name(s) for FastTry. \n- The only dependency is Ruby 2.3+. It does NOT require Rails /ActiveSupport anything else however it works great with it too!\n\n# Setup\n\nAdd the following line to your gemfile:\n```ruby\ngem 'fast_try', git: 'https://github.com:westonganger/fast_try.git'\n\n## OR if you dont want to use an initializer and default to FastTry.methods = [:try]\ngem 'fast_try', require: ['fast_try', 'fast_try/apply'], git: 'https://github.com:westonganger/fast_try.git'\n```\n\nIf you want change the name(s) of the methods to create/overwrite just add an initializer. If using Rails, simply run `rails generate fast_try:create_initializer`\n\n```ruby\nFastTry.method_names = [:try, :custom_try, :try!]\n\nrequire 'fast_try/apply'\n```\n\n# Usage\n\n```ruby\nstr.try(:upcase).try(:downcase)\n\n# the above statement is now equivalent to using the safe navigation operator Ex.\nstr\u0026.upcase\u0026.downcase\n```\n\n# Known Limitations\n\nOur main goal is speed and simplicity. As such we may not handle some obscure syntax. Please create a PR or Issue if you find a limitation that should be documented here.\n\n- Apparently safe navigation does not work with class methods so this does not either. Ex. `Array\u0026.bad_method` results in `NoMethodError`.\n\n# Differences between FastTry and ActiveSupport#try\n\nIt is not our goal to maintain any consistency with the ActiveSupport version of the `try` method. I do however want to maintain a simple list of the differences. Please create a PR or Issue if you find a difference that should be documented here.\n\n- Nothing reported yet\n\n# Credits\n\nCreated and Maintained by [Weston Ganger - @westonganger](https://github.com/westonganger)\n\nImplementation originally based on this StackOverflow answer by \u003ci\u003eengineersmnky\u003c/i\u003e on https://stackoverflow.com/questions/50471518/ruby-is-it-possible-to-alias-a-method-to-the-safe-navigation-operator\n\n\u003ca href='https://ko-fi.com/A5071NK' target='_blank'\u003e\u003cimg height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /\u003e\u003c/a\u003e \n\n# Benchmarks\n\nAs tested on master branch. See benchmark: `test/benchmarks.rb`\n\n```\nResults when method is found\n       user     system      total        real\n               FastTry  0.430000   0.020000   0.450000 (  0.456562)\n       Safe Navigation  0.070000   0.000000   0.070000 (  0.069512)\n     ActiveSupport try  0.860000   0.100000   0.960000 (  0.959851)\n         Check for nil  0.070000   0.000000   0.070000 (  0.074626)\n           respond_to?  0.230000   0.020000   0.250000 (  0.239758)\n                rescue  0.060000   0.000000   0.060000 (  0.063295)\n  Calculating -------------------------------------\n               FastTry    80.000M memsize (     0.000  retained)\n                           2.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n       Safe Navigation     0.000  memsize (     0.000  retained)\n                           0.000  objects (     0.000  retained)\n                           0.000  strings (     0.000  retained)\n     ActiveSupport try   200.000M memsize (     0.000  retained)\n                           5.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n         Check for nil     0.000  memsize (     0.000  retained)\n                           0.000  objects (     0.000  retained)\n                           0.000  strings (     0.000  retained)\n           respond_to?    40.000M memsize (     0.000  retained)\n                           1.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n                rescue     0.000  memsize (     0.000  retained)\n                           0.000  objects (     0.000  retained)\n                           0.000  strings (     0.000  retained)\n\n  Results when method is not found\n         user     system      total        real\n               FastTry  0.230000   0.000000   0.230000 (  0.227693)\n       Safe Navigation  0.060000   0.000000   0.060000 (  0.065258)\n     ActiveSupport try  0.300000   0.000000   0.300000 (  0.292541)\n         Check for nil  0.060000   0.000000   0.060000 (  0.063945)\n           respond_to?  0.240000   0.000000   0.240000 (  0.245455)\n                rescue  3.580000   0.000000   3.580000 (  3.575365)\n  Calculating -------------------------------------\n               FastTry    80.000M memsize (     0.000  retained)\n                           2.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n       Safe Navigation     0.000  memsize (     0.000  retained)\n                           0.000  objects (     0.000  retained)\n                           0.000  strings (     0.000  retained)\n     ActiveSupport try   120.000M memsize (     0.000  retained)\n                           3.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n         Check for nil     0.000  memsize (     0.000  retained)\n                           0.000  objects (     0.000  retained)\n                           0.000  strings (     0.000  retained)\n           respond_to?    40.000M memsize (     0.000  retained)\n                           1.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n                rescue     2.640B memsize (     0.000  retained)\n                           6.000M objects (     0.000  retained)\n                           1.000  strings (     0.000  retained)\n```\n","funding_links":["https://ko-fi.com/A5071NK'"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Ffast_try","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwestonganger%2Ffast_try","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Ffast_try/lists"}