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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:45:34+00:00 2026-05-26T23:45:34+00:00

I am working with Ruby scripting language. I have a snippet here. Just want

  • 0

I am working with Ruby scripting language. I have a snippet here. Just want to know what is happening inside that with in the loop. Here is my code

#!/usr/bin/ruby

presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]

for ss in 0...presidents.length
  print ss, ": ", presidents[presidents.length - ss - 1], "\n";
end

I know that it is to print the array in reverse order, but my intention is to know what is happening in

"presidents[presidents.length - ss - 1]"

Please help me understanding this. I am really confused with this.

  • 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-26T23:45:34+00:00Added an answer on May 26, 2026 at 11:45 pm

    It means that, whoever wrote this code, he was still learning Ruby :-p. That’s more idiomatic and, I hope, self-explanatory:

    presidents.reverse_each.with_index do |name, index|
      puts "#{index}: #{name}"
    end
    

    About presidents[presidents.length - ss - 1]: ss starts at 0, so length-0-1 = length-1 -> last element of presidents. On the last iteration of the loop you have length - (length-1) -1 = 0, so the first element of presidents is shown. A simple reverse, nothing fancy.

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

Sidebar

Related Questions

I have some simple shell scripting tasks that I want to do For example:
Im working on Ruby c extension, I have following code from c program, VALUE
Im working with Ruby c extension, I have following sample code, const char varP[]
I'm working with a Ruby project for school, and have sadly not been able
We're working on a Ruby on Rails app that needs to take advantage of
I came from Java, and now I am working more with Ruby. One language
I'm working on a game (using Ruby) and planning to have it available in
I'm working on a socket app in Ruby Shoes, and want to send a
I am working on Ruby on Rails application, with Prototype. I want to display
I'm working with Ruby on rails 2.3.4 and I'd like to have a link

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.