20

How can I delete some elements from an array and select them?

For example:

class Foo 
  def initialize
    @a = [1,2,3,4,5,6,7,8,9]
  end

  def get_a
    return @a
  end
end

foo = Foo.new
b = foo.get_a.sth{ |e| e < 4 }
p b # => [1,2,3]
p foo.get_a # => [4,5,6,7,8,9,10]

What I can use instead of foo.get_a.sth?

4
  • Your instance variable @a on the class Foo is not doing anything. You should remove it. Commented Jul 31, 2013 at 14:58
  • Question is abstracted and detail is removed. you can suppose that line @a = [1,2,3,4,5,6,7,8,9] is the result of a some process. Commented Jul 31, 2013 at 15:04
  • I'm sorry!! I'm used to java form!!! :p Commented Jul 31, 2013 at 15:10
  • where's the abstractFactoryFactory? jk, but seriously ruby is a different culture than java, please use clearer examples and maybe some reference to real world objects and so forth. Hard to read this. Commented Apr 17, 2014 at 21:14

5 Answers 5

35

If you don't need to retain the object id of a:

a = [1,2,3,4,5,6,7,8,9,10]
b, a = a.partition{|e| e < 4}
b # => [1, 2, 3]
a # => [4, 5, 6, 7, 8, 9, 10]

If you do need to retain the object id of a, then use a temporal array c:

a = [1,2,3,4,5,6,7,8,9,10]
b, c = a.partition{|e| e < 4}
a.replace(c)
Sign up to request clarification or add additional context in comments.

4 Comments

nice answer, but i can not change the a. a is result of a some_object.get_some_array function
@NewMrd Why can't you change it? A variable is a variable, so you can always assign one to be the result of that call, then this pattern works fine.
Can you not just use a.select { |e| e < 4 } in that case? This will return the elements that pass the test.
tanks @sawa. i used some_object.set_some_array c instead of a.replace c.
4

Rails 6 now has this:

a = [1, 2, 3]
#=> [1, 2, 3]
a.extract! { |n| n.even? }
#=> [2]
a
#=> [1, 3] 

Comments

2

If you were only deleting one item, this doesn't require duplicating the array, etc:

array = [{ id: 1 }, { id: 2 }, {id: 3 }]
array.delete_at(array.find_index { |element| element[:id] == 1 })
#=> {:id=>1} 

2 Comments

This is the perfect solution to my application for similar need.
this will fail if find_index returns nil
1
a = [1, 2, 3, 4]
a.dup - (a.delete_if(&:even?))
#=> [2, 4]

a
#=> [1, 3]

a = [1, 2, 3, 4]
b = a.dup - (a.delete_if { |e| e < 4 })
a
#=> [4]
b
#=> [1, 2, 3]

Edit: It sounds like you are just after #select...

a = [1, 2, 3, 4]
a.select { |e| e < 3 }
#=> [1, 2]

2 Comments

If there are duplicate elements then the block would return the same for both of them so this isn't something that would be a problem!
Can you provide an example?
0

I still don't believe ruby doesn't have something for this in its default libraries. There should be #drop method equivalent for this. Surely there's a gem available that would add the functionality to the array class. But who needs gems when you can just break out your own scripts:

#in initializer somewhere
class Array
  def exclude(obj)
    x = self
    x.delete(obj)
    x
  end
end

I may submit a pull request to the ruby github project for this method. This superman-patch works very well so far.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.