Jak usunąć puste elementy z tablicy?

Mam następującą tablicę

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"]

Chcę usunąć puste elementy z tablicy i uzyskać następujący wynik:

cities = ["Kathmandu", "Pokhara", "Dharan", "Butwal"]

Czy jest jakaś metoda jak compact, która zrobi to bez pętli?

 232
Author: the Tin Man, 2011-05-04

16 answers

Istnieje wiele sposobów, aby to zrobić, jeden jest reject

noEmptyCities = cities.reject { |c| c.empty? }

Można również użyć reject!, który zmodyfikuje cities w miejscu. Zwróci cities jako wartość zwracaną, jeśli coś odrzuci, lub nil jeśli nie zostaną odrzucone. To może być gotcha, jeśli nie jesteś ostrożny (dzięki ninja08 za wskazanie tego w komentarzach).

 434
Author: Matt Greer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-10 03:54:38
1.9.3p194 :001 > ["", "A", "B", "C", ""].reject(&:empty?)

=> ["A", "B", "C"]
 146
Author: user2010324,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-15 17:51:59

W moim projekcie używam delete:

cities.delete("")
 51
Author: esio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-21 21:47:06

Oto, co dla mnie działa:

[1, "", 2, "hello", nil].reject(&:blank?)

Wyjście:

[1, 2, "hello"]
 49
Author: kimerseen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-18 15:28:30

Kiedy chcę uporządkować taką tablicę używam:

["Kathmandu", "Pokhara", "", "Dharan", "Butwal"] - ["", nil]

Spowoduje usunięcie wszystkich pustych lub zerowych elementów.

 37
Author: superluminary,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-04 12:05:02

Spróbuj tego:

puts ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"] - [""]
 20
Author: Raels,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-30 00:27:06

Najbardziej Wyraźne

cities.delete_if(&:blank?)

Spowoduje usunięcie zarówno wartości nil, jak i pustych łańcuchów ("").

Na przykład:

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal", nil]

cities.delete_if(&:blank?)
# => ["Kathmandu", "Pokhara", "Dharan", "Butwal"]
 19
Author: phlegx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-14 20:18:25

Użycie reject:

>> cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].reject{ |e| e.empty? }
=> ["Kathmandu", "Pokhara", "Dharan", "Butwal"]
 15
Author: the Tin Man,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-15 17:42:41
cities.reject! { |c| c.blank? }

Powodem, dla którego chcesz użyć blank? nad empty? jest to, że puste rozpoznaje zero, puste ciągi i białe spacje. Na przykład:

cities = ["Kathmandu", "Pokhara", " ", nil, "", "Dharan", "Butwal"].reject { |c| c.blank? }

By jeszcze powrócić:

["Kathmandu", "Pokhara", "Dharan", "Butwal"]

I wywołanie empty? na " " zwróci false, którym prawdopodobnie chcesz być true.

Uwaga: blank? jest dostępna tylko przez Rails, Ruby obsługuje tylko empty?.

 14
Author: Colton Fent,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-15 17:49:53

Jest już wiele odpowiedzi, ale tutaj jest inne podejście, jeśli jesteś w świecie Rails:

 cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].select &:present?
 10
Author: Naveed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-21 21:46:27

Oto jeszcze jedno podejście, aby to osiągnąć

Możemy użyć presence z select

cities = ["Kathmandu", "Pokhara", "", "Dharan", nil, "Butwal"]

cities.select(&:presence)

["Kathmandu", "Pokhara", "Dharan", "Butwal"]
 9
Author: Sampat Badhe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-05 05:48:03

Oto rozwiązanie, jeśli masz mieszane typy w tablicy:

[nil,"some string here","",4,3,2]

Rozwiązanie:

[nil,"some string here","",4,3,2].compact.reject{|r| r.empty? if r.class == String}

Wyjście:

=> ["some string here", 4, 3, 2]
 8
Author: Francois,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-15 17:51:33

Możesz spróbować tego

 cities.reject!(&:empty?)
 4
Author: anusha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-05 06:31:54
 cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].delete_if {|c| c.empty? } 
 2
Author: suren,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-04 06:00:00

Najkrótsza droga cities.select(&:present?)

 1
Author: Javier Segovia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-28 22:16:12

Update with a strict with join & split

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"]
cities.join(' ').split

Wynik będzie:

["Kathmandu", "Pokhara", "Dharan", "Butwal"]

Zauważ, że: to nie działa z miastem ze spacjami

 -1
Author: Hieu Pham,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-02 05:03:05