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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:51:17+00:00 2026-05-24T11:51:17+00:00

I want to create an object that acts as a specific class, such as

  • 0

I want to create an object that acts as a specific class, such as Fixnum, but isn’t an instance of that class nor its subclasses.

There are various use-cases for this. In the Fixnum case, I want to define a more specific integer type that is essentially a Fixnum but implements some extra logic, too. I can’t subclass Fixnum itself because immediate types such as Fixnum and Symbol can’t be subclassed.

Another use-case is mocking in automated tests: sometimes you want to create an object that acts like a certain class (usually a model instance) but is for technical reasons not an instance of that exact class.

Here’s how to create a specific integer type that delegates all methods to an internally stored fixnum:

require 'delegate'
require 'forwardable'

# integer representing a page number
class PageNumber < DelegateClass(Integer)
  extend Forwardable

  def initialize(value, name)
    @name = name
    super(value)
  end

  def inspect
    "#{@name} #{to_i}"
  end

  alias_method :to_i, :__getobj__
  def_delegators :to_i, :instance_of?, :kind_of?, :is_a?
end

This object can pass is_a? and similar checks:

page = PageNumber.new(1, "page")
page.is_a? Fixnum  #=> true

But nothing I do can make it pass the Module#=== type check:

# my problem:
Fixnum === page    #=> false

The fact that my object fails this check is very unfortunate, since the === method is used internally in case statements:

case page
when Fixnum
  # it will never get here
when String
  # ...
else
  # ...
end

My question is how can I create a mock type that passes the === check without augmenting the === methods on built-in classes?

  • 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-24T11:51:18+00:00Added an answer on May 24, 2026 at 11:51 am

    This is a hackish solution that I warned against in my question:

    Fixnum === page  #=> false
    
    Numeric.extend Module.new {
      def ===(obj)
        obj.instance_of?(PageNumber) or super
      end
    }
    
    Fixnum === page  #=> true
    

    It solves the problem but raises a question is it safe to do? I can’t think of any drawbacks of this method from the top of my mind but since we’re messing with a very important method here it might not be something we’d want to do.

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

Sidebar

Related Questions

I want to create such object in Java that will contain some dispatcher function
I want to create a Map object that can lose value only if we
My User object that I want to create and store in the datastore has
I want to create an object in python that is a collection of around
I want to create an object with a non-enumerable property(a property that does not
In Delphi, I want to be able to create an private object that's associated
I want to create a background thread that's owned by an object. When that
I want to create an object that is globally accessible after the DOM has
I want to create an object that contains 2 links to Users. For example:
I want to create an object that will work in a similar way to

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.