3

I'm mapping a array with this code:

url = "http://www.cnn.com"
page = Mechanize.new.get(url)
images_url = page.images.map{|img| img.url.to_s if (img.width.to_i > 200)}

I get this result in console:

[nil, "http://i2.cdn.turner.com/cnn/dam/assets/110929092349-nelson-mandela-t1-main.jpg", nil, nil, nil, nil, nil, "http://i2.cdn.turner.com/cnn/dam/assets/120225123812-syria-mani-2-c1-main.jpg", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "http://i2.cdn.turner.com/cnn/dam/assets/120225022127-blue-fish-bin-tease.jpg", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

I want get this array without nil values, its mean, I want get a array sth like:

["http://i2.cdn.turner.com/cnn/dam/assets/110929092349-nelson-mandela-t1-main.jpg", "http://i2.cdn.turner.com/cnn/dam/assets/120225123812-syria-mani-2-c1-main.jpg", "http://i2.cdn.turner.com/cnn/dam/assets/120225022127-blue-fish-bin-tease.jpg"]

For this case, 3 values.

Thank you

2 Answers 2

9

You can call the compact method on your Array to remove empty results from it.

images_url = page.images.map{|img| img.url.to_s if (img.width.to_i > 200)}.compact
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Matteo could you put the example for this case?. Thank you!
sure, I've just added it to the answer
images_url = page.images.map{|img| img.url.to_s if (img.width.to_i > 200)}.compact!
@hyperrjas You don't want compact!. If there are no nil values in the array, compact! will return nil instead of the list of URLs. You probably want compact (i.e. without the !, as in Matteo's example).
nice. i have been using <code>reject{|i| i.nil?}</code> till now
0

You can also use Array#delete if you ever want to remove things other than nil, without modifying the receiver (add an bang [!] if you want do want it to.

Or if you want to delete elements based on something other than if it matches, you can use Array#delete_if. It iterates over each element with a block you provide and removes the element if the return value of that block evaluates to true.

Here are some examples of both:

    ary = [42, nil, "foo", "bar", "foo", Object.new, self, /hi/]
    ary.delete("foo")  #=> [42, nil, 'bar', #<Object:xxxxx&rt, main, /hi/]
    ary                #=> [42, nil, "foo", "bar", "foo", Object.new, self, /hi/]
    ary.delete_if {|elem|
      elem.class == Object or
      elem.kind_of?(Regexp)
    }                  #=> [42, nil, "foo", "bar", "foo"]

Hope this helps! :)

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.