6 ms·
For those playing along at home, if you find yourself parsing the output of ls in any language, you are doing it wrong (tm). First doing so execs a program rat
by onedognight 15y ago
For those playing along at home, if you find yourself parsing the output of ls in any language, you are doing it wrong (tm). First doing so execs a program rather than just making a function call, and second you are trusting the filenames to not contain special characters, in this case newlines (unlikely, I know, but why not be robust). The function the author was looking for is glob (and it exists in ruby, perl, shell, C, etc).
Dir.glob('/Applications/*/Contents/Info.plist') do |f|
puts f
end
Shorter and better.
- mceachen 15y agoRight, `ls` could be shrinkwrapped. But then so could the entire ruby environment. You're missing an each after your glob. Also -- did you know about [] alias for glob? Dir['/Applications/*/Contents/Info.plist'].each do |f| ... end
- onedognight 15y agoThanks, I didn't know about [] as an alias for glob(). Also, while you do need the .each in the [] version you don't need it in the glob() version.