tags:

views:

130

answers:

1

Hey all,

Given following Ruby statements:

(Read input and store each word in array removing spaces between words etc)

input = gets.chomp
inArr = []
input.strip.each (" ") { |w| inArr.push w }
inArr.delete_if {|ele| ele == " "}
inArr.each {|w| w.strip!}

I was wondering if anyone can suggest a way to optimize this code, maybe via chaining or removing some unneeded statements, because i have a feeling this can be done in much less code, but since I'm new to Ruby its hard for me to see how :)

Thank,

RM

+8  A: 

gets.split should get you what you want

>> gets.split
this is a test
=> ["this", "is", "a", "test"]
Aaron Hinni
thnks for the speedy replay... i guess there is a better way :)missed this method in the docs .. :(
Roman M