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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:06:57+00:00 2026-05-12T23:06:57+00:00

I play around with arrays and hashes quite a lot in ruby and end

  • 0

I play around with arrays and hashes quite a lot in ruby and end up with some code that looks like this:

sum = two_dimensional_array.select{|i|
  i.collect{|j|
    j.to_i
  }.sum > 5
}.collect{|i|
  i.collect{|j|
    j ** 2
  }.average
}.sum

(Let’s all pretend that the above code sample makes sense now…)

The problem is that even though TextMate (my editor of choice) picks up simple {...} or do...end blocks quite easily, it can’t figure out (which is understandable since even I can’t find a “correct” way to fold the above) where the above blocks start and end to fold them.

How would you fold the above code sample?

PS: considering that it could have 2 levels of folding, I only care about the outer consecutive ones (the blocks with the i)

  • 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-12T23:06:57+00:00Added an answer on May 12, 2026 at 11:06 pm

    To be honest, something that convoluted is probably confusing TextMate as much as anyone else who has to maintain it, and that includes you in the future.

    Whenever you see something that rolls up into a single value, it’s a good case for using Enumerable#inject.

    sum = two_dimensional_array.inject(0) do |sum, row|
      # Convert row to Fixnum equivalent
      row_i = row.collect { |i| i.to_i }
    
      if (row_i.sum > 5)
        sum += row_i.collect { |i| i ** 2 }.average
      end
    
      sum # Carry through to next inject call
    end
    

    What’s odd in your example is you’re using select to return the full array, allegedly converted using to_i, but in fact Enumerable#select does no such thing, and instead rejects any for which the function returns nil. I’m presuming that’s none of your values.

    Also depending on how your .average method is implemented, you may want to seed the inject call with 0.0 instead of 0 to use a floating-point value.

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

Sidebar

Related Questions

I'd like to play around with writing some very small, simple web-based games (checkers,
I'd like to play around with building a recommendations system, and by that I
I'm trying to play around with some music clustering algorithms, and I thought that
I was just having a play around with some code in LINQPad and noticed
I am using Interface Builder to play around with some ideas. I never noticed
I just wanted to play around with the jdk . Like changing the implementation
I play around a lot when I'm programming in FLEX, mostly cause I'm new
I'm starting to play around with the code-first approach to the entity framework, primarily
I've been looking around and I'm starting to worry that this isn't possible. Is
I'm new to Ruby and I'm just having a play around with ideas and

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.