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

  • Home
  • SEARCH
  • 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 8928163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:22:27+00:00 2026-06-15T08:22:27+00:00

I have this class in a ruby on rails application class ChartData def self.user_logs_by_day(logs)

  • 0

I have this class in a ruby on rails application

class ChartData
  def self.user_logs_by_day(logs)
    days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
      logs.each do |log|
        days_with_hours[log.user.name][log.log_date] = log.total_hours
      end
    days_with_hours
  end

  def self.project_logs_by_day(logs)
    days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
      logs.each do |log|
        if !log.project.nil?
          days_with_hours[log.project.name][log.log_date] = log.total_hours
        else
          days_with_hours[@default][log.log_date] = log.total_hours
        end
      end
    days_with_hours
  end
end

I want to make the two methods into one like this, because I want to use this method for many models.

class ChartData
  def self.logs_by_day(logs, klass)
    days_with_hours = Hash.new{|h, k| h[k] = Hash.new(&h.default_proc)}
      logs.each do |log|
        if !log.klass.nil?
          days_with_hours[log.klass.name][log.log_date] = log.total_hours
        else
          days_with_hours[@default][log.log_date] = log.total_hours
        end
      end
    days_with_hours
  end
end

I want to pass the class or model into the method like this

ChartData.logs_by_day(logs, Project)

How can I handle the parameter Project in the model so it gets to be a instance method on the log?

A rather clumsy attempt

1.9.3p125 :026 > Project.name.downcase
 => "project" 

Any better? Other refactor suggestions are welcome

  • 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-15T08:22:28+00:00Added an answer on June 15, 2026 at 8:22 am

    Why do you want to pass the class? You’re not using it anywhere. Pass a symbol.

    ChartData.logs_by_day(logs, :project)
    

    and then

    def self.logs_by_day(logs, prop)
      # ...
      log.send(prop).name
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I'm using Rails 2.3.4/Ruby 1.9.1 for my application. In the view I have this
I have just installed paperclip into my ruby on rails blog application. Everything is
I have built a blog application w/ ruby on rails and I am trying
I have created a blog application using Ruby on Rails and have just added
I'm using Ruby on Rails to develop a web application. I have a wiki
I am writing ruby on rails application, which will have 2 different users types
I have just upgraded the application from Rails 2 to Rails 3 (ruby 1.9.2-head).
This is a Rails 3 application. I have images that can be tied to
I want to use Resque in a Ruby on Rails application. I have no

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.