{"id":25824429,"url":"https://github.com/petitviolet/rstructural","last_synced_at":"2026-03-15T23:05:46.112Z","repository":{"id":56893034,"uuid":"233547769","full_name":"petitviolet/rstructural","owner":"petitviolet","description":"Ruby structural types: Struct, Enum, Algebraic Data Type","archived":false,"fork":false,"pushed_at":"2020-03-14T13:17:28.000Z","size":51,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-27T11:07:21.423Z","etag":null,"topics":["adt","enum","ruby","ruby-gem","struct","structured-data"],"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/petitviolet.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":"2020-01-13T08:33:02.000Z","updated_at":"2025-05-12T19:02:31.000Z","dependencies_parsed_at":"2022-08-21T01:50:12.563Z","dependency_job_id":null,"html_url":"https://github.com/petitviolet/rstructural","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/petitviolet/rstructural","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petitviolet%2Frstructural","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petitviolet%2Frstructural/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petitviolet%2Frstructural/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petitviolet%2Frstructural/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petitviolet","download_url":"https://codeload.github.com/petitviolet/rstructural/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petitviolet%2Frstructural/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266578483,"owners_count":23951143,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["adt","enum","ruby","ruby-gem","struct","structured-data"],"created_at":"2025-02-28T12:51:09.279Z","updated_at":"2026-03-15T23:05:41.092Z","avatar_url":"https://github.com/petitviolet.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rstructural - Ruby structural types\n\n[![rstructural](https://badge.fury.io/rb/rstructural.svg)](https://badge.fury.io/rb/rstructural)\n[![Actions Status](https://github.com/petitviolet/rstructural/workflows/test/badge.svg)](https://github.com/petitviolet/rstructural/actions)\n\n- Rstruct \n    - Struct implemented with Ruby\n- Enum\n    - A set of constants\n- Algebraic Data Type(ADT)\n    - A set of objects\n    - A object is a constant or a set of objects\n\n### Applied Types\n\n- Option\n    - Some or None\n- Either\n    - Left or Right\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rstructural'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install rstructural\n\n## Usage\n\n### require\n\n```ruby\nrequire 'rstructural'\n# Imported these types\n# - Rstruct \n# - Enum \n# - ADT\n```\n\nor, require each modules with namespace `Rstructural`\n\n```ruby\nrequire 'rstructural/struct'\n# - Rstructural::Struct \nrequire 'rstructural/enum'\n# - Rstructural::Enum \nrequire 'rstructural/adt'\n# - Rstructural::ADT\n```\n\n### Rstruct\n\n```ruby\nmodule RstructSample\n  # define a struct type\n  Value = Rstruct.new(:value)\n\n  puts Value.name #=\u003e 'RstructSample::Value'\n  puts value = Value.new(100) #=\u003e 'RstructSample::Value(value: 100)\n  puts value == Value.new(100) #=\u003e true\n  puts value.value == 100 #=\u003e true\n  case value\n    in Value[n] # pattern match (Ruby 2.7~)\n    puts \"here! value: #{n}\" #=\u003e 'here! value: 100'\n  else\n    raise\n  end\nend\n```\n\n### Enum\n\n```ruby\nmodule EnumSample\n  # define enum\n  module Status\n    extend Enum\n\n    # method(:enum) creates a enum value\n    OK = enum 200\n    NotFound = enum 404\n    InternalServerError = enum 500 do\n      # add block to define custom methods\n      def message\n        \"Something wrong\"\n      end\n    end\n  end\n\n  puts Status::OK #=\u003e EnumSample::Status::OK(value: 200)\n  puts Status::InternalServerError.message  #=\u003e Something wrong\n\n  # find enum by value\n  case Status.of(404)\n  in Status::NotFound\n    puts \"NotFound!!!\" #=\u003e NotFound!!!\n  else\n    raise\n  end\nend\n```\n\n### ADT\n\n```ruby\nmodule AdtSample\n  # define ADT\n  module Shape\n    extend ADT\n\n    # define a constant object\n    Point = const\n\n    # define a object with 1 attribute\n    Circle = data :radius do |mod|\n      def scale(i)\n        Circle.new(radius * i)\n      end\n      def area\n        3.14 * radius * radius\n      end\n    end\n    # define a object with 2 attributes\n    Rectangle = data :width, :height do |mod|\n      def area\n        width * height\n      end\n    end\n\n    # define common interface\n    interface do \n      def is_circle?\n        case self\n        in Circle \n          true \n        else\n          false\n        end\n      end\n    end\n  end\n\n  puts Shape::Point #=\u003e AdtSample::Shape::Point\n  puts Shape::Rectangle.new(3, 4) #=\u003e AdtSample::Shape::Rectangle(width: 3, height: 4)\n  puts Shape::Rectangle.new(3, 4).area #=\u003e 12\n  puts Shape::Circle.new(5).scale(2).area #=\u003e 314.0\n  puts Shape::Circle.is_circle? #=\u003e true\n\n  case Shape::Rectangle.new(1, 2)\n  in Shape::Rectangle[Integer =\u003e i, Integer =\u003e j] if j % 2 == 0\n    puts \"here! rectangle #{i}, #{j}\" #=\u003e here! rectangle 1, 2\n  else\n    raise\n  end\nend\n```\n\n### Rstructural::Option\n\n```ruby\nputs Option.of(100).map { |v| v * 2 } #=\u003e Option::Some(value: 200)\nputs Option.of(nil).map { |v| v * 2 } #=\u003e Option::None\nputs Option.of(100).flat_map { |v| Option.of(v * 2) } #=\u003e Option::Some(value: 200)\nputs Option.of(100).flat_map { |v| v * 2 } #=\u003e 200\nputs Option.of(nil).flat_map { |v| Option.of(v * 2) } #=\u003e Option::None\nputs Option.of(100).get_or_else { 0 } #=\u003e 100\nputs Option.of(nil).get_or_else(0) #=\u003e 0\nputs Option.of(nil).get_or_else { 0 } #=\u003e 0\nputs Option.of(nil).is_a?(Option) #=\u003e true\n```\n\n### Rstructural::Either\n\n```ruby\nputs Either.try { 100 } #=\u003e Either::Right(value: 100)\nputs Either.try { raise \"this is error\" } #=\u003e Either::Left(value: this is error)\nputs Either.try { raise \"this is error\" }.value.inspect #=\u003e #\u003cRuntimeError: this is error\u003e\nputs Either.right(100).map { |v| v * 2 } #=\u003e Either::Right(value: 200)\nputs Either.left(100).map { |v| v * 2 } #=\u003e Either::Left(value: 100)\nputs Either.left(100).map_left { |v| v * 2 } #=\u003e Either::Left(value: 200)\nputs Either.right(100).flat_map { |v| Either.right(v * 2) } #=\u003e Either::Right(value: 200)\nputs Either.right(100).flat_map { |v| Either.left(v * 2) } #=\u003e Either::Left(value: 200)\nputs Either.left(100).flat_map { |v| Either.right(v * 2) } #=\u003e Either::Left(value: 100)\nputs Either.left(100).flat_map_left { |v| Either.right(v * 2) } #=\u003e Either::Right(value: 200)\nputs Either.right(100).right_or_else { 0 } #=\u003e 100\nputs Either.left(100).right_or_else { 0 } #=\u003e 0\nputs Either.right(100).left_or_else { 0 } #=\u003e 0\nputs Either.left(100).left_or_else { 0 } #=\u003e 100\nputs Either.right(100).right? #=\u003e true\nputs Either.right(100).left? #=\u003e false\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/petitviolet/rstruct. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/petitviolet/rstruct/blob/master/CODE_OF_CONDUCT.md).\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://petitviolet.mit-license.org/).\n\n## Code of Conduct\n\nEveryone interacting in the Rstruct project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/petitviolet/rstruct/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetitviolet%2Frstructural","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetitviolet%2Frstructural","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetitviolet%2Frstructural/lists"}