I have this .map function that returns all the employers that are enabled.
::Employer.enabled.ordered.map do |e|
I need to filter out employers that == "some text"
I tried this but it isn't giving me the result I'm looking for:
::Employer.enabled.ordered.map do {|e| e.name == "some text" ? nil : [e] }.compact
I'm new to Ruby and not great at syntax, what am I missing?
CodePudding user response:
There is a select method available in ruby
a = [:foo, 'bar', 2, :bam]
a1 = a.select {|element| element.to_s.start_with?('b') }
a1 # => ["bar", :bam]
For your specific use case (Only keep items where e does not equal some text):
::Employer.enabled.ordered.select do {|e| e != "some text" }
Other options would be select!, reject! or reject. Each of them comes with a different behavior, but they are used in order to filter.
CodePudding user response:
Ruby has a nice Enumerable method for this scenario: reject.
::Employer.enabled.ordered.reject { |e| e.name == "unwanted-name" }
