How to divide a two-dimensional array into four with ruby

Due to various reasons, I created a method that is profitable for everyone. I think the letters are refreshing, so I wrote a picture!

For example, suppose you have an arbitrary two-dimensional array like this. スクリーンショット 2020-12-26 14.20.49 2 2.png

I want to specify arbitrary coordinates as shown in (1,1) and divide it into four. スクリーンショット 2020-12-26 14.20.49.png

The code is here.

def divide_2d_array(array, x, y)
    temp1 = []
    temp2 = []
    array.each do |a|
        temp1 << a[0..y]
        temp2 << a[(y+1)..-1]
    end
    a,c = temp1[0..x], temp1[(x+1)..-1]
    b,d = temp2[0..x], temp2[(x+1)..-1]
    return a,b,c,d
end

#I will try to run
arr = [[1,2,3],
       [4,5,6],
       [7,8,9]]

p divide_2d_array(arr, 1, 1)
# =>[[[1, 2], [4, 5]], [[3], [6]], [[7, 8]], [[9]]]

It's done! !! !! Please let me know if there is a better way!

Recommended Posts

How to divide a two-dimensional array into four with ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
[Java] How to turn a two-dimensional array with an extended for statement
[Ruby] Extracting a two-dimensional array
[Ruby] How to split each GraphQL query into a file
How to change a string in an array to a number in Ruby
How to make a Java array
Try to imitate the idea of a two-dimensional array with a one-dimensional array
How to add a new hash / array
Ruby two-dimensional array
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to SSH into Ubuntu from a terminal with public key authentication
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Convert a string to a character-by-character array with swift
How to create pagination for a "kaminari" array
[Ruby] How to generate a random alphabet string
How to make a factory with a model with polymorphic association
How to use an array for a TreeMap key
How to make LINE messaging function made with Ruby
How to delete a new_record object built with Rails
How to manually generate a JWT with Rails Knock
How to launch another command in a Ruby program
How to output standard from an array with forEach
[How to insert a video in haml with Rails]
How to convert a file to a byte array in Java
How to query Array in jsonb with Rails + postgres
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
How to request by passing an array to the query with HTTP Client of Ruby
How to use a structure with variable length array or bit field in Ruby-FFI
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to take a screenshot with the Android Studio emulator
How to request a CSV file as JSON with jMeter
Java program to resize a photo into a square with margins
How to use a foreign key with FactoryBot ~ Another solution
How to use Ruby return
How to leave a comment
[For Ruby beginners] Explain how to freely delete array elements!
[Easy] How to automatically format Ruby erb file with vsCode
[Ruby] How to comment out
How to retrieve the hash value in an array in Ruby
Multiplication in a Ruby array
How to number (number) with html.erb
How to test a private method with RSpec for yourself
How to update with activerecord-import
How to move another class with a button action of another class.
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby] How to retrieve the contents of a double hash
Ruby: How to use cookies
How to output array values without using a for statement
How to add the same Indexes in a nested array
Mapping to a class with a value object in How to MyBatis
Think about how to divide MVC into M and V
[Ruby] How to write blocks
How to initialize Java array
How to insert a video
How to set up a proxy with authentication in Feign
How to create a method
How to register as a customer with Square using Tomcat
How to deal with different versions of rbenv and Ruby