Skip to content
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

[ fix, change ] puts behavior for Ruby v3.0.x #41

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions lib/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ def to_sjis
end
end

module Kernel
alias original_kernel_puts puts

def puts(*args)
original_kernel_puts(args)
rescue Encoding::CompatibilityError
original_kernel_puts(args.map { |arg| arg.force_encoding('utf-8') })
end
end
23 changes: 23 additions & 0 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: utf-8
require 'test_helper'
require 'aozora2html'

class I18nTest < Test::Unit::TestCase
def test_t
assert_equal "警告(123行目):JIS外字「①」が使われています",
Aozora2Html::I18n.t(:warn_jis_gaiji,
123,
"①".encode("cp932").force_encoding("shift_jis"))
.force_encoding("cp932").encode("utf-8")
end

def test_ruby_puts_behavior
$stdout = StringIO.new
begin
puts "①".encode("cp932").force_encoding("shift_jis")
assert_equal "①\n", $stdout.string.force_encoding("cp932").encode("utf-8")
ensure
$stdout = STDOUT
end
end
end