[RUBY] Character range / character string range

I was curious about the behavior of the string range of Crystal, so I investigated various things in other languages.

Perl

% perl -v | head -2 | tail -1
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux
% perl -e 'print chr ++($foo = ord "Z");'
[
% perl -e 'print ++($foo = "Z");'
AA
% perl -e 'print "A".."Z";'
ABCDEFGHIJKLMNOPQRSTUVWXYZ
% perl -e 'print "A".."z";'
ABCDEFGHIJKLMNOPQRSTUVWXYZ

This is intense.

% perl -e 'print "a".."Z";'
abcdefghijklmnopqrstuvwxyz
% perl -e 'print "ZZ".."z";'
%

Ruby

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]
> "Z".ord.succ.chr
=> "["
> "Z".succ
=> "AA"
("A".."z").to_a
=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
> ("A".."AA").to_a
=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA"]
> ("ZZ".."z").to_a
=> ["ZZ"]

Python

$ python -V
Python 3.4.2
In [1]: x = ord("Z"); x += 1; chr(x)
Out[1]: '['

Can not.

There is no such syntax, but it can be written.

for x in range(ord("A"), ord("[")):
    print(chr(x), end="")
#=> ABCDEFGHIJKLMNOPQRSTUVWXYZ

You can also use string.ascii_letters with ʻimport string`, but only in uppercase and lowercase letters.

for x in string.ascii_letters:
    print(x, end="")
#=> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Can not.

D language

% dmd -v | head -1
DMD64 D Compiler v2.068.0
import std.stdio;

void main()
{
  char x;
  (++(x = 'Z')).writeln;
}
% rdmd x.d
[
import std.stdio;
import std.string;

void main()
{
  "Z".succ.writeln;
}
% rdmd xx.d
AA
import std.stdio;
import std.range;

void main()
{
  iota('A', '[').writeln;
}
% rdmd xxx.d
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Can not.

Crystal

% crystal -v
Crystal 0.7.7 [170f859](Sat Sep  5 02:53:51 UTC 2015)
puts 'Z'.succ
% crystal x.cr
[
puts "Z".succ
% crystal x.cr
AA
% cat xx.cr
p ('A'..'z').to_a
% crystal xx.cr
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

This is fine.

% cat xxx.cr
p ("A".."Z").to_a
% crystal xxx.cr
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

Case of problem.

% cat xxx.cr
p ("A".."z").to_a

"A"-> "Z"-> "AA"-> "ZZ"-> "AAA"-> "AAA .."-> "ZZZ .." is called infinitely.

% crystal xxx.cr
^C

General comment

The implementation of the string range is quite different. I didn't put it here, but Julia and Elixir don't seem to implement the string range or the equivalent of String # succ.

Recommended Posts

Character range / character string range
Various character string operations
BLAST result-like character string display
Date and time ⇔ character string
Python f character (formatted string)
range
Is it a character string operation?
String format
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
# 5 [python3] Extract characters from a character string
String format 2
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
String summary 1
Character code
[Pandas] Expand the character string to DataFrame
raw string
Basic grammar of Python3 system (character string)
Python string
Python basic course (4 numeric type / character string type)
[PowerShell] Get the reading of the character string
Calculation of match rate of character string breaks [python]
I tried to generate a random character string
"Parentheses character string" Simple parser implementation example summary