Project

General

Profile

Misc #16096

each in each (multiple uses 'each')

Added by D1mon (Dim F) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
[ruby-core:94255]

Description

obj.each {|a|
    a.some_method1.each {|b|
        ... # comes here (enter)
    }
    a.some_method2.each {|c|
        ... # does not enter here
    }
}

# tried and that way. also does not go
for a in obj
    for b in a.some_method1
        ... # comes here
    end
    for c in a.some_method2
        ... # does not enter here
    end
end

help solve the problem. I’ve been suffering for 2 days.

Updated by jeremyevans0 (Jeremy Evans) over 1 year ago

  • Status changed from Open to Feedback

Can you explain what you are trying to do, and provide a self-contained example? It is not clear from the description or the code what you think the problem is. I can only guess that the object returned by a.some_method2 has an each method that does not yield when called.

Updated by shevegen (Robert A. Heiler) over 1 year ago

In ruby it is in general very rare that deeply nested (and multiple) .each
are necessary.

I recommend to you to use "pp" before you run the loop, such as:

pp obj

Perhaps you have an empty Array there - that happens to me sometimes
where I would otherwise get confused why .each is not running.

This is also why I love pp. :)

As Jeremy wrote, it is very likely to assume that something is
not quite right with .each for the object; or it is empty.

Updated by D1mon (Dim F) over 1 year ago

selenium

for el in driver.find_elements(xpath: <some_xpath>)
  for e1 in el.find_elements(xpath: ...)
    ...
  end
  for e2 in el.find_elements(xpath: ...)
    ...
  end
end

I need to do two searches in one pass

Updated by jeremyevans0 (Jeremy Evans) over 1 year ago

  • Status changed from Feedback to Rejected

D1mon (Dim F) wrote:

selenium

for el in driver.find_elements(xpath: <some_xpath>)
  for e1 in el.find_elements(xpath: ...)
    ...
  end
  for e2 in el.find_elements(xpath: ...)
    ...
  end
end

I need to do two searches in one pass

You probably want something like:

driver.find_elements(xpath: "...").each do |el|
  (el.find_elements(xpath: "...").to_a + el.find_elements(xpath: "...").to_a).each do |e1|
    # ...
  end
end

Alternatively, you need an xpath expression that matches both elements you want to select (not sure if that is possible).

As this is not a bug report, I'm going to close this.

Updated by D1mon (Dim F) over 1 year ago

this method is not suitable since the 3rd search (find_elements) is also used.
Are you saying this is a bug? you can do issue?

Also available in: Atom PDF