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

str = “Tomato”
num = 2

puts str[num-1, 1]

Scott Andrechek wrote:

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”

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

COMP: Type a number to have a letter displayed
=> [“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:

irb> word = “Tomato”.split(’’)
=> [“T”, “o”, “m”, “a”, “t”, “o”]
irb> word.unshift nil
=> [nil, “T”, “o”, “m”, “a”, “t”, “o”]
irb> word[2]
=> “o”
irb> word[5]
=> “t”

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs