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 3672554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:37:24+00:00 2026-05-19T02:37:24+00:00

I’m in that uncomfortable position again, where somebody has left me with a code

  • 0

I’m in that uncomfortable position again, where somebody has left me with a code snippet in a language I don’t know and I have to maintain it. While I haven’t introduced Ruby to myself some parts of it are quite simple, but I’d like to hear your explanations nonetheless.
Here goes:

words = File.open("lengths.txt") {|f| f.read }.split # read all lines of a file in 'words'?

values = Array.new(0)
words.each { |value| values << value.to_i } # looked this one up, it's supposed to convert to an array of integers, right?
values.sort!
values.uniq!

diffs = Array.new(0) # this looks unused, unless I'm missing something obvious
sum = 0
s = 0 # another unused variable
# this looks like it's computing the sum of differences between successive
# elements, but that sum also remains unused, or does it?
values.each_index { |index| if index.to_i < values.length-1 then sum += values.at(index.to_i + 1) - values.at(index.to_i) end } # could you also explain the syntax here?
puts "delta has the value of\n"

# this will eventually print the minimum of the original values divided by 2
puts values.at(0) / 2

The above script was supposed to figure out the average of the differences between every two successive elements (integers, essentially) in a list. Am I right in saying this is nowhere near what it actually does, or am I missing something fundamental, which is likely considering I have no Ruby knowledge?

  • 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-19T02:37:25+00:00Added an answer on May 19, 2026 at 2:37 am

    That guy surely didn’t grasp Ruby himself. I wonder why he chose to use that language.

    Here’s an annotated explanation:

    # Yes, it reads all lines of a file in words (an array)
    words = File.open("lengths.txt") {|f| f.read }.split
    
    values = Array.new(0)
    # Yes, to_i convert string into integer
    words.each { |value| values << value.to_i }
    values.sort!
    values.uniq!
    
    # diffs and s seem unused
    diffs = Array.new(0)
    sum = 0
    s = 0
    
    # The immediate line below can be read as `for(int index = 0; index < values.length; index++)`
    values.each_index { |index|
        # index is integer, to_i is unnecessary
        if index.to_i < values.length-1 then
    
            # The `sum` variable is used here
            # Following can be rewritten as sum += values[i-1] - values[i]
            sum += values.at(index.to_i + 1) - values.at(index.to_i)
        end
    }
    
    puts "delta has the value of\n"
    
    # Yes, this will eventually print the minimal of the original values divided by 2
    puts values.at(0) / 2
    

    To help you get a better grasp of what “real” (idiomatic) Ruby looks like, I’ve written what you wanted, with some annotations

    values = open("lengths.txt") do |f|
        # Read it like this:
        #
        # Take the list of all lines in a file,
        # apply a function to each line
        # The function is stripping the line and turning it 
        # into an integer
        # (This means the resultant list is a list of integers)
        #
        # And then sort it and unique the resultant list
        #
        # The eventual resultant list is assigned to `values`
        # by being the return value of this "block"
        f.lines.map { |l| l.strip.to_i }.sort.uniq
    end
    
    # Assign `diffs` to an empty array (instead of using Array.new())
    diffs = []
    values.each_index do |i|
        # Syntactic sugar for `if`
        # It applies the 1st part if the 2nd part is true
        diffs << (values[i+1] - values[i]) if i < values.length - 1
    end
    
    # You can almost read it like this:
    #
    # Take the list `diffs`, put all the elements in a sentence, like this 
    # 10 20 30 40 50
    #
    # We want to inject the function `plus` in between every element, 
    # so it becomes
    # 10 + 20 + 30 + 40 + 50
    #
    # The colon `:+` is used to refer to the function `plus` as a symbol
    #
    # Take the result of the above summation, divided by length,
    # which gives us average
    delta = diffs.inject(:+) / diffs.length
    
    # `delta` should now contains the "average of differences" between 
    # the original `values`
    
    # String formatting using the % operator
    # No \n needed since `puts` already add one for us
    puts "delta has the value of %d" % delta
    

    That is by no means pushing the true power of Ruby, but you see why Rubyists get so enthusiastic about expressiveness and stuffs 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.