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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:40:22+00:00 2026-05-17T01:40:22+00:00

In Ruby, I have a hash of objects. Each object has a type and

  • 0

In Ruby, I have a hash of objects. Each object has a type and a value. I am trying to design an efficient function that can get the average of the values of all of objects of a certain type within the hash.

Here is an example of how this is currently implemented:

#the hash is composed of a number of objects of class Robot (example name)
class Robot
  attr_accessor :type, :value

  def initialize(type, value)
    @type = type
    @value = value
  end

end


#this is the hash that inclues the Robot objects
hsh = { 56 => Robot.new(:x, 5), 21 => Robot.new(:x, 25), 45 => Robot.new(:x, 35), 31 => Robot.new(:y, 15), 0 => Robot.new(:y, 5) }


#this is the part where I find the average
total = 0
count = 0
hsh.each_value { |r|  
if r.type == :x        #is there a better way to get only objects of type :x ?
  total += r.value 
  count += 1
end
} 

average = total / count

So my question is:

is there a better way to do this that does not involve looping through the entire hash?

Note that I can’t use the key values because there will be multiple objects with the same type in the same hash (and the key values are being used to signify something else already).

If there is an easy way to do this with arrays, that would also work (since I can convert the hash to an array easily).

Thanks!

EDIT: fixed error in my code.

  • 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-17T01:40:23+00:00Added an answer on May 17, 2026 at 1:40 am
    hsh.values.select {|v| v.type == :x}.map(&:value).reduce(:+) / hsh.size
    

    I am trying to design an efficient function that can get the average of the values of all of objects of a certain type within the hash

    Unless I misunderstood what you are trying to say, this is not what the code you posted does. The average value of the :x robots is 21 (there’s 3 :x robots, with values 5, 25 and 35; 5 + 25 + 35 == 65 and 65 divided by 3 robots is 21), but your code (and mine, since I modeled mine after yours) prints 13.

    is there a better way to get only objects of type 😡 ?

    Yes. To select elements, use the select method.

    is there a better way to do this that does not involve looping through the entire hash?

    No. If you want to find all objects with a given property, you have to look at all objects to see whether or not they have that property.

    Do you have actual hard statistical evidence that this method is causing your performance bottlenecks?

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

Sidebar

Related Questions

Each key in a hash has a value that's also a hash. { 100
I have a ruby hash that I'd like to render using RABL . The
I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
How to convert Ruby Hash to C# Dictionary? Do we have any built-in function...or
I'm kinda new to Flex. I have trying to send Hash from Ruby on
I have Ruby script that creates a proxy so that I can make HTTP
I have a function in Ruby: def find_item(keyword) potential = [] $items.each do |item|
I have a ruby hash that looks like this { stuff_attributes => { 1
I have a Ruby hash that contains a user-generated string. I need to pass
How can I create an Object in ruby that will be evaluated to false

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.