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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:51:34+00:00 2026-05-23T03:51:34+00:00

I have a class that has hashes in various stages of completion. This is

  • 0

I have a class that has hashes in various stages of “completion”. This is to optimize so that I don’t have to keep recreating hashes with root data that I already know. For example this is a counter called @root that would serve as a starting point.

{3=>4, 4=>1, 10=>3, 12=>5, 17=>1}

and it took key+key+key+key+key number of iterations to create @root. But now I have all combinations of [x,y] left to be added to the counter and individually evaluated. So I could do it like:

a = (1..52)
a.combination{|x,y|
  evaluate(x,y)
}

But instead of I would like to do this:

a.each{|x| 
  evaluate(x, "foo")
  a.each {|y| evaluate(y, "bar")}
}

Where i have a method like this to keep track of the hash at each state:

def evaluate index, hsh
    case hsh
    when "root"
        @root.key?(index) ? @root[index] += 1 : @root[index] = 1
    when "foo"
        @foo = @root.clone
        @foo.key?(index) ? @foo[index] += 1 : @foo[index] = 1
    when "bar"
        @bar = @foo.clone
        @bar.key?(index) ? @bar[index] += 1 : @bar[index] = 1
    end
end

But there is alot of repetition in this method. Is there a way that I could do this dynamically without using eval?

  • 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-23T03:51:35+00:00Added an answer on May 23, 2026 at 3:51 am

    Instead of using hsh as a string descriptor, you can directly pass the hash object as parameter to your method evaluate? E.g. instead of evaluate(x, "foo") you write

    @foo = @root.clone
    evaluate(x, @foo)
    

    Also note the @root.clone in your code overwrites the field several times inside the loop.

    Additionally if you use a default initializer for your hash you save quite some logic in your code. E.g. the code lines

    h = Hash.new{0}
    ...
    h[index] += 1
    

    will set the default value to zero if non was set for index. Thus you do not have to take care of the special case inside your evaluate method.

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

Sidebar

Related Questions

I have a class that has an Action<string> called DisplayData ; In various points
I have a class that has a dozen or so properties that represent various
I have a class that has private fields... (cars) I then inherit from this
i have a class that has something like this public class Foo { public
I have this class that has a delegate method. Its delegate has a navigationController.
I have a class that has this code, pCollection pub = RSSXmlDeserializer.GetPub(path, fReload); Get
I have a class that has this in the initializer: @implementation BaseFooClass -(id) init
You have a class that has to send a message to its parent. This
i have a class that has a ConcurrentDictionary as a private member. This class
I have class B that has class A, this is simplified version of class

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.