[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string#[]

Greg Chagnon

9/26/2006 1:03:00 PM

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
8 Answers

Robert Klemme

9/26/2006 1:07:00 PM

0

On 26.09.2006 15:02, Newbie wrote:
> 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

Thomas Adam

9/26/2006 1:17:00 PM

0

On Tue, 26 Sep 2006 22:06:18 +0900
Newbie <none@none.com> wrote:

> 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

Greg Chagnon

9/26/2006 4:13:00 PM

0

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

Thomas Adam wrote:
>> 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
>

Greg Chagnon

9/26/2006 4:14:00 PM

0

Why not a 1-character string?

Robert Klemme wrote:
>> 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>

Paul Lutus

9/26/2006 4:38:00 PM

0

Newbie wrote:

> Why not a 1-character string?

A one-character string isn't a character, because a string has the
properties of an array. A character (if there were such a thing in Ruby)
cannot be expanded into two or more characters as a string can.

When you reference a string with a single index, you get back a character
code, but this is as close to a character or character class as exists in
Ruby.

--
Paul Lutus
http://www.ara...

James Gray

9/26/2006 5:03:00 PM

0

On Sep 26, 2006, at 11:15 AM, Newbie wrote:

> Why not a 1-character string?

Ruby will work this way in the future.

James Edward Gray II



Gary Wright

9/26/2006 6:30:00 PM

0


On Sep 26, 2006, at 12:15 PM, Newbie wrote:

> 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[i] # 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[i] # 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




MonkeeSage

9/27/2006 1:44:00 AM

0

gwtmp01@mac.com wrote:
> 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