Let's suppose that we class SomeClass.
class SomeClass < SomeClassThatAssignAttributesOnInitialization
attr_accessor :group
attr_accessor :name
end
And array of it instances
arr = [SomeClass.new(group:0, name:'one'),
SomeClass.new(group:0, name:'two'),
SomeClass.new(group:1, name:'three'),
SomeClass.new(group:1, name:'four')]
I need to put first element of each group into array.
Now it works next way
current_group = nil
first_elements = arr.map do |instance|
if current_group != instance.group
current_group = instance.group
instance.name
end
end
This works fine but i think that there is some kind of "Ruby" way to do it.
Can anybody help me?
Thanks