{"id":22808324,"url":"https://github.com/sixarm/sixarm_ruby_defining","last_synced_at":"2025-03-30T21:15:47.684Z","repository":{"id":1123195,"uuid":"996179","full_name":"SixArm/sixarm_ruby_defining","owner":"SixArm","description":"SixArm.com » Ruby » Meta-programming to detect when you are defining/redefining a method ","archived":false,"fork":false,"pushed_at":"2023-09-15T19:28:00.000Z","size":327,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-06T00:25:24.758Z","etag":null,"topics":["defining","gem","metaprogramming","ruby"],"latest_commit_sha":null,"homepage":"http://sixarm.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SixArm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null}},"created_at":"2010-10-18T03:28:35.000Z","updated_at":"2023-05-22T14:11:10.000Z","dependencies_parsed_at":"2023-07-05T21:01:34.387Z","dependency_job_id":null,"html_url":"https://github.com/SixArm/sixarm_ruby_defining","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_defining","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_defining/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_defining/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SixArm%2Fsixarm_ruby_defining/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SixArm","download_url":"https://codeload.github.com/SixArm/sixarm_ruby_defining/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246379366,"owners_count":20767696,"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":["defining","gem","metaprogramming","ruby"],"created_at":"2024-12-12T11:09:14.445Z","updated_at":"2025-03-30T21:15:47.659Z","avatar_url":"https://github.com/SixArm.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SixArm.com → Ruby → \u003cbr\u003e Class#defining and #redefining metaprogramming methods\n\n* Doc: \u003chttp://sixarm.com/sixarm_ruby_defining/doc\u003e\n* Gem: \u003chttp://rubygems.org/gems/sixarm_ruby_defining\u003e\n* Repo: \u003chttp://github.com/sixarm/sixarm_ruby_defining\u003e\n\u003c!--header-shut--\u003e\n\n\n## Introduction\n\nMeta-programming method that we call when we are defining (or redefining) a class method, to detect if we're accidentally overriding an existing method.\n\n  * Suppose we're creating a class defining a method named \"foo\".\n  * Before we do it, we want to check to see if the method \"foo\" already exists, so we don't accidentally override it.\n  * We call #defining which asks if the method \"foo\" already exists then gives us feedback if we can proceed safely.\n  * We call #redefining if we know the method already exists and we are overriding it/\n\nExample of defining a new method:\n\n    class C\n      defining \"foo\" \n      #=\u003e return true; it's safe to proceed because #foo is not defined.\n\nExample of error detection:\n\n    class C\n      defining \"hash\"\n      #=\u003e raise an error; it's not safe to proceed because #hash is already defined.\n\nExample of intentionally overriding an existing method:\n\n    class C\n      redefining \"hash\"  \n      #=\u003e return true; it's safe to proceed because #hash is already defined.\n\nExample of error detection:\n\n    class C\n      redefining \"foo\"  \n      #=\u003e raise an error; it's not safe to proceed because #foo is not yet defined.\n\n\nFor docs go to \u003chttp://sixarm.com/sixarm_ruby_defining/doc\u003e\n\nWant to help? We're happy to get pull requests.\n\n\n\n\u003c!--install-open--\u003e\n\n## Install\n\n### Gem\n\nTo install this gem in your shell or terminal:\n\n    gem install sixarm_ruby_defining\n\n### Gemfile\n\nTo add this gem to your Gemfile:\n\n    gem 'sixarm_ruby_defining'\n\n### Require\n\nTo require the gem in your code:\n\n    require 'sixarm_ruby_defining'\n\n\u003c!--install-shut--\u003e\n\n\n## Multiple method names\n\nThe methods can take multiple names.\n\nExample of #defining multiple names:\n\n    class C\n      defining \"foo\", \"goo\", \"hoo\"\n      #=\u003e return true if we're safe i.e. all the methods are not yet defined,\n      #=\u003e otherwise raise an error for each method that is already defined.\n\n\nExample of #redefining multiple names:\n\n    class C\n      redefining \"hash\", \"inspect\", \"equal?\"\n      #=\u003e return true if all the methods are already defined,\n      #=\u003e otherwise raise an error for each method that is not yet defined.\n\n\n## Intermixing\n\nWe can intermix the methods however we like:\n\n    class C\n      ...\n      defining \"foo\", \"goo\"\n      ...\n      redefining \"hash\", \"inspect\"\n      ...\n      defining \"goo\"\n      ...\n      redefining \"equals?\"\n   \n\n## Customization for success and failure\n\nFor meta-programming we can customize what happens when #defining and #redefining encounter methods that are\n\n  * defining_success: called by #defining when a method name is not yet defined; this will return true.\n\n  * defining_failure: called by #defining when a method name is already defined; tihs will raise an error.\n\n  * redefining_success: called by #redefining when a method name is already defined; this will return true.\n\n  * redefining_failure: called by #redefining when a method name is not yet defined; tihs will raise an error.\n\n   \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixarm%2Fsixarm_ruby_defining","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsixarm%2Fsixarm_ruby_defining","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsixarm%2Fsixarm_ruby_defining/lists"}