ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Eregon (Benoit Daloze) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
Subject: [ruby-core:119500] [Ruby master Feature#20792] String#forcible_encoding?
Date: Wed, 09 Oct 2024 22:34:34 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-110114.20241009223433.10206@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20792.20241009154452.10206@ruby-lang.org>

Issue #20792 has been updated by Eregon (Benoit Daloze).


Right, that would also solve this specific usage.
However I think `String#with_encoding` is a good general method to have, as for instance there are [lots of `.dup.force_encoding`](https://github.com/search?q=.dup.force_encoding+language%3ARuby&type=code&l=Ruby).
It's one of the rare cases where Ruby core API don't have a nice way to do something in a non-mutating manner so that would fix it.
It's especially striking for frozen literal strings where one just wants it in some specific encoding, but currently one has to `"foo".dup.force_encoding(SOME_ENCODING)`.
There is `String#b` but that's only for the BINARY encoding (and the name is not really clear).

----------------------------------------
Feature #20792: String#forcible_encoding?
https://bugs.ruby-lang.org/issues/20792#change-110114

* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
I would like to add a method to String called `forcible_encoding?(encoding)`. This would return true or false depending on whether the receiver can be forced into the given encoding without breaking the string. It would effectively be an alias for:

```ruby
def forcible_encoding?(enc)
  original = encoding
  result = force_encoding(enc).valid_encoding?
  force_encoding(original)
  result
end
```

I would like this method because there are extremely rare but possible circumstances where source files are marked as binary but contain UTF-8-encoded characters. In that case I would like to check if it's possible to cleanly force UTF-8 before actually doing it. The code I'm trying to replace is here: https://github.com/ruby/prism/blob/d6e9b8de36b4d18debfe36e4545116539964ceeb/lib/prism/parse_result.rb#L15-L30.

The pull request for the code is here: https://github.com/ruby/ruby/pull/11851.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

  parent reply	other threads:[~2024-10-09 22:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-09 15:44 [ruby-core:119495] " kddnewton (Kevin Newton) via ruby-core
2024-10-09 18:20 ` [ruby-core:119496] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 18:23 ` [ruby-core:119497] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 18:53 ` [ruby-core:119498] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 19:07 ` [ruby-core:119499] " austin (Austin Ziegler) via ruby-core
2024-10-09 22:34 ` Eregon (Benoit Daloze) via ruby-core [this message]
2024-10-10  3:44 ` [ruby-core:119501] " nirvdrum (Kevin Menard) via ruby-core
2024-10-11  8:05 ` [ruby-core:119509] " nobu (Nobuyoshi Nakada) via ruby-core
2024-10-11 15:04 ` [ruby-core:119512] " byroot (Jean Boussier) via ruby-core
2024-10-14 17:41 ` [ruby-core:119525] " kddnewton (Kevin Newton) via ruby-core
2024-10-14 19:24 ` [ruby-core:119526] " Eregon (Benoit Daloze) via ruby-core
2024-10-21  8:50 ` [ruby-core:119538] " kddnewton (Kevin Newton) via ruby-core
2024-10-21 10:52 ` [ruby-core:119571] " Eregon (Benoit Daloze) via ruby-core
2024-11-06 18:21 ` [ruby-core:119779] [Ruby master Feature#20792] String#with_encoding(encoding) kddnewton (Kevin Newton) via ruby-core
2024-11-07 10:38 ` [ruby-core:119804] " mame (Yusuke Endoh) via ruby-core
2024-11-08 15:45 ` [ruby-core:119846] " kddnewton (Kevin Newton) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-110114.20241009223433.10206@ruby-lang.org \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).