你可以試試這個方法
,如果你只想要該目錄的唯一目錄
>> Dir.entries(".").select {|f| File.directory? f}
=> [".", "..", "api_product", "connection_pool", "factory_girl",
e_api", "todo"]
現在刪除所有特殊字符
>> Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip
=> "api product connection pool factory girl grape lonely lonely batch mysql2 postgres rails rubocop salesforceheroku salesforceheroku2 sidekiq simple api todo"
我想你想的下方
irb(main):064:0* Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip
=> "api product connection pool factory girl grape lonely lonely batch mysql2 postgres rails rubocop salesforceheroku salesforceheroku2 sidekiq simple api todo"
irb(main):065:0> Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip
=> "api product connection pool factory girl grape lonely lonely batch mysql2 postgres rails rubocop salesforceheroku salesforceheroku2 sidekiq simple api todo"
irb(main):066:0> Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip.split(" ")
=> ["api product", "connection pool", "factory girl", "grape", "lonely", "lonely batch", "mysql2", "postgres", "rails", "rubocop", "salesforceheroku", "salesforceheroku2", "sidekiq", "simple api", "to
do"]
irb(main):067:0> Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip.split(" ").map{|c| "<p>"+c+"</p>"}
=> ["<p>api product</p>", "<p>connection pool</p>", "<p>factory girl</p>", "<p>grape</p>", "<p>lonely</p>", "<p>lonely batch</p>", "<p>mysql2</p>", "<p>postgres</p>", "<p>rails</p>", "<p>rubocop</p>",
"<p>salesforceheroku</p>", "<p>salesforceheroku2</p>", "<p>sidekiq</p>", "<p>simple api</p>", "<p>todo</p>"]
我在這裏單獨的兩個字是「」雙空間(如後刪除「的所有API產品和連接池_」將用雙空格隔開,這樣我可以用
和
它映射)
Dir.entries(".").select {|f| File.directory? f}.join(" ").gsub!(/[^0-9A-Za-z]/, ' ').strip.split(" ").map{|c| "<p>"+c+"</p>"}
外觀join
和split
都具有雙重空間
你不能。該方法不佔用塊。目前還不清楚你想要做什麼。 – sawa
更新了一下,我需要從輸出中刪除塊字符,引號,逗號和點 – ADL
您可以試試這個'Dir.entries('。'){| e | 「
#{e}
」} .map {| a |一個。gsub(/ [^ 0-9A-Za-z] /,'')}。reject(&:blank?)' –