Project

General

Profile

Feature #14574

percent literals and binary encoding strings

Added by pb (pumbur _) almost 2 years ago. Updated almost 2 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:85913]

Description

coding "\x00".force_encoding('binary') or ["\x00"].pack('a*') is a hassle, is there a chance to have special percent literal for it?
i.e: %b"\x00" would return binary string. (note, signle-character \x00 there is accidental, the idea is force any string from script encoding to binary)

or, more general suggestion:
allow percent literal with any [a-z] character, move all logic to user-level and allow it to be redefinable:

define_percent_literal(:b){|q| q.force_encoding('binary') }
%b'\x00' #=> "\x00"

History

Updated by phluid61 (Matthew Kerwin) almost 2 years ago

str = "\x00".b #=> "\x00"
str.encoding #=> #<Encoding:ASCII-8BIT>

Incidentally, if you care about it:

str = -"...".b
str.frozen? #=> true
str.encoding #=> #<Encoding:ASCII-8BIT>

Updated by pb (pumbur _) almost 2 years ago

oops. well, that certainly closes first part for me, the one that agitated me to write issue.

there still was cases where i wanted to define other string literals, but i'm not sure its worth it.

sorry for bothering, if no one has thoughts on second part, issue may be closed.

Updated by phluid61 (Matthew Kerwin) almost 2 years ago

I think runtime-customisable percent-literal strings are a bit of a hard ask, since things like %w, %r, %x are so tightly integrated with the parser itself.

Even the question of when to stop consuming (nested brackets? escape characters? interpolation??) means there will be messy and confusing edge-cases.

I'm not against the idea per se, I just think it's not workable in the Ruby we have today.

Updated by nobu (Nobuyoshi Nakada) almost 2 years ago

"Customizable percent literal" had been proposed as %y, YAML literal.

Also available in: Atom PDF