Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:45+00:00 2026-06-13T03:40:45+00:00

I am trying to write a loop that takes in a nested array and

  • 0

I am trying to write a loop that takes in a nested array and makes a sub-array of two consecutive tuples at a time. The input array may be something like this

arr = [['A','B'],['C','D'],['E','F'],['G','H'],['I','J'],['K','L'],
      ['M','N'],['O','P']]

Output: ['A','B'],['C','D']
        ['E','F'],['G','H']
        ['I','J'],['K','L']
        ['M','N'],['O','P']

I have tried out various loops, like

arr.each_slice(2) do |k,m|
   new_arr=[k,m]
   puts new_arr
end 

and

 arr.each_slice(2) { |k,m| puts(k,m) }

What is wrong with this? In both the cases, the output is

A
B
C
D .....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:40:47+00:00Added an answer on June 13, 2026 at 3:40 am

    That’s just how puts is treating arrays in ruby 1.9, it prints each element on a new line. The result is what you want, it just looks differently when printed 🙂 Try printing with .inspect, for example.

    arr = [['A','B'],['C','D'],['E','F'],['G','H'],['I','J'],['K','L'],
          ['M','N'],['O','P']]
    
    arr.each_slice(2) do |k,m|
       new_arr = [k,m]
       puts new_arr.inspect
    end
    # >> [["A", "B"], ["C", "D"]]
    # >> [["E", "F"], ["G", "H"]]
    # >> [["I", "J"], ["K", "L"]]
    # >> [["M", "N"], ["O", "P"]]
    

    http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-puts

    puts(obj, …) → nil

    Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.

    $stdout.puts("this", "is", "a", "test")
    

    produces:

    this
    is
    a
    test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes two string arguments and returns the
I trying to complete this exercise; Write a Lisp function that takes as input
I am trying to write a function in R that takes two arguments, x
I'm trying to write a unit test that will loop through all action methods
I'm trying to write a program that takes a large data frame and replaces
I'm trying to write an SQL Statement that needs to loop through a list
I am trying to make algorithm that takes two arrays, S and T of
I'm trying to write a small function that takes in a filepath (where the
I'm trying to write a really small program in Java that takes a string
I am currently trying to write a script that will loop through multiple directories.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.