Can You Store Individual Letters?

I was wandering if it is possible to have user inputted words stored and
each letter as a seperate variable. I know i could just have each letter
inputted indvidually but thats time consuming on the users part. Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t

This is not the type of program I am looking to make, it's just an
example.

Thanks,
Scott

···

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

str = "Tomato"
num = 2

puts str[num-1, 1]

···

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

Scott Andrechek wrote:

I was wandering if it is possible to have user inputted words stored and
each letter as a seperate variable. I know i could just have each letter
inputted indvidually but thats time consuming on the users part. Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t

Check out String#split

irb(main):005:0> x = "tomato"
=> "tomato"
irb(main):006:0> y = x.split ''
=> ["t", "o", "m", "a", "t", "o"]
irb(main):007:0> y[0]
=> "t"
irb(main):008:0> y[5]
=> "o"

···

--
MagickWand for Ruby - http://magickwand.rubyforge.org/

Scott Andrechek wrote:

I was wandering if it is possible to have user inputted words stored and
each letter as a seperate variable. I know i could just have each letter
inputted indvidually but thats time consuming on the users part. Here's
an example in case you don't know what i mean:
Ex.
USER: Tomato
COMP: Type a number to have a letter displayed
USER: 2
COMP: o
USER: 5
COMP: t

Check out String#split

irb(main):005:0> x = "tomato"
=> "tomato"
irb(main):006:0> y = x.split ''
=> ["t", "o", "m", "a", "t", "o"]
irb(main):007:0> y[0]
=> "t"
irb(main):008:0> y[5]
=> "o"

--
MagickWand for Ruby - http://magickwand.rubyforge.org/

And if you unshift a value (like nil) into the first element, then you can use 1-based positions:

word = "Tomato".split('')

=> ["T", "o", "m", "a", "t", "o"]

word.unshift nil

=> [nil, "T", "o", "m", "a", "t", "o"]

word[2]

=> "o"

word[5]

=> "t"

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Sep 5, 2009, at 7:37 AM, Tim Hunter wrote: