[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

turning a string into array of ASCII bytes

David Garamond

11/26/2003 1:40:00 PM

What is the shortest, most straightforward way (without temporary
variables, etc)?

My best route is now "1234".split(//).collect{|c|c[0]} but I'm sure
there is a much better way. Also it's a bit slow.

--
dave



8 Answers

Stefan Scholl

11/26/2003 3:15:00 PM

0

On 2003-11-26 14:39:45, David Garamond wrote:

> What is the shortest, most straightforward way (without temporary
> variables, etc)?

Do you really need an array? You can access the ASCII codes of every
character in a string with [].

irb(main):001:0> a='David'
=> "David"
irb(main):002:0> a[0]
=> 68
irb(main):003:0> a[1]
=> 97

Mark J. Reed

11/26/2003 4:17:00 PM

0

On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:
> On 2003-11-26 14:39:45, David Garamond wrote:
>
> > What is the shortest, most straightforward way (without temporary
> > variables, etc)?
>
> Do you really need an array? You can access the ASCII codes of every
> character in a string with [].

Currently, that's so. However, I believe that such behavior is due to
be phased out in the future, in order to have subscripting be more consistent
(i.e. so that s[0] and s[0,1] return the same thing, as is already true
of arrays).

-Mark

Robert Klemme

11/26/2003 5:02:00 PM

0


"Mark J. Reed" <markjreed@mail.com> schrieb im Newsbeitrag
news:20031126161656.GA617@mulan.thereeds.org...
> On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:
> > On 2003-11-26 14:39:45, David Garamond wrote:
> >
> > > What is the shortest, most straightforward way (without temporary
> > > variables, etc)?
> >
> > Do you really need an array? You can access the ASCII codes of every
> > character in a string with [].
>
> Currently, that's so. However, I believe that such behavior is due to
> be phased out in the future, in order to have subscripting be more
consistent
> (i.e. so that s[0] and s[0,1] return the same thing, as is already true
> of arrays).

But you can be sure that then there will be another method (possibly
String#at(index)) that is equivalent to String#[index] of today.

robert

T. Onoma

11/26/2003 5:37:00 PM

0

On Wednesday 26 November 2003 06:07 pm, Robert Klemme wrote:

> > (i.e. so that s[0] and s[0,1] return the same thing, as is already true
> > of arrays).
>
> But you can be sure that then there will be another method (possibly
> String#at(index)) that is equivalent to String#[index] of today.

or

str[index].asc as the opposite of Integer#chr, perhaps?

i thought we already had this but i can't seem to find it so guess we don't.

-t0


Ara.T.Howard

11/26/2003 6:00:00 PM

0

Stefan Scholl

11/26/2003 6:38:00 PM

0

On 2003-11-26 17:16:56, Mark J. Reed wrote:
> On Wed, Nov 26, 2003 at 04:14:43PM +0100, Stefan Scholl wrote:
>> Do you really need an array? You can access the ASCII codes of every
>> character in a string with [].
>
> Currently, that's so. However, I believe that such behavior is due to
> be phased out in the future, in order to have subscripting be more consistent
> (i.e. so that s[0] and s[0,1] return the same thing, as is already true
> of arrays).

Who wants to use such a language? I don't want to rewrite my code
every few months.

If these tiny little methods aren't stable enough to be mentioned in
an answer then I'm really wrong here. :-(

Robert Klemme

11/26/2003 6:42:00 PM

0


"T. Onoma" <transami@runbox.com> schrieb im Newsbeitrag
news:200311260931.14413.transami@runbox.com...
> On Wednesday 26 November 2003 06:07 pm, Robert Klemme wrote:
>
> > > (i.e. so that s[0] and s[0,1] return the same thing, as is already
true
> > > of arrays).
> >
> > But you can be sure that then there will be another method (possibly
> > String#at(index)) that is equivalent to String#[index] of today.
>
> or
>
> str[index].asc as the opposite of Integer#chr, perhaps?

Inefficient since in the future str[index] would return a string (see the
other postings in the thread). It would be a bad idea to create a new
string just to get at the ascii value of it's first character.

> i thought we already had this but i can't seem to find it so guess we
don't.

Currently str[index] returns an int denoting the ascii value.

robert

Mark J. Reed

11/26/2003 6:49:00 PM

0

On Wed, Nov 26, 2003 at 07:38:13PM +0100, Stefan Scholl wrote:
> Who wants to use such a language? I don't want to rewrite my code
> every few months.

You don't have to. Ruby has evolved relatively slowly, and
*incompatible* changes such as this one have been few and far
between. This one hasn't been made yet, you'll notice, and there
will probably be a long period where the current behavior still
works but generates a warning. I wouldn't be surprised if the
final change were left out until Ruby 2.0 - and a major revision number
change means you're allowed to break things. :)

-Mark