Reputation: 648
I have an array of ActiveRecord objects where Object.word is a string.
I know that
array.uniq{|s| s.word}
will return the objects with unique :word values.
I also want to make an extra condition (using uniq if possible) so that no word is one character away from another: specifically, I don't want to return objects with both the words "know" and "knows", or "weigh" and "weighs" but only to choose one of them.
Is there a neat, intuitive way to achieve this?
Upvotes: 0
Views: 1493
Reputation: 4633
You can use singularize
in Rails to get the singular value of the word, and still use your uniq
code:
array.uniq{|s| s.word.singularize}
[2] pry(main)> ["test", "tests", "word", "words", "entity", "entities"].uniq{|s| s.singularize}
=> ["test", "word", "entity"]
Upvotes: 1
Reputation: 11137
You can use reject to reject some elements depend on some conditions for example:
[1,2,3].reject {|a| a > 1} # [1]
Upvotes: 2