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

  • SEARCH
  • Home
  • 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 527741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:54+00:00 2026-05-13T08:54:54+00:00

I am trying to understand this code snippet: while row = input.gets row.strip! next

  • 0

I am trying to understand this code snippet:

while row = input.gets
  row.strip!
  next if row.empty?

  valuesplit = row.split("---")
  a, b = valuesplit[1..2]
  unless a == b
    $hash1[a] ||= {} <--------------What is this line doing?  How is the whole loop
    $hash1[a][b] = true               being traversed?

    if $hash1[b] && $hash1[b][a] <-----Can you please describe this if() loop
      $hash2[a] ||= []
      $hash2[a] << b
      $hash2[b] ||= []
      $hash2[b] << a
    end
  end
end

NOTE: $hash1 = {}
$hash2 = {}

Thanks!

UPDATE

Input:

junkdata1 value1 value2
junkdata2 value3 value4
junkdata3 value5 value6

and so on.

Updated the code lines with comments too.

  • 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-05-13T08:54:54+00:00Added an answer on May 13, 2026 at 8:54 am
    # loops by reading in every line of the input
    # (input can be a file or another I/O object)
    # every line is stored successively in a variable
    # called "row"
    while row = input.gets
    
      # removes leading and trailing whitespace from
      # the string that is stored in the "row" variable
      row.strip! 
    
      # if the string is empty, continue to the next
      # line (go back to beginning of loop)
      next if row.empty? 
    
      # split the string into an array of substrings
      # based on the "---" delimiter
      valuesplit = row.split("---") 
    
      # assign the second substring in the valuesplit
      # array to a variable called a, and the third to
      # a variable called b
      a, b = valuesplit[1..2] 
    
      # if a and b are different
      unless a == b
    
        # initialize the hash1 dictionary's a entry
        # to an empty sub-dictionary if it is null
        $hash1[a] ||= {} 
    
        # in the hash1 dictionary, set a's entry
        # to a dictionary that has b as the entry
        # and true as the value
        $hash1[a][b] = true
    
        # if the value for the b entry in the hash1
        # dictionary is true (not false or null) AND the value for a's
        # entry of the dictionary found at the b
        # entry of the hash1 dictionary is true
        if $hash1[b] && $hash1[b][a] 
    
          # initialize the hash2 dictionary's a entry
          # to an empty arraylist if it null or false
          $hash2[a] ||= [] 
    
          # add b to this arraylist
          $hash2[a] << b
    
          # initialize the hash2 dictionary's b entry
          # to an empty arraylist if it null or false
          $hash2[b] ||= []
    
          # add a to this arraylist
          $hash2[b] << a 
        end # end of the if $hash1[b]... statement
      end # end of the unless a == b statement
    end # end of the gets loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 269k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Imagine 20 millions rows in a table, with an int… May 13, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer You don't need to use curl to retrieve that file,… May 13, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer Well, if your static nested class is public, as in… May 13, 2026 at 1:24 pm

Related Questions

I have a generic question about scope and encapsulation. Take two scenarios: Scenario 1:
I was trying to implement a sort of shared cache between two or more
I am trying to place an action to happen after an entire .aspx page
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
I need help programatically graphing more points than can fit in a single Excel

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.