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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:39:07+00:00 2026-06-09T01:39:07+00:00

I always have a brain cramp when it comes to this. I’m creating a

  • 0

I always have a brain cramp when it comes to this. I’m creating a module to mix in to model-like classes but it needs to keep exactly one copy of serializable attributes per class. So here is the code (that doesn’t work).

module Checkin
  module Model
    def self.included(base)
      base.extend(ClassMethods)
    end

    @@serialiable_attrs = []   <== really not the right place
    module ClassMethods
      def serializable(*attrs)
        attrs.each{|attr| @@serializable_attrs << attr} # Is this @@ or just @?
      end
    end

    def serialize!
      @@serializable_attrs.each{|a| do_something_with(a)} # <== Will this work?
    end
  end
end

class Person  
  include Checkin::Model
  serializable :first_name, :original_name, :last_name, :checked_in, :people_attending
  # etc., etc.
end

What I’m wrangling with are two things:

  1. How to define my mix-in such that a class variable magically springs into existence; and
  2. How to access that variable both in my ClassMethods module and in the (for lack of a better term) instance methods part of the module.

Note that I’ve settled on a mix-in technique rather than inheritance because I will have Validatable, Persistable, Serializable and so on. Also, I know there are all sorts of validation and persistence layers available that are well tested and heavily used. This is a different beast and I really should know who to do this in my sleep, right?

Any Ruby wizards who can help me understand how to do this or suggest a different direction to approach this problem from, I appreciate the help!

  • 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-09T01:39:09+00:00Added an answer on June 9, 2026 at 1:39 am

    Try removing the class variable, and adding this to the module ClassMethod:

    def self.extended(klass)
      klass.instance_variable_set("@serializable_attrs", [])
    end
    

    And changing the double-@ to single in serializable. And change serialize! to this:

    self.class.instance_variable_get("@serializable_attrs").each{|a| do_something_with(a)}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to always have my model look to see if an object
I always have used Spring Framework classes as-is. However I am thinking of customizing
I always have trouble with complicated SQL queries. This is what I have $query
This is a brain-dead newbie question, but here goes: What determines what files get
I always have this problem. When a windows seems too small to view code,
I always have this notion that writing SQL queries in the code behind is
I always have heard this motto from many entrepeneurs: do what you are passionate
I always have trouble mixing languages and I recently started with MYSQL. In this
I have got a bit of 'brain fade' going on this afternoon, so if
I've always felt that my graphic design skills have lacked, but I do have

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.