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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:54:49+00:00 2026-06-07T10:54:49+00:00

The details here aren’t important, it’s just an example, all that matters is the

  • 0

The details here aren’t important, it’s just an example, all that matters is the i=0, i+=1 action

def sortAndIndex
  #sorting keys
  @disco = Hash[@disco.sort]

  #basic setup for both input types
  @years = @disco.keys
  @albums = @disco.values
  sum = @years.count

  #setup for "albums" input
  @allalbums = []
  i = 0
  sum.times do
  thatyear = @years[i] + ", " + @albums[i]
  @allalbums << thatyear
  i += 1
 end
end

Now this does work, as should any other “i++” type (“C-style”) iterator in ruby or most other languages. However, I keep hearing that ruby has this better way to do this, where basically I’d be passing a second parameter variable other than, here, ‘a’.

What modifications would I make to do this and are there multiple “shortcuts” which ruby has involving these sorts of i++ ary[i] iterator problems?

  • 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-07T10:54:52+00:00Added an answer on June 7, 2026 at 10:54 am

    Yes, you should do things the “Ruby way” and forget about indexing. Why? Because it is idiomatic and looks nicer. You pass in a block of code to be executed upon each element of a collection. How the elements are stored, in what order, if they can be indexed into, is all irrelevant.

    # nice, eh?
    some_array.each do |elem|
      some_action(elem)
    end
    

    If you need an index counter (you don’t in your example) use each_with_index instead.

    Also note that the concept of blocks is important in Ruby outside of iterating a collection. What you have there is… odd… and you should listen to them, but many methods take blocks as arguments (or optional arguments) which do not return collections which can be indexed. For example, File.open takes a block and, if supplied, closes the file for you when the block returns

    Also, another thing to note; your naming conventions are atypical for Ruby. Rubyists use lowercase function and variable names with underscores_between_words. Best to stick with the community here.

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

Sidebar

Related Questions

I have two tables. One changelog-table, the details of which aren't important here. Another
Here's some LINQ to select all order details. It creates a join with the
This is a theoretical question, so expect that many details here are not computable
Here is the problem details: 1) I want to create dynamic (ip based) download
Here is the model we are using to store the CC details how secure
Here is my code.. string attachment = attachment; filename=Call-Details-Report- + startDate.SelectedDate.Value.ToString(MM-dd-yyyy) + .csv; Response.Clear();
I am using hibernate 3.2 with struts 1.2 framework here is the details of
I'm creating a simple questions DB, here are some details to start: 1st off,
OK. So, here's some details on my situation : I've got a brand-new iPhone
I am having trouble running Purify on Suse Linux. Here are the version details:

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.