{"id":26142525,"url":"https://github.com/hiteshjasani/funswap","last_synced_at":"2026-05-27T18:32:41.120Z","repository":{"id":31744727,"uuid":"35310792","full_name":"hiteshjasani/funswap","owner":"hiteshjasani","description":"Rubygem to enable swapping functions (module functions) when using a more functional style of programming.","archived":false,"fork":false,"pushed_at":"2015-05-22T23:47:58.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-15T06:45:07.327Z","etag":null,"topics":["mocking","ruby"],"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/hiteshjasani.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-09T01:57:25.000Z","updated_at":"2015-05-22T23:23:21.000Z","dependencies_parsed_at":"2022-09-06T05:31:54.888Z","dependency_job_id":null,"html_url":"https://github.com/hiteshjasani/funswap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hiteshjasani/funswap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiteshjasani%2Ffunswap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiteshjasani%2Ffunswap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiteshjasani%2Ffunswap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiteshjasani%2Ffunswap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiteshjasani","download_url":"https://codeload.github.com/hiteshjasani/funswap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiteshjasani%2Ffunswap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33579665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-27T02:00:06.184Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["mocking","ruby"],"created_at":"2025-03-11T03:57:00.643Z","updated_at":"2026-05-27T18:32:41.099Z","avatar_url":"https://github.com/hiteshjasani.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# funswap\n\nThis was born out of my need to do more functional style programming and testing in Ruby.  With normal\nobject oriented programming, you might use mock objects to handle testing deep stacks.  What do you do\nif your stack is functional?  How do you mock pure functions in Ruby?\n\nLet's say you have a module with some functions.\n\n```ruby\nmodule ABC\n  def self.add2(x)\n    x + 2\n  end\n\n  module_function\n  def mul5(x)\n    x * 5\n  end\nend\n```\n\nIt's easy enough to test these in a straightforward manner since they are functions with no dependency on\nany shared data.  Now suppose those are used in another set of functions or even object#methods.\n\n```ruby\nmodule XYZ\n  def self.calculate_sales_tax(a, b, ...)\n  # ... some work\n  z = ABC.add2(b)\n  # ... some work\n  end\nend\n\nclass TUVController\n  def index\n    # ... some stuff\n    z = ABC.mul5(n)\n    # ... some stuff\n  end\nend\n```\n\nHow would you mock out the calls to `ABC.add2()` and `ABC.mul5()` while testing the XYZ module or the TUVController?\nPerhaps you want `ABC.add2()` to return a different value or throw an exception.  That's what mock object libraries\nenable for objects and FunSwap makes possible with functions.\n\n```ruby\n# outputs 5\nputs ABC.add2(3)\n\n# outputs 24\nFunSwap.with_fn(ABC, :add2, -\u003e(x) { x + 21 }) do\n  puts ABC.add2(3)\nend\n\n# outputs 5 again - the swapping of add2() did not leak outsite the block\nputs ABC.add2(3)\n```\n\nThe mocking is only done for the duration of the block.  Outside of it, the original behavior of the function\nis maintained.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiteshjasani%2Ffunswap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiteshjasani%2Ffunswap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiteshjasani%2Ffunswap/lists"}