ary = [“Crazy litte dog”, “Ungly little cat”, “Blue house on hill”]
ary.split(’ ')
I would like to pull first word of each item in array.
Would like the new array to look like this:
ary = [“Crazy”, “Ungly”, “Blue”]
ary = [“Crazy litte dog”, “Ungly little cat”, “Blue house on hill”]
ary.split(’ ')
I would like to pull first word of each item in array.
Would like the new array to look like this:
ary = [“Crazy”, “Ungly”, “Blue”]
Dansei,
Thanks for the answer. What if I would want to pull middle word? How
would that be done? Thanks
ary.map(&:split).each(&:pop).map(&:last)
…or better
ary.map{|w|w.split[1]}
ary.map(&:split).map(&:first)
Or to modify the original array
ary.map!(&:split).map!(&:first)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs