Pairing Ruby Operators

Given the following Ruby instructions:

(Reading input and storing each word in an 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 could suggest a way to optimize this code, perhaps by chaining or removing some unnecessary assertions, because I have a feeling that much less code can be done, but since I'm new to Ruby, for me, to see how :)

Thank,

RM

+1


source to share


1 answer


gets.split

gotta get what you want



>> gets.split
this is a test
=> ["this", "is", "a", "test"]

      

+8


source







All Articles