1

I have a multi-dimensional array of this form:

array = [["http://domain.com/product.html", 10], ["http://domain.com/product.html", 150], ["http://domain.com/product.html", 500]]

I need to delete all arrays that have the last value smaller than 150.

I already tried the following, but it doesn't seem to have any effect:

array.delete_if {|element| element.last < 150 }

Any help will be highly appreciated. Thanks.

3
  • 4
    Your one should work.. What output you got ? Commented Apr 3, 2014 at 9:45
  • It somehow gives me the same array, it doesn't delete any element from it. Commented Apr 3, 2014 at 9:55
  • 1
    The code you've posted works as expected: codepad.org/CvR9Ykfr Commented Apr 3, 2014 at 10:00

2 Answers 2

1

I would probably do it this way:

array.reject!{|x| x if x.last < 150}
Sign up to request clarification or add additional context in comments.

5 Comments

This is not an answer to this post,, It is another way to solve this.. Do you know why #delete_if didn't work in OP's example, which I am quite sure should work
I have no idea why it doesn't work, it just gives me the same multi-dimensional array, unchanged.
Perhaps the OP didn't refer to the return value, but referred to array again. That is the point of this answer.
I don't know why this answers has been entertained, which has not been asked.. OP didn't ask how to do it, while OP asked why not working
@user3493101 Although this answer misleading, correct way to write is array.reject!{|x| x.last < 150}. If you like it, teach yourself, this way.
0

You can also use this:

array.map{|f| f if f.last < 150}.compact

I don't know if it is better or not than Akarsh, just another solution that I would have used. Anyways, your solution works, user3493101, but if it doesn't, you can still use that.

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.