Project

General

Profile

Actions

Feature #11785

closed

add `encoding:` optional argument to `String.new`

Added by usa (Usaku NAKAMURA) about 9 years ago. Updated about 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:71927]

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

Also available in: Atom PDF

Like0
Like0Like0Like0