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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:55:01+00:00 2026-05-22T23:55:01+00:00

In a rails application, I have a number of attributes for a model called

  • 0

In a rails application, I have a number of attributes for a model called Record. I want to design a method that when called on an attribute, returns the name of the attribute (which is essentially a method on the Record object). This name is then passed to an Hash, which returns a number (for the sake of this example, say the number is a percentage which is then multiplied by the original attribute value to get a new value).

For example, say my Record has four attributes: teachers, students, principals, and parents. The method would then look like the following:

def name
  **something here**
end

and the corresponding new_value method and PRECENTAGE hash would look like this:

def new_value
  self * PERCENTAGE[self.name]
end

PERCENTAGE = {
  "teachers"   => 0.40,
  "students"   => 0.53,
  "principals" => 0.21,
  "parents"    => 0.87
}

Then, to execute this whole thing, I would do Record.students.new_value, which would return new number of students according to the percentage obtained in the hash.

I know that to get the name of a method that is currently executing, you can do something like this: (found on http://ryat.la/7RDk)

def this_method
  __method__
end

but that won’t work for me, because I need the name of the previously executed method.

If you have any suggestions as to an alternative approach to accomplishing my goal, I’d be happy to try something else.

  • 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-22T23:55:02+00:00Added an answer on May 22, 2026 at 11:55 pm

    Ryan, I’m struggling to understand your question, but I think this is what you want, for record.teachers_percent, for example:

    ["teachers", "students", "principals", "parents"].each do |attrib|
      Record.class_eval <<-RUBY
        def #{attrib}_percent
          #{attrib} * PERCENTAGE[#{attrib.inspect}]
        end
      RUBY
    end
    

    Although this is probably a cleaner solution, giving record.percent(:teachers) or record.percent("teachers"):

    class Record
      def percent(attrib)
        self.send(attrib) * PERCENTAGE[attrib.to_s]
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails simple application that has two main models. A person model
I have a simple Rails 3 application that has a number of models. A
In my rails application I have a rake task that reads emails using imap.
In a Rails application I have a situation where I want to update the
I am building a Rails application that contains Developer s who have Application s.
I have a Rails application that accepts file uploads of CSV files. When developing
I have a really simple Rails application that allows users to register their attendance
In a toy Rails application, I'm modelling a situation where you have a number
I have defined a method called ccy which takes in a number num ,
I have a rails application what I want to send my users notifications when

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.