ruby / ruby/open-uri

Incorrect API for `IO#read`: 2 parameters expected, only 1 accepted

Open
#53 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
61
Forks
22
Avg merge
15m
Merged PRs (30d)
2

Description

Ruby's IO#read declares 2 parameters: def read(maxlen = nil, out_string = nil).

OpenURI::OpenRead declares a single Hash parameter: https://github.com/ruby/open-uri/blob/0523bdb6b2bf2931bb4774718ada4da3ce7617b5/lib/open-uri.rb#L812

Marcel, a mime-type detection library, expects to use the 2 parameter form of IO#read: https://github.com/rails/marcel/blob/170458c687ed22f07d8829043a04e008a2b1936b/lib/marcel/magic.rb#L136

This inconsistency results in a crash like this:

/Users/francois/.rvm/gems/ruby-3.4.4/gems/open-uri-0.3.0/lib/open-uri.rb:747:in 'read': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:136:in 'block in Marcel::Magic.magic_match_io'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:128:in 'Array#any?'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:128:in 'Marcel::Magic.magic_match_io'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'block in Marcel::Magic.magic_match'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Array#each'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Enumerable#find'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Marcel::Magic.magic_match'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:87:in 'Marcel::Magic.by_magic'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/mime_type.rb:39:in 'block in Marcel::MimeType.for_data'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/mime_type.rb:75:in 'Marcel::MimeType.with_io'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/mime_type.rb:38:in 'Marcel::MimeType.for_data'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/mime_type.rb:31:in 'Marcel::MimeType.for'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/mime_type.rb:11:in 'RubyLLM::MimeType.for'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/attachment.rb:102:in 'RubyLLM::Attachment#determine_mime_type'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/attachment.rb:22:in 'RubyLLM::Attachment#initialize'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:18:in 'Class#new'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:18:in 'RubyLLM::Content#add_attachment'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:39:in 'block in RubyLLM::Content#process_attachments_array_or_string'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:38:in 'Array#each'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:38:in 'RubyLLM::Content#process_attachments_array_or_string'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:48:in 'RubyLLM::Content#process_attachments'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/content.rb:13:in 'RubyLLM::Content#initialize'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/chat.rb:35:in 'Class#new'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/ruby_llm-1.3.1/lib/ruby_llm/chat.rb:35:in 'RubyLLM::Chat#ask'
        from /Users/francois/Projects/marketplace/a.rb:13:in '<main>'
...
$ ruby --version
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [arm64-darwin24]
$ bundle show open-uri
/Users/francois/.rvm/gems/ruby-3.4.4/gems/open-uri-0.3.0
$ bundle show marcel
/Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4
$ bundle show rails
/Users/francois/.rvm/gems/ruby-3.4.4/gems/rails-7.2.2.1

I tried using OpenURI 0.5.0, with essentially the same backtrace:

/Users/francois/.rvm/gems/ruby-3.4.4/gems/open-uri-0.5.0/lib/open-uri.rb:812:in 'read': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:136:in 'block in Marcel::Magic.magic_match_io'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:128:in 'Array#any?'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:128:in 'Marcel::Magic.magic_match_io'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'block in Marcel::Magic.magic_match'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Array#each'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Enumerable#find'
        from /Users/francois/.rvm/gems/ruby-3.4.4/gems/marcel-1.0.4/lib/marcel/magic.rb:124:in 'Marcel::Magic.magic_match'

If I start a new project and leave out open-uri from the Gemfile (meaning I get the default version bundled with Ruby 3.4.4), Marcel does not raise an error.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/open-uri.rb at the OpenURI::OpenRead#read implementation referenced by the issue, then reproduce the two-argument call from marcel/magic.rb:136 with Ruby 3.4.4. The work is done when OpenURI's read accepts the IO#read two-parameter form without the reported ArgumentError; verify the behavior against the provided Marcel example.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.