Summary of Changes
Hello @katafrakt, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly enhances the Struct class by introducing support for keyword argument initialization and the :keyword_init option, bringing its behavior closer to CRuby. This change provides more flexible and explicit ways to instantiate structs, improving readability and maintainability for struct definitions and their usage.
Highlights
- Keyword Argument Initialization for Structs: Implemented the ability to initialize
Structinstances using keyword arguments, allowing fields to be set by name (e.g.,MyStruct.new(field1: value1, field2: value2)). - Support for
:keyword_initOption: Added support for the:keyword_initoption when defining aStruct(e.g.,Struct.new(:foo, :bar, keyword_init: true)). This option controls whether a struct can be initialized with positional arguments, keyword arguments, or exclusively keyword arguments. - CRuby Compatibility: The behavior of
:keyword_initand keyword argument initialization aims to mirror CRuby's implementation, including specific error messages for argument mismatches. - Argument Validation: Introduced validation during keyword argument initialization to raise an
ArgumentErrorif unknown keywords are provided, listing the invalid keys.
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with ๐ and ๐ on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. โฉ
Comment on lines +390 to +405
| if (mrb_test(keyword_init) && !mrb_nil_p(keyword_init)) { | ||
| if (argc == 0 || !mrb_hash_p(argv[0])) { | ||
| mrb_raise(mrb, E_ARGUMENT_ERROR, "keyword argument hash expected"); | ||
| } | ||
| return mrb_struct_initialize_withKw(mrb, argv[0], self); | ||
| } | ||
| else if (mrb_equal(mrb, keyword_init, mrb_false_value())) { | ||
| return mrb_struct_initialize_withArg(mrb, argc, argv, self); | ||
| } | ||
| else if (argc == 1 && mrb_hash_p(argv[0])) { | ||
| return mrb_struct_initialize_withKw(mrb, argv[0], self); | ||
| } | ||
| else { | ||
| return mrb_struct_initialize_withArg(mrb, argc, argv, self); | ||
| } |
Comment on lines +358 to +365
| mrb_value keys = mrb_hash_keys(mrb, hash); | ||
| mrb_value invalid_keys = mrb_ary_new(mrb); | ||
|
|
||
| for (mrb_int i = 0; i < RARRAY_LEN(keys); i++) { | ||
| mrb_value key = RARRAY_PTR(keys)[i]; | ||
| mrb_value include_result = mrb_funcall(mrb, members, "include?", 1, key); | ||
| if (mrb_test(include_result) == FALSE) { | ||
| mrb_ary_push(mrb, invalid_keys, key); | ||
| } | ||
| } |
Comment on lines +342 to +352
| /* Initialize all members to nil */ | ||
| for (mrb_int i = 0; i < member_count; i++) { | ||
| mrb_ary_set(mrb, self, i, mrb_nil_value()); | ||
| } | ||
|
|
||
| for (mrb_int i = 0; i < member_count; i++) { | ||
| mrb_value member = RARRAY_PTR(members)[i]; | ||
|
|
||
| if (mrb_hash_key_p(mrb, hash, member)) { | ||
| mrb_value val = mrb_hash_get(mrb, hash, member); | ||
| mrb_ary_set(mrb, self, i, val); | ||
| } | ||
| } |
Comment on lines +253 to +275
| assert "Struct initialize when :keyword_init is true" do | ||
| c = Struct.new(:foo, :bar, keyword_init: true) | ||
| o = c.new(foo: 1, bar: 2) | ||
| assert_equal 1, o.foo | ||
| assert_equal 2, o.bar | ||
|
|
||
| assert_raise(ArgumentError) do | ||
| c.new(1, 2) | ||
| end | ||
| end |