Skip to content

Fix ArgumentError after invoking setter#1

Merged
ur5us merged 3 commits into
masterfrom
bugfix/argument-error
Jun 4, 2026
Merged

Fix ArgumentError after invoking setter#1
ur5us merged 3 commits into
masterfrom
bugfix/argument-error

Conversation

@ur5us

@ur5us ur5us commented Jun 3, 2026

Copy link
Copy Markdown

Fix add_accessors': wrong number of arguments (given 1, expected 0) (ArgumentError). The issue was the lack of []= operator method definition which resulted in the method_missing hook redefining [] method.

Fix `add_accessors': wrong number of arguments (given 1, expected 0)
(ArgumentError)`. The issue was the lack of `[]=` operator method
definition which resulted in the `method_missing` hook redefining `[]`
method.
@fonji

fonji commented Jun 3, 2026

Copy link
Copy Markdown
Member

Oh I approved but please add tests

@ur5us

ur5us commented Jun 3, 2026

Copy link
Copy Markdown
Author

@fonji I’ve added a spec. I’ve stated the spec in a positive way even though the change was motivated by a lack of behavior leading to an exception. Open to feedback if you think there’s a better way to express the spec.

@fonji fonji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve stated the spec in a positive way even though the change was motivated by a lack of behavior leading to an exception. Open to feedback if you think there’s a better way to express the spec.

Oh it's nice like that, don't worry.
What would be cool but very optional is to have the path leading to the exception covered.

Comment thread spec/lib/hubspot/resource_spec.rb Outdated
@ur5us

ur5us commented Jun 4, 2026

Copy link
Copy Markdown
Author

@fonji

What would be cool but very optional is to have the path leading to the exception covered.

Technically, the steps from the spec are exactly the path formerly triggering the behavior! Here’s an example before this change:

hubspot-api-ruby(master) $ bin/console
3.3.11 :001 > resource = Hubspot::Resource.from_result({ id: 1, firstname: "Test" })
 =>
#<Hubspot::Resource:0x00007efe66cb4d80
...
3.3.11 :002 > resource[:firstname] = "Test"
 => "Test"
3.3.11 :003 > resource[:firstname]
/home/ur5us/dev/repos/hubspot-api-ruby/lib/hubspot/resource.rb:235:in `block (3 levels) in add_accessors': wrong number of arguments (given 1, expected 0) (ArgumentError)
        from (irb):3:in `<main>'
        from <internal:kernel>:187:in `loop'
        from bin/console:30:in `<main>'
3.3.11 :004 >

Though I guess I could add the following to mimic the previous behavior but at that point I’m basically testing Ruby:

resource = Hubspot::Resource.from_result({ id: 1, firstname: "Test" })
resource.send :method_missing, :[]=, :firstname, "Test"
resource[:firstname]
/home/ur5us/dev/repos/hubspot-api-ruby/lib/hubspot/resource.rb:239:in `block (3 levels) in add_accessors': wrong number of arguments (given 1, expected 0) (ArgumentError)
        from (irb):8:in `<main>'
        from <internal:kernel>:187:in `loop'
        from bin/console:30:in `<main>'

The real issue: method_missing is actually somewhat broken IMO as it will blindly redefine an existing accessor for which there’s no writer (:[]=(name, value) in this case).

@fonji fonji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Yeah no need to go further with the specs

@ur5us ur5us merged commit 380da13 into master Jun 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants