0

if I get from params something like a 165. and this is number contains in array number 3 of 9. how to get this number of the array?

range = 50..450
cutter = range.last/50
b = range.each_slice(range.last/cutter).to_a

b #=> [[50, 51,..., 99],..., [400, 401,..., 450]]

How to get it?

5
  • In your code, the variable a is undefined. Also, i don't understand what b is supposed to represent, since the expression [[50..99]..[400..450]] is not valid Ruby (the lower/upper bound of a range can't be an array). Commented Jan 23, 2020 at 10:22
  • @user1934428 sorry, i updated. b in my example return array of 9 arrays. each array have a 50 numbers, Commented Jan 23, 2020 at 10:25
  • Even after the update, the two problems I mentioned in my comment are still there ...... Did you ever actually execute the code you posted? Please write a complete example, which we can reproduce in irb, describe the output you receive, and the output you would like to see. Commented Jan 23, 2020 at 10:28
  • @user1934428 yep. try it again. Commented Jan 23, 2020 at 10:30
  • Your code produces the array b #=> [[50, 51,..., 99],..., [400, 401,..., 449], [450]]. Commented Jan 24, 2020 at 17:47

1 Answer 1

2

The array method you want is find_index. I think this will do what you want:

b.find_index {|a| a.include?(165)}
Sign up to request clarification or add additional context in comments.

1 Comment

@CarySwoveland I think you are wrong. I've just tried it and get NoMethodError (undefined method 'include?' for 50:Integer). find_index iterates over the outer arrays in the block, so a is always one of the inner arrays.

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.