Actions
Feature #11785
closedadd `encoding:` optional argument to `String.new`
Status:
Closed
Assignee:
-
Target version:
-
Description
I propose to add encoding:
optional argument to String.new
.
Ruby doesn't have the syntax to specify the encoding of a string literal.
So we're using String#force_encoding
for the purpose when writing m17n script, just like:
str = "\xA4\xA2".force_encoding('euc-jp')
But when using frozen-string-literal: true
, force_encoding
to literals raise RuntimeError.
So, we must write like:
str = "\xA4\xA2".dup.force_encoding('euc-jp')
or, if don't prefer dup
,
str = String.new("\xA4\xA2").force_encoding('euc-jp')
but these are very unshapely.
To begin with, using force_encoding
would be the cause of the unshapliness.
Therefore, I propose encoding:
optional argument of String.new
.
If it's available, we can write:
str = String.new("\xA4\xA2", encoding: 'euc-jp')
This was proposed at the developer meeting on the last August and was generally favorably accepted (in my impression), but was forgotten after it.
Actions
Like0
Like0Like0Like0