Wali Chaudhary
Wali Chaudhary

Reputation: 177

Ruby 2.3. How can I convert User input string into an array, and then split it into individual elements?

Ruby noob here. I've been on this for a couple of hours while, but I can't figure out how to store user input, turn it into an array, and then split the array into separate elements.

puts "Hey! What's up?"

response = Array.new
response << gets.chomp

crypt = response.each_slice(3).to_a
print crypt

This is what is outputted:

Hey! What's up?
Nothin
[["Nothin"]]

This is what I want, but can't figure out:

[["N", "o", "t", "h", "i", "n"]]

I've looked at Ruby docs but got confused, so after several hours of trial and error I'm here. Thanks for the help!

Upvotes: 1

Views: 811

Answers (2)

This is how I'd do to get what you need:

puts "Hey! What's up?"

response = Array.new
input = gets.chomp

response << input.split("")

puts response.to_s

Running this code you'll get

Hey! What's up?
Nothin
[["N", "o", "t", "h", "i", "n"]]

Upvotes: 0

Sean W
Sean W

Reputation: 587

use:

puts "Hey! What's up?"

crypt = gets.chomp

p [crypt.split("")]

Upvotes: 1

Related Questions