1

This following Code doesn't work, the Problem is inside of the calc_fitness method, the each block does not return an value, and i dont know why

# takes an array, splices them into groups of 2 and returns a sum of values read from the matrix
def calc_fitness(hypothesis_arr)
   hypothesis_arr.each_slice(2).to_a.map{|v| $distances[v.first,v.last]}
end

def main

  # filling the matrix, with random values
  $distances = create_sym_matrix
  p calc_fitness((0..99).to_a) 
end

main
# => [67,67,67,67....67,] #these should not be the same, which means i  the block alway returns the same value. Why?
2
  • everything in ruby returns a value. it just so happens the return value for main is nil because the return value from hypothesis_arr.each_slice(2).each{|v| $distances[v.first,v.last]} is nil. You can recreate this by simply (0..99).to_a.each_slice(2).each{} #=> nil Commented Nov 7, 2017 at 15:18
  • Be extremely careful when using global variables like $distances, which is what the $ prefix means. These can render otherwise ordinary code completely unmaintainable in a hurry. Likewise there is no reason for a main method, Ruby already has a main context for this purpose, that is code outside of any class, module or method definition. Commented Nov 7, 2017 at 19:16

1 Answer 1

1

This happens because each returns itself (an instance of Enumerator) and each_slice return nil.

https://ruby-doc.org/core/Enumerable.html#method-i-each_slice

You could try changing the each to a map

To simplify and get a visual of what was generated, I used:

(1..99).to_a.each_slice(2).map { |v| { first: v.first, last: v.last } }
=> [{:first=>1, :last=>2}, {:first=>3, :last=>4}, {:first=>5, :last=>6}, {:first=>7, :last=>8}, {:first=>9, :last=>10}, {:first=>11, :last=>12}, {:first=>13, :last=>14}, {:first=>15, :last=>16}, {:first=>17, :last=>18}, {:first=>19, :last=>20}, {:first=>21, :last=>22}, {:first=>23, :last=>24}, {:first=>25, :last=>26}, {:first=>27, :last=>28}, {:first=>29, :last=>30}, {:first=>31, :last=>32}, {:first=>33, :last=>34}, {:first=>35, :last=>36}, {:first=>37, :last=>38}, {:first=>39, :last=>40}, {:first=>41, :last=>42}, {:first=>43, :last=>44}, {:first=>45, :last=>46}, {:first=>47, :last=>48}, {:first=>49, :last=>50}, {:first=>51, :last=>52}, {:first=>53, :last=>54}, {:first=>55, :last=>56}, {:first=>57, :last=>58}, {:first=>59, :last=>60}, {:first=>61, :last=>62}, {:first=>63, :last=>64}, {:first=>65, :last=>66}, {:first=>67, :last=>68}, {:first=>69, :last=>70}, {:first=>71, :last=>72}, {:first=>73, :last=>74}, {:first=>75, :last=>76}, {:first=>77, :last=>78}, {:first=>79, :last=>80}, {:first=>81, :last=>82}, {:first=>83, :last=>84}, {:first=>85, :last=>86}, {:first=>87, :last=>88}, {:first=>89, :last=>90}, {:first=>91, :last=>92}, {:first=>93, :last=>94}, {:first=>95, :last=>96}, {:first=>97, :last=>98}, {:first=>99, :last=>99}]

Now at this point I'm not sure what that function is doing that is assigned to $distances. You might want to provide the code for that or give some more detail on what your are attempting to accomplish.

Sign up to request clarification or add additional context in comments.

5 Comments

You are right, i chaged it and now the problem is a different. Ill explain above
Updated my response, also I noticed you had another to_a in there right before map. You shouldn't need that.
the matrix just contains random values
Actually, each returns self by convention, but you are right that each_slice returns nil.
So does the map-function only return the first value, as array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.