3 Replies - 908 Views - Last Post: 04 September 2013 - 01:21 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

weird output

Posted 04 September 2013 - 12:58 PM

Hello there. I am new to coding and am doing code quizzes online. I have been instructed that "string"[-1] will return the last letter of the string. However, terminal seems to be returning some integer that is associated with the last letter instead of the letter itself. For example, "string"[-1] returns an output of 103. Can someone fill me in on the meaning of this? Thank you very much for your time.
Is This A Good Question/Topic? 1
  • +

Replies To: weird output

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,209
  • Joined: 12-May 09

Re: weird output

Posted 04 September 2013 - 01:10 PM

In pre 1.9 Ruby, using [] on a string returned "The code of the character".

I think it's the ascii code, as 103 corresponds to 'g'.

In 1.9, it will return the actual character.

If you're stuck in 1.8 land, you can use 103.chr to get the actual character.
Was This Post Helpful? 2
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: weird output

Posted 04 September 2013 - 01:15 PM

Thank you very very much. I am currently using ruby v.1.8.7. I will try to figure out how to download 1.9. Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: weird output

Posted 04 September 2013 - 01:21 PM

View Postxclite, on 04 September 2013 - 10:10 PM, said:

I think it's the ascii code, as 103 corresponds to 'g'.


Doesn't have to be ASCII - it depends on what encoding the string is in.

Quote

In 1.9, it will return the actual character.


That is a string containing a single character. There's no such thing as a character type in Ruby, which is why the decision to return an int before 1.9 was justifiable in the first place.</nitpick>


View Postheaphyg, on 04 September 2013 - 10:15 PM, said:

I will try to figure out how to download 1.9. Thank you for your time.


I'd go straight to 2.0 if I were you. That shouldn't be an issue since the compatibility issues from 1.9 to 2.0 are much more tame than those from 1.8 to 1.9. So if you can use 1.9, you very likely could use 2.0 as well.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1