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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:11:44+00:00 2026-06-08T19:11:44+00:00

In the code below Ocean represents the heights of undersea mountains. atlantis is a

  • 0

In the code below Ocean represents the heights of undersea mountains. atlantis is a set of coordinates for various locations. I want to sum up the heights of the mountains referenced by atlantis.

require 'matrix'    
Ocean=Matrix[ [3,1,4,4,6,2,8,12,8,2],
        [6,2,4,13,25,21,11,22,9,3,],
        [6,20,27,34,22,14,12,11,2,5],
        [6,28,17,23,31,18,11,9,18,12],
        [9,18,11,13,8,9,10,14,24,11],
        [3,9,7,16,9,12,28,24,29,21],
        [5,8,4,7,17,14,19,30,33,4],
        [7,17,23,9,5,9,22,21,12,21,],
        [7,14,25,22,16,10,19,15,12,11],
        [5,16,7,3,6,3,9,8,1,5] ]

atlantis=[[2,3],[3,4]]




puts atlantis.inject {|sum, n| sum + Ocean[n(0),n(1)]}

Since atlantis is an array of arrays, n(0) should refer to the first element, 2, of the first array [2,3] the first time inject does the block. But the error I get

t1.rb:15:in block in <main>': undefined methodn’ for main:Object (NoMethodError)
from t1.rb:15:in each'
from t1.rb:15:in
inject’
from t1.rb:15:in `’

Seems to indicate it doesn’t even know what “n” is. What am I missing? And precisely what does n get/stand for in this case?

  • 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-08T19:11:46+00:00Added an answer on June 8, 2026 at 7:11 pm

    From the Ruby documentation for Enumerable#inject(...), that function returns the result of applying the given binary operator repeatedly to the seed and each successive value. So the return value should be an object of the type returned by the block procedure (or symbol). In your case, the block ultimately uses the + operator with numeric arguments, so the returned value should be a Numeric.

    You should modify your attempt by providing a seed value and using the correct syntax for array lookup:

    atlantis.inject(0) { |sum,n| sum + Ocean[n[0],n[1]] } # => 65
    

    So your call sequence will look something like this:

    # sum = seed = 0 (from .inject(0))
    # sum = sum + Ocean[atlantis[0][0], atlantis[0][1]] = 34
    # sum = sum + Ocean[atlantis[1][0], atlantis[1][1]] = 34 + 31
    # sum = 65
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code below causes infinate loop problem (as documented). So how do I set
The code below adds a value into a textbox: $('#mainTxtWeight').val(textWeight); But what I want
The code below is in C. When given a set of input numbers, it
Code below is working well as long as I have class ClassSameAssembly in same
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
This code below allows me to find the word error in all my files
The code below fails on the line: Class.forName(oracle.jdbc.driver.OracleDriver); with the error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver The
My code below gave a different length for the unsigned char pointer I expect.
The code below sets a select menu with UI selectmenu by $('.anyclass').selectmenu(); but the

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.