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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:36:34+00:00 2026-05-14T01:36:34+00:00

I use a lot of iterations to define convenience methods in my models, stuff

  • 0

I use a lot of iterations to define convenience methods in my models, stuff like:

PET_NAMES.each do |pn|
define_method(pn) do
...
...
end

but I’ve never been able to dynamically define setter methods, ie:

def pet_name=(name)
...
end

using define_method like so:

define_method("pet_name=(name)") do
...
end

Any ideas? Thanks in advance.

  • 1 1 Answer
  • 2 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-14T01:36:34+00:00Added an answer on May 14, 2026 at 1:36 am

    Here’s a fairly full example of using define_method in a module that you use to extend your class:

    module VerboseSetter
      def make_verbose_setter(*names)
        names.each do |name|
          define_method("#{name}=") do |val|
            puts "@#{name} was set to #{val}"
            instance_variable_set("@#{name}", val)
          end
        end
      end
    end
    
    class Foo
      extend VerboseSetter
    
      make_verbose_setter :bar, :quux
    end
    
    f = Foo.new
    f.bar = 5
    f.quux = 10
    

    Output:

    @bar was set to 5
    @quux was set to 10
    

    You were close, but you don’t want to include the argument of the method inside the arguments of your call to define_method. The arguments go in the block you pass to define_method.

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

Sidebar

Related Questions

use this website a lot but first time posting. My program creates a number
I use the gitweb RSS feed a lot for my remote repositories but it
I use Netbeans auto format (ctrl+alt+f) a lot. It's a very nice function!. But
I end up with a lot of code like this: List<string> dates = someMethodCall();
Our applications use lot of custom built and third party libraries and right now
I am writing a client-side .NET application which is expected to use a lot
I have learned to use Lua a lot of the time and I also
I make a lot of use of 'do.call' to generate function calls. E.g: myfun
A lot of Java ME tutorials use the startApp() method for creating and initializing
I use $_SERVER[DOCUMENT_ROOT] a lot to make sure that all my links will work

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.