Skip to main content
Code Review

Return to Question

edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm having a bit of trouble trying to find a more Rubyist way to achieve the following. Essentially, I want to try and iterate over every element e and apply e.method(n) for every n ∈ array\$n \in \text{array}\$, n ≠ e\$n \ne e\$. In order to determine whether or not n = e\$n = e\$, I'll have to use an index comparison (really just test for reference equality as opposed to functional equality.) This is the implementation I have so far:.

arr = [413, 321, 654, 23, 11]
(0...arr.length).each do |outer_i|
 (0...arr.length).each do |inner_i|
 next if outer_i == inner_i
 arr[outer_i].apply arr[inner_i]
 end
end

This reeks of Java/C++ and I can tell that this is not the Ruby way, but I can't seem to find an alternative. Any ideas to improve its Ruby-ness? I was thinking of Array#product but I'm not sure where to go from there.

I'm having a bit of trouble trying to find a more Rubyist way to achieve the following. Essentially, I want to try and iterate over every element e and apply e.method(n) for every n ∈ array, n ≠ e. In order to determine whether or not n = e I'll have to use an index comparison (really just test for reference equality as opposed to functional equality.) This is the implementation I have so far:

arr = [413, 321, 654, 23, 11]
(0...arr.length).each do |outer_i|
 (0...arr.length).each do |inner_i|
 next if outer_i == inner_i
 arr[outer_i].apply arr[inner_i]
 end
end

This reeks of Java/C++ and I can tell that this is not the Ruby way, but I can't seem to find an alternative. Any ideas to improve its Ruby-ness? I was thinking of Array#product but I'm not sure where to go from there.

I'm having a bit of trouble trying to find a more Rubyist way to achieve the following. Essentially, I want to try and iterate over every element e and apply e.method(n) for every \$n \in \text{array}\$, \$n \ne e\$. In order to determine whether or not \$n = e\$, I'll have to use an index comparison (really just test for reference equality as opposed to functional equality).

arr = [413, 321, 654, 23, 11]
(0...arr.length).each do |outer_i|
 (0...arr.length).each do |inner_i|
 next if outer_i == inner_i
 arr[outer_i].apply arr[inner_i]
 end
end

This reeks of Java/C++ and I can tell that this is not the Ruby way, but I can't seem to find an alternative. Any ideas to improve its Ruby-ness? I was thinking of Array#product but I'm not sure where to go from there.

edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Rubyist Way of Nesting Loopsloops on Same Arraysame array but Skipping Same Elementskipping same element

Source Link
Michael
  • 237
  • 1
  • 5

Rubyist Way of Nesting Loops on Same Array but Skipping Same Element

I'm having a bit of trouble trying to find a more Rubyist way to achieve the following. Essentially, I want to try and iterate over every element e and apply e.method(n) for every n ∈ array, n ≠ e. In order to determine whether or not n = e I'll have to use an index comparison (really just test for reference equality as opposed to functional equality.) This is the implementation I have so far:

arr = [413, 321, 654, 23, 11]
(0...arr.length).each do |outer_i|
 (0...arr.length).each do |inner_i|
 next if outer_i == inner_i
 arr[outer_i].apply arr[inner_i]
 end
end

This reeks of Java/C++ and I can tell that this is not the Ruby way, but I can't seem to find an alternative. Any ideas to improve its Ruby-ness? I was thinking of Array#product but I'm not sure where to go from there.

lang-rb

AltStyle によって変換されたページ (->オリジナル) /