{"id":17942636,"url":"https://github.com/petercamilleri/in_array","last_synced_at":"2025-04-03T13:27:41.181Z","repository":{"id":13161911,"uuid":"15844800","full_name":"PeterCamilleri/in_array","owner":"PeterCamilleri","description":"A tiny gem to encapsulate data in_array. A case study in replacing program flow with object oriented design.","archived":false,"fork":false,"pushed_at":"2021-05-19T19:01:30.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-09T17:44:52.913Z","etag":null,"topics":["array","polymorphism","ruby","rubygem","utility-library"],"latest_commit_sha":null,"homepage":"","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/PeterCamilleri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-12T15:28:20.000Z","updated_at":"2023-03-05T04:20:47.000Z","dependencies_parsed_at":"2022-08-20T11:31:05.077Z","dependency_job_id":null,"html_url":"https://github.com/PeterCamilleri/in_array","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fin_array","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fin_array/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fin_array/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fin_array/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PeterCamilleri","download_url":"https://codeload.github.com/PeterCamilleri/in_array/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247008807,"owners_count":20868425,"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":["array","polymorphism","ruby","rubygem","utility-library"],"created_at":"2024-10-29T03:06:33.522Z","updated_at":"2025-04-03T13:27:41.164Z","avatar_url":"https://github.com/PeterCamilleri.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The in_array gem.\r\n\r\nThis project contains the Ruby in_array gem. This is perhaps the tiniest gem\r\never created. It allows all objects to respond to the :in_array method that\r\nencapsulates the object in an array unless it is already an array. So\r\n```ruby\r\n'Hello'.in_array    # ['Hello']\r\n['Hello'].in_array  # ['Hello']\r\nmy_object.in_array  # [my_object]\r\n[1,2,3].in_array    # [1,2,3]\r\n```\r\nThis very simple gem accomplishes a very simple task that is nonetheless useful.\r\nFor example a method can now easily accept a single object or an array of\r\nobjects:\r\n\r\n```ruby\r\ndef my_method(args, other)\r\n  args.in_array.each{|arg| process(arg, other) }\r\nend\r\n```\r\nNow all of the following will work correctly:\r\n```ruby\r\nmy_method 12, 11\r\nmy_method 42, 11\r\nmy_method [12, 42], 11\r\n```\r\nFrom an instructive view point, this gem also demonstrates the use of\r\ninheritance as opposed to simulated polymorphism.\r\n\r\n## Installation\r\n\r\nAdd this line to your application's Gemfile:\r\n\r\n    gem 'in_array'\r\n\r\nAnd then execute:\r\n\r\n    $ bundle\r\n\r\nOr install it yourself as:\r\n\r\n    $ gem install in_array\r\n\r\nThe in_array gem itself is found at: ( https://rubygems.org/gems/in_array )\r\n\r\n## Usage\r\nThis is as simple as:\r\n```ruby\r\nrequire 'in_array'\r\n```\r\n\r\nthen, in those places where array-ness was problematic, use:\r\n```ruby\r\nmy_object.in_array.each do |option| #etc, etc, etc...\r\n```\r\n\r\ninstead of\r\n```ruby\r\nmy_object = [my_object] unless my_object.is_a?(Array)\r\nmy_object.each do |option| #etc, etc, etc...\r\n```\r\n\r\n#### Extending the Protocol\r\n\r\nThe in_array gem sets up a simple protocol that permits its behavior to be\r\neasily added to user defined classes that behave (aka duck type) as arrays. The\r\nfollowing snippet shows how this is done:\r\n```ruby\r\nclass MyArray\r\n  #This class behaves like an array.\r\n  include InArrayAlready\r\n\r\n  #Rest of the class omitted.\r\nend\r\n\r\n```\r\n\r\n## Performance\r\n\r\nAll other aspects aside, good code should not exact a significant speed\r\npenalty. The benchmark tests that follow show that the in_array gem should be\r\nexpected to improve performance when compared with the simulated polymorphism\r\nalternative.\r\n\r\nAll benchmarks were done with:\r\n    ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]\r\n\r\nThe first test assumes a simple scenario with only the Array class. Polymorphism\r\nis simulated using the is_a? method:\r\n\r\n    Warming up --------------------------------------\r\n    Process with in_array\r\n                             5.332k i/100ms\r\n    Process with is_a?\r\n                             4.084k i/100ms\r\n    Calculating -------------------------------------\r\n    Process with in_array\r\n                             60.573k (± 0.2%) i/s -    303.924k in   5.017537s\r\n    Process with is_a?\r\n                             45.736k (± 0.2%) i/s -    228.704k in   5.000545s\r\n\r\n    Comparison:\r\n    Process with in_array:    60572.6 i/s\r\n    Process with is_a?   :    45736.0 i/s - 1.32x  slower\r\n\r\nThe second scenario adds a user defined array like class. In this case, two\r\nmethods of simulated polymorphism are tested, based on include? and a hash\r\nof allowed array classes. Here are those results.\r\n\r\n    Warming up --------------------------------------\r\n    Process with in_array\r\n                             5.334k i/100ms\r\n    Process with hash[]\r\n                             1.565k i/100ms\r\n    Process with include?\r\n                             2.656k i/100ms\r\n    Calculating -------------------------------------\r\n    Process with in_array\r\n                             60.959k (± 0.3%) i/s -    309.372k in   5.075113s\r\n    Process with hash[]\r\n                             16.323k (± 0.7%) i/s -     82.945k in   5.081691s\r\n    Process with include?\r\n                             28.529k (± 0.2%) i/s -    143.424k in   5.027394s\r\n\r\n    Comparison:\r\n    Process with in_array:    60959.3 i/s\r\n    Process with include?:    28528.6 i/s - 2.14x  slower\r\n    Process with hash[]  :    16323.1 i/s - 3.73x  slower\r\n\r\n## Contributing\r\n\r\n#### Plan A\r\n\r\n1. Fork it ( https://github.com/PeterCamilleri/in_array/fork )\r\n2. Create your feature branch (`git checkout -b my-new-feature`)\r\n3. Commit your changes (`git commit -am 'Add some feature'`)\r\n4. Push to the branch (`git push origin my-new-feature`)\r\n5. Create a new Pull Request\r\n\r\n#### Plan B\r\n\r\nGo to the GitHub repository and raise an issue calling attention to some\r\naspect that could use some TLC or a suggestion or an idea.\r\n\r\n## License\r\n\r\nThe gem is available as open source under the terms of the\r\n[MIT License](./LICENSE.txt).\r\n\r\n## Code of Conduct\r\n\r\nEveryone interacting in the fully_freeze project’s codebases, issue trackers,\r\nchat rooms and mailing lists is expected to follow the\r\n[code of conduct](./CODE_OF_CONDUCT.md).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fin_array","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetercamilleri%2Fin_array","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fin_array/lists"}