string#[]

N

Newbie

This must be a common newbie question, but I can't find the answer.

Why does string#[] return an ASCII code, rather than a character?

"abc"[1,2] #-> "bc"
"abc"[1..2] #-> "bc"
"abc"[1] #-> 98
 
R

Robert Klemme

This must be a common newbie question, but I can't find the answer.

Why does string#[] return an ASCII code, rather than a character?

"abc"[1,2] #-> "bc"
"abc"[1..2] #-> "bc"
"abc"[1] #-> 98

Because there is no character class in Ruby. If you need a one
character string you can do

irb(main):001:0> "abc"[2,1]
=> "c"
irb(main):002:0> "abc"[2].chr
=> "c"

I guess the former is more performant because the internal buffer can be
shared.

Kind regards

robert
 
T

Thomas Adam

This must be a common newbie question, but I can't find the answer.

Why does string#[] return an ASCII code, rather than a character?

"abc"[1,2] #-> "bc"
"abc"[1..2] #-> "bc"
"abc"[1] #-> 98

It tells you why in "ri String#[]":

``
Element Reference---If passed a single +Fixnum+, returns the code
of the character at that position. If passed two +Fixnum+ objects,
returns a substring starting at the offset given by the first, and
a length given by the second. If given a range, a substring
containing characters at offsets given by the range is returned.
''

-- Thomas Adam
 
N

Newbie

That answers what?, which I already knew. I'm asking why?

Thomas said:
This must be a common newbie question, but I can't find the answer.

Why does string#[] return an ASCII code, rather than a character?

"abc"[1,2] #-> "bc"
"abc"[1..2] #-> "bc"
"abc"[1] #-> 98

It tells you why in "ri String#[]":

``
Element Reference---If passed a single +Fixnum+, returns the code
of the character at that position. If passed two +Fixnum+ objects,
returns a substring starting at the offset given by the first, and
a length given by the second. If given a range, a substring
containing characters at offsets given by the range is returned.
''

-- Thomas Adam
 
N

Newbie

Why not a 1-character string?

Robert said:
This must be a common newbie question, but I can't find the answer.

Why does string#[] return an ASCII code, rather than a character?

"abc"[1,2] #-> "bc"
"abc"[1..2] #-> "bc"
"abc"[1] #-> 98

Because there is no character class in Ruby.
<snip>
 
G

gwtmp01

Why not a 1-character string?

Even if String#[index] returned a 1-character string, you would still
want a way to extract individual code-points/bytes. Right now you have:

s[i..i] # substring starting at position i of length 1
s # code-point at position i

I think in future Ruby versions it is going to be something like:

s[i..i] # substring starting at position i of length 1
s # same as s[i..i]
s.byte(i) # code-point at position i

I'm guessing at String#byte. I know I read something about that but I
couldn't find a reference right away.

Anyway, as I understand it, the concept of 'character' or even
'position'
is pretty complicated in a fully i18n world (such as with Unicode).

Gary Wright
 
M

MonkeeSage

I think in future Ruby versions it is going to be something like:

In 1.9 it's #ord:

1.8:
'a'[0] # => 97
'a'[0,1] # => a
'a'[0..0] # => a

1.9:
'a'[0] # => a
'a'.ord # => 97

Regards,
Jordan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,213
Messages
2,571,109
Members
47,701
Latest member
LeoraRober

Latest Threads

Top