Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #5045

Re: String Element Reference

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail
From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: String Element Reference
Date Wed, 25 May 2011 12:40:52 -0500
Organization Service de news de lacave.net
Lines 39
Message-ID <9d9424e5d54086e522ca388a1f1c3faa@ruby-forum.com> (permalink)
References <f16342a93e87a7dafcd54bd5776d1c38@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1306345267 94698 65.111.164.187 (25 May 2011 17:41:07 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Wed, 25 May 2011 17:41:07 +0000 (UTC)
In-Reply-To <f16342a93e87a7dafcd54bd5776d1c38@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 383755
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <9d9424e5d54086e522ca388a1f1c3faa@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:5045

Show key headers only | View raw


jason solomon wrote in post #1000974:
> I am new to Ruby so I apologize if this seems dumb.
>
> I am currently running ruby 1.8.7 (2009-06-12 patchlevel 174)
> [universal-darwin10.0].
>
> When working in an irb session I'm having trouble with String element
> reference.
>
> Example:
>
> a = "hello"
>
> a[0] should return "h", but instead is returning 104.
>
> Seems like it's return the ascii value of the element, but why?
>
>
> When I do a[0,3] the return is the expected "hel".
>
>
>
>
> Any ideas as to why a[0] is returning a FixNumb object?
>

For some reason Matz thought it was more useful to define the [] method 
for strings to return the ascii value.  As you discovered, if you 
specify a length as the second argument, you get the characters. 
Applying that knowledge, if you specify a length of 1, you will get the 
character.  So the expression:

str[0,1]

will return the character in both ruby 1.8 and ruby 1.9.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

String Element Reference jason solomon <solomon.jas@gmail.com> - 2011-05-25 12:23 -0500
  Re: String Element Reference Robert Klemme <shortcutter@googlemail.com> - 2011-05-25 19:29 +0200
  Re: String Element Reference jason solomon <solomon.jas@gmail.com> - 2011-05-25 12:34 -0500
  Re: String Element Reference 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 12:40 -0500
  Re: String Element Reference Javier Hidalgo Villegas <cooljavy@hotmail.com> - 2011-05-25 13:35 -0500

csiph-web