find duplicates in each array rails

hi,

<% @sp_references.each do |sp_ref| %>
<% sp_ref.all_references.each_with_index do |tax_ref, i| %>
<%if (tax_ref.reference.uniq) && (tax_ref.reference !~ /emend$/i) %>

<%= tax_ref.reference %>
<%end%>
<%end%>
<%end%>

This ‘uniq’ option to get distinct elements in tax_ref.reference is not working. It shows “undefined method `uniq’ for #<String:”. Kindly help me this regard to get the distinct elements from array using each in rails.

hi,

<% @sp_references.each do |sp_ref| %>
<% sp_ref.all_references.each_with_index do |tax_ref, i| %>
<%if (tax_ref.reference.uniq) && (tax_ref.reference !~ /emend$/i) %>
<%= tax_ref.reference %>
<%end%>
<%end%>
<%end%>

This 'uniq' option to get distinct elements in tax_ref.reference is not
working. It shows "undefined method `uniq' for #<String:". Kindly help me
this regard to get the distinct elements from array using each in rails.

Sounds like tax_ref.refrence is a string, not an array so calling uniq
on it doesn't make any sense.

Fred

Dear Fred,

Yes I accept, any other way to extract the distinct content from each array?

Which array? The "sp_ref.all_references" array that you're iterating,
or a different one?

Are you're asking "how would I get a collection of distinct
"reference" values from each collection of "sp_ref.all_references"?

If so this might help...

<% @sp_references.each do |sp_ref| %>
  <%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref
=~ /emend$/i}.join("<br/>") %>
<% end %>

... you could break it apart into separate operations if you wished.

Dear Michael,

Thanks lot for your answer.

<% @sp_references.each do |sp_ref| %>

<%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref

=~ /emend$/i}.join("
") %>

<% end %>

I need the results have to print without duplicates. This gives the results with duplicates.

<% @sp_references.each do |sp_ref| %>

<%= sp_ref.all_references.map(&:reference).delete_if{|ref| ref=~ /emend$/i}.join("
") %>
<% end %>

and

<% @sp_references.each do |sp_ref| %>

<%= sp_ref.all_references.map(&:reference).uniq.delete_if{|ref| ref=~ /emend$/i}.join("
") %>
<% end %>

presents same results with duplicates. Please about your suggestion.

I need the results have to print without duplicates. This gives the results
with duplicates.

<snip>

and

<snip>

presents same results with duplicates. Please about your suggestion.

I'm having trouble seeing where you're even trying to explain what you
have, and what you want....

Does this give what you're after? :
   <%= @sp_references.map(&:all_references).flatten.map(&:reference).uniq.delete_if{|ref|
ref=~ /emend$/i}.join("<br/>") %>

To be honest, I'm just stabbing in the dark, because you've not
explained anything about what these objects are made of or what actual
end result you want to achieve... some pseudo-code would help...