I need to select and prune an array of strings in Ruby and I have it partially working.
I have array of strings that looks like this:
321 com.apple.storeaccountd.daemon
- com.apple.cmio.AVCAssistant
- com.apple.diskmanagementd
150 com.apple.CodeSigningHelper
- com.apple.tailspind
197 com.apple.sysmond
- com.apple.storeassetd.daemon
160 com.apple.sandboxd
What I need to do is select only those entries that have an integer which the line below does:
launchctl_list.select! { |f| /^(?!- )/.match(f) }
This results in an array that looks like this:
321 com.apple.storeaccountd.daemon
150 com.apple.CodeSigningHelper
197 com.apple.sysmond
160 com.apple.sandboxd
I now just want to retain the 'last' part of each string in the array and I thought the following line would do the trick but doesn't.
launchctl_list.select! { |f| f.split(' ').last }
The strings in the array do not have their numbers dropped, what am I missing?
launchctl_list.select { |f| /^(?!- )/.match(f) }.map { |f| f.split(' ').last }is what you wantReturns a new array containing all elements of ary for which the given block returns a true value.vs ` Invokes the given block once for each element of self. Creates a new array containing the values returned by the block.`SELECTin SQL and a projection in Relational Algebra, is calledmapin Ruby (and most other languages),collectin Smalltalk (and Ruby), andtransformin C++. What Ruby (and Smalltalk) callsselectis calledWHEREin SQL, a selection or restriction in Relational Algebra, andfilteralmost anywhere else. Maybe it helps when you considerselect's aliasfind_all.