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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:56:23+00:00 2026-06-12T15:56:23+00:00

Given the simple example here: class Base @tag = nil def self.tag(v = nil)

  • 0

Given the simple example here:

class Base
  @tag = nil 

  def self.tag(v = nil) 
    return @tag unless v 
    @tag = v
  end 
end 

class A < Base 
  tag :A
end

class B < Base
  tag :B
end 

class C < Base; end

puts "A: #{A.tag}"
puts "B: #{B.tag}"
puts "A: #{A.tag}"
puts "C: #{C.tag}"

which works as expected

A: A
B: B 
A: A
C: 

I want to create a module that base will extend to give the same functionality but with all the tag information specified by the class. Eg.

module Tester 
  def add_ident(v); ....; end
end

class Base 
  extend Tester 

  add_ident :tag
end 

I’ve found i can do it with a straight eval, so:

def add_ident(v)
  v = v.to_s 
  eval "def self.#{v}(t = nil); return @#{v} unless t; @#{v} = t; end"
end

but i really dislike using eval string in any language.

Is there a way that i can get this functionality without using eval? I’ve gone through every combination of define_method and instance_variable_get/set i can think of and i can’t get it to work.

Ruby 1.9 without Rails.

  • 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-12T15:56:25+00:00Added an answer on June 12, 2026 at 3:56 pm

    You want to define a dynamic method on the singleton class of the class you’re extending. The singleton class of a class can be accessed with expression like this: class << self; self end. To open the scope of a class’s class, you can use class_eval. Putting all this together, you can write:

    module Identification
    
      def add_identifier(identifier)
        (class << self; self end).class_eval do
          define_method(identifier) do |*args|
            value = args.first
            if value
              instance_variable_set("@#{identifier}", value)
            else
              instance_variable_get("@#{identifier}")
            end
          end
        end
      end
    
    end
    
    class A
      extend Identification
    
      add_identifier :tag
    end
    

    If you’re using recent versions of Ruby, this approach can be replaced with Module#define_singleton_method:

    module Identification
    
      def add_identifier(identifier)
        define_singleton_method(identifier) do |value = nil|
          if value
            instance_variable_set("@#{identifier}", value)
          else
            instance_variable_get("@#{identifier}")
          end
        end
      end
    
    end
    

    I don’t believe you want to use self.class.send(:define_method), as shown in another answer here; this has the unintended side effect of adding the dynamic method to all child classes of self.class, which in the case of A in my example is Class.

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

Sidebar

Related Questions

Here is an implementation example of the algorigthm in the base absctract class from
Here's what I'm thinking: class Widget < ActiveRecord::Base has_one :widget_layout end class WidgetLayout <
Given a simple namespaced route map.namespace :api do |api| api.resources :genres end how can
Given a simple C# class definition like: [System.Windows.Markup.ContentProperty(PropertyOne)] public class SimpleBase { public string
Given a simple class: public class Person { public string FirstName; public string LastName;
Given a simple parent/child class structure. I want to use linqkit to apply a
Given this simple example that I got from the JOGAMP wiki, why would the
Note that I'm mirroring the example given here very closely. In fact, my situation
I am attempting to parse WSDL, along the lines of the example given here
UPDATE:: OK i am putting the original problem statement here Given the Main 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.