4 Replies - 1072 Views - Last Post: 26 June 2014 - 03:17 AM Rate Topic: -----

#1 Tinker_Jobber_Hutt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-June 13

Question about the brackets

Posted 15 April 2014 - 05:04 AM

I was doing a example from the book Ruby in A Day.
The example is this
my_string = "Ruby in A Day"

# Print first character  in string
puts my_string[0]

# Print fourth character in string
puts my_string[3]


then you run it the output is suppose to be
R y

but it's 82 121. Has there been a update to Ruby that changes this or the book wrong on this example?

Thanks

This post has been edited by xclite: 15 April 2014 - 05:07 AM
Reason for edit:: Adding code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Question about the brackets

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,180
  • Joined: 12-May 09

Re: Question about the brackets

Posted 15 April 2014 - 05:06 AM

The book is right. Which version of Ruby are you running? I think this changed in the move to 1.9. 1.8 returns the ordinal value of the character when indexing on strings. 1.9 returns the character.

This post has been edited by xclite: 15 April 2014 - 05:06 AM

Was This Post Helpful? 3
  • +
  • -

#3 bhensley  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 8
  • Joined: 18-April 14

Re: Question about the brackets

Posted 18 April 2014 - 02:20 PM

View PostTinker_Jobber_Hutt, on 15 April 2014 - 05:04 AM, said:

I was doing a example from the book Ruby in A Day.
The example is this
my_string = "Ruby in A Day"

# Print first character  in string
puts my_string[0]

# Print fourth character in string
puts my_string[3]


then you run it the output is suppose to be
R y

but it's 82 121. Has there been a update to Ruby that changes this or the book wrong on this example?

Thanks


Ruby Integers have a method (chr) that will return the corresponding character. In your example, you'd want to do:

my_string = "Ruby in A Day"

# Print first character  in string
puts my_string[0].chr

# Print fourth character in string
puts my_string[3].chr



I would compare the version of Ruby you're working with locally to that which the book is using. Sounds to me you're in need of an update (if it's feasible).
Was This Post Helpful? 2
  • +
  • -

#4 konami_code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 14

Re: Question about the brackets

Posted 11 May 2014 - 11:21 AM

Wow totally had this issue as well and this helped me thanks as well!
Was This Post Helpful? 0
  • +
  • -

#5 Echo`  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-June 14

Re: Question about the brackets

Posted 26 June 2014 - 03:17 AM

Ruby has CHR method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1