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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:16:56+00:00 2026-06-01T16:16:56+00:00

I have the following defined: #!/usr/bin/env ruby class Something def self._attr_accessor key, value, type

  • 0

I have the following defined:

#!/usr/bin/env ruby

class Something
  def self._attr_accessor key, value, type
    (class << self; self; end).send( :attr_accessor, key.to_sym)
    instance_variable_set "@#{key}", value
  end
end

class Client < Something
  _attr_accessor 'foo_bar', 'json', String
end

my_something = Client.new
puts my_something.foo_bar

But I recieve the following error:

/test_inheritance.rb:18:in `<class:Client>': undefined method `foo_bar' for Client:Class (NoMethodError)
    from ./test_inheritance.rb:14:in `<main>'

The bit of metaprograming I am doing works:

#!/usr/bin/env ruby
class Something
  def self._attr_accessor key, value, type
    (class << self; self; end).send( :attr_accessor, key.to_sym)
    instance_variable_set "@#{key}", value
  end
end

class Client < Something
  _attr_accessor 'foo_bar', 'json', String

  puts self.foo_bar
end

my_something = Client.new

#puts my_something.foo_bar

As it outputs the proper result. But how do I define the _attr_accessor methods such that I am able to access it methods publicly?

  • 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-01T16:16:57+00:00Added an answer on June 1, 2026 at 4:16 pm

    For one I think you’re tripping over the fact that format is a reserved method on Class and it’s conflicting with your attr_accessor attempt.

    Secondly there’s a better way to do this. I’ve made a fairly robust “accessor” utility class for a project I’m working on. It allows you to define class-level defaults and still override instance definitions.

    The implementation looks like this:

    module OptionAccessor
      # Given a list of names, this declares an option accessor which works like
      # a combination of cattr_accessor and attr_accessor, except that defaults
      # defined for a class will propagate down to the instances and subclasses,
      # but these defaults can be over-ridden in subclasses and instances
      # without interference. Optional hash at end of list can be used to set:
      #  * :default => Assigns a default value which is otherwise nil
      #  * :boolean => If true, creates an additional name? method and will
      #                convert all assigned values to a boolean true/false.
      def option_accessor(*args)
        option_reader(*args)
        option_writer(*args)
      end
    
      # Given a list of names, this declares an option reader which works like
      # a combination of cattr_reader and attr_reader, except that defaults
      # defined for a class will propagate down to the instances and subclasses,
      # but these defaults can be over-ridden in subclasses and instances
      # without interference. Optional hash at end of list can be used to set:
      #  * :default => Assigns a default value which is otherwise nil
      #  * :boolean => If true, creates an additional name? method and will
      #                convert all assigned values to a boolean true/false.
      def option_reader(*names)
        names = [ names ].flatten.compact
        options = names.last.is_a?(Hash) ? names.pop : { }
    
        names.each do |name|
          iv = :"@#{name}"
    
          (class << self; self; end).class_eval do
            if (options[:boolean])
              define_method(:"#{name}?") do
                iv_value = instance_variable_get(iv)
    
                !!(iv_value.nil? ? (self.superclass.respond_to?(name) ? self.superclass.send(name) : nil) : iv_value)
              end
            end
    
            define_method(name) do
              iv_value = instance_variable_get(iv)
    
              iv_value.nil? ? (self.superclass.respond_to?(name) ? self.superclass.send(name) : nil) : iv_value
            end
          end
    
          define_method(name) do
            iv_value = instance_variable_get(iv)
    
            iv_value.nil? ? self.class.send(name) : iv_value
          end
    
          if (options[:boolean])
            define_method(:"#{name}?") do
              iv_value = instance_variable_get(iv)
    
              !!(iv_value.nil? ? self.class.send(name) : iv_value)
            end
          end
    
          instance_variable_set(iv, options[:default])
        end
      end
    
      # Given a list of names, this declares an option writer which works like
      # a combination of cattr_writer and attr_writer, except that defaults
      # defined for a class will propagate down to the instances and subclasses,
      # but these defaults can be over-ridden in subclasses and instances
      # without interference. Options can be specified:
      #  * :boolean => If true, converts all supplied values to true or false
      #                unless nil, in which case nil is preserved.
      def option_writer(*names)
        names = [ names ].flatten.compact
        options = names.last.is_a?(Hash) ? names.pop : { }
    
        names.each do |name|
          iv = :"@#{name}"
    
          (class << self; self; end).class_eval do
            if (options[:boolean])
              define_method(:"#{name}=") do |value|
                instance_variable_set(iv, value.nil? ? nil : !!value)
              end
            else
              define_method(:"#{name}=") do |value|
                instance_variable_set(iv, value)
              end
            end
          end
    
          if (options[:boolean])
            define_method(:"#{name}=") do |value|
              instance_variable_set(iv, value.nil? ? nil : !!value)
            end
          else
            define_method(:"#{name}=") do |value|
              instance_variable_set(iv, value)
            end
          end
        end
      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 script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit)
I have defined following in a ResourceDictionary: <DockPanel x:Key=errorDisplay LastChildFill=False> <Border Background=Red DockPanel.Dock=Top> <TextBlock
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following function defined inside my linked list class. The declaration in
I have the following interface defined to expose a .NET class to COM: [InterfaceType(ComInterfaceType.InterfaceIsDual)]
I have a bash-script (let's call it /usr/bin/bosh ) using the following she-bang line:
I have the following script: #!/usr/bin/perl use warnings; use strict; my $count = 0;
I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is
I have the following defined in a file called build-dependencies.xml <?xml version=1.0 encoding=UTF-8?> <project

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.