Skip to content

Fix frozen string literal warning in magic detection #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/marcel/magic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def self.magic_match(io, method)

io.binmode if io.respond_to?(:binmode)
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
buffer = "".encode(Encoding::BINARY)
buffer = (+"").encode(Encoding::BINARY)

Choose a reason for hiding this comment

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

Perhaps this is better for readability:

Suggested change
buffer = (+"").encode(Encoding::BINARY)
buffer = String.new.encode(Encoding::BINARY)

Choose a reason for hiding this comment

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

I see from the bench-marking that you did already consider that option. Also this is more succinct hence better.

Suggested change
buffer = (+"").encode(Encoding::BINARY)
buffer = String.new(encoding: Encoding::BINARY)

Choose a reason for hiding this comment

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

Please disregard!

Choose a reason for hiding this comment

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

I think what you want here is to declare a mutable string with binary encoding. That's

String.new(encoding: Encoding::BINARY)

Choose a reason for hiding this comment

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

Suggested change
buffer = (+"").encode(Encoding::BINARY)
buffer = String.new(encoding: Encoding::BINARY)


MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
end
Expand Down