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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:29:03+00:00 2026-06-01T07:29:03+00:00

Consider some function foo : def foo(input) input * 2 end How to get

  • 0

Consider some function foo:

def foo(input)
  input * 2
end

How to get the max value of input for some array a?

a = [3, 5, 7, 9, 6]

Something like the following (which doesn’t work) should return 9:

a.max do |value|
  foo(value)
end

How to do it?

Ruby 1.9.2

  • 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-01T07:29:05+00:00Added an answer on June 1, 2026 at 7:29 am

    You need max_by , not max. http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-max_by

    max:

    Returns the object in enum with the maximum value. The first form
    assumes all objects implement Comparable; the second uses the block to
    return a <=> b.

    a = %w(albatross dog horse)
    a.max                                  #=> "horse"
    a.max {|a,b| a.length <=> b.length }   #=> "albatross"
    

    So max does take a block, but it doesn’t do what you expected it to.

    max_by:

    Returns the object in enum that gives the maximum value from the given
    block.

    If no block is given, an enumerator is returned instead.

    a = %w(albatross dog horse)
    a.max_by {|x| x.length }   #=> "albatross"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following jasmine spec: describe(something.act(), function() { it(calls some function of my module,
Consider the following wrapper function that retrys a given function some given number of
Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #
Consider the following example: var funcToCall = function() {...}.bind(importantScope); // some time later var
Lets consider I have the following function: SomeType createSomeType(); which can throw depending on
We're designing a database in which I need to consider some FK(foreign key) constraints.
consider the following pgSQL statement: SELECT DISTINCT some_field FROM some_table WHERE some_field LIKE 'text%'
Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =
Consider the following line: install --with-some-params some_pkg_name.10000 install --with-some-params other_pkg_name.10000 install --with-some-params yet_other_pkg_name.10000 I'm
Consider the following HTML snippet containing some javascript utilizing prompt and unload . 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.