# Regualr expression question

**URL:** <https://www.ruby-forum.com/t/regualr-expression-question/165748>\
**Category:** Ruby\
**Created:** [April 17, 2009, 12:40am UTC](https://www.ruby-forum.com/t/regualr-expression-question/165748 "2009-04-17T00:40:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [April 17, 2009, 12:40am UTC](https://www.ruby-forum.com/t/regualr-expression-question/165748/1 "2009-04-17T00:40:44Z")

</div>

The “Ruby\_RegularExpression” sample application in the  
javapassion rubyonrails tutorial has this code

puts “----Regular Expression”  
puts a = “hello there”  
puts a[1] #=\> 101  
puts a[1,3] #=\> “ell”

How do you get 101 from a[1]

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [April 17, 2009, 1:04am UTC](https://www.ruby-forum.com/t/regualr-expression-question/165748/2 "2009-04-17T01:04:08Z")

</div>

On Apr 16, 2009, at 6:40 PM, Reg wrote:

> The “Ruby\_RegularExpression” sample application in the  
> javapassion rubyonrails tutorial has this code
> 
> puts “----Regular Expression”  
> puts a = “hello there”  
> puts a[1] #=\> 101  
> puts a[1,3] #=\> “ell”
> 
> How do you get 101 from a[1]

It’s the value associated with the character e in ASCII.

In irb:

> RUBY\_VERSION  
> =\> “1.8.6”
> 
> > a = ‘hello’  
> > =\> “hello”  
> > a[1]  
> > =\> 101  
> > ?e  
> > =\> 101  
> > a[1 ,1]  
> > =\> “e”

If you look at the ri documnetation for String#[] in Ruby 1.8.6 you  
can see:

## -------------------------------------------------------------- String#[] str[fixnum] =\> fixnum or nil str[fixnum, fixnum] =\> new\_str or nil str[range] =\> new\_str or nil str[regexp] =\> new\_str or nil str[regexp, fixnum] =\> new\_str or nil str[other\_str] =\> new\_str or nil […]

```
  Element Reference---If passed a single +Fixnum+, returns the code
  of the character at that position.

```

[…]

In Ruby 1.9 things change so that you always get a String or nil back  
from String#[]:

irb(main):001:0\> RUBY\_VERSION  
=\> “1.9.2”  
irb(main):002:0\> a = ‘hello’  
=\> “hello”  
irb(main):003:0\> a[1]  
=\> “e”  
irb(main):004:0\>

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [April 17, 2009, 1:26am UTC](https://www.ruby-forum.com/t/regualr-expression-question/165748/3 "2009-04-17T01:26:21Z")

</div>

On Thu, 16 Apr 2009 18:02:52 -0500, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > a[1]
> 
> ```
> of the character at that position.
> 
> ```
> 
> =\> “e”  
> irb(main):004:0\>
> 
> Hope this helps,
> 
> Mike

Its a great help!  
Thanks Mike
