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


Groups > comp.lang.ruby > #5041 > unrolled thread

String Element Reference

Started byjason solomon <solomon.jas@gmail.com>
First post2011-05-25 12:23 -0500
Last post2011-05-25 13:35 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#5041 — String Element Reference

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 12:23 -0500
SubjectString Element Reference
Message-ID<f16342a93e87a7dafcd54bd5776d1c38@ruby-forum.com>
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?

Ideas / help would be greatly appreciated.

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

[toc] | [next] | [standalone]


#5043

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-05-25 19:29 +0200
Message-ID<944sjtFe5mU1@mid.individual.net>
In reply to#5041
On 25.05.2011 19:23, jason solomon wrote:
> 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?
>
> Ideas / help would be greatly appreciated.

This has changed in Ruby 1.9.  You probably read 1.9 documentation 
that's why you expect a[0] to return "h".

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#5044

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 12:34 -0500
Message-ID<e6a09560f8192eaae6132d75cd2f0ea5@ruby-forum.com>
In reply to#5041
Thanks, appreciate the help.

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

[toc] | [prev] | [next] | [standalone]


#5045

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-25 12:40 -0500
Message-ID<9d9424e5d54086e522ca388a1f1c3faa@ruby-forum.com>
In reply to#5041
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/.

[toc] | [prev] | [next] | [standalone]


#5047

FromJavier Hidalgo Villegas <cooljavy@hotmail.com>
Date2011-05-25 13:35 -0500
Message-ID<SNT133-w27B2DCEF3D615B0CA6251AC4740@phx.gbl>
In reply to#5041
That is the way String indexing works in Ruby, if you only pass one parameter you'll get the ASCII value of the character. If you would like to get "h" you must do  a[0,1] where the first parameter tells the parser to get the first character of the String and the second parameter tells the number of characters you want to get from that position.
Hope this helps
Javier Hidalgo




> Date: Thu, 26 May 2011 02:23:38 +0900
> From: solomon.jas@gmail.com
> Subject: String Element Reference
> To: ruby-talk@ruby-lang.org
> 
> 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?
> 
> Ideas / help would be greatly appreciated.
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
 		 	   		  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web