Forum: Ruby .split(" ") but splits all chars

0d2c3ee10ce0a8345f4fb7d8df3bc4ed?d=identicon&s=25 Stoney Mysels (stoneynine)
on 2014-07-27 01:15
Okay, when I split a string at every space, it does what's expected. But
when I call the array at x (array[x]) it only shows the first letter
rather than the part of the string I split. Let me just show you what
the problem is...

a = "one two three"
=> "one two three"
a.split(" ")
=>["one", "two", "three"]
num = 0
=> 0
while num < a.length
puts a[num]
num = num + 1
end
o
n
e

t
w
o

t
h
r
e
e
=> nil

Can someone explain to me why this is happening? Why isn't it showing
this instead?

"one
two
three
=> nil"
0d2c3ee10ce0a8345f4fb7d8df3bc4ed?d=identicon&s=25 Stoney Mysels (stoneynine)
on 2014-07-27 01:20
Let me add that I was unable to find anything on google to help me
because I didn't know how to word my question.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-07-27 03:07
> a = "one two three"
> => "one two three"
> a.split(" ")
> =>["one", "two", "three"]
>

a.split create an array, which must be affected to something. a
remain unchanged :
> b=a.split(" ")
> p b
["one", "two", "three"]
> p a
"one two three"
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.