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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:04:47+00:00 2026-06-07T08:04:47+00:00

Context : I am trying to put up a Decorator pattern in Ruby. As

  • 0

Context: I am trying to put up a Decorator pattern in Ruby. As a Decorator should delegate all unknown methods to the underlying objects, I used the Delegator class.
I could have used SimpleDelegator but I wanted to fully understand what I was doing.

So the basic code I came out with was :

class Decorator < Delegator
  def initialize(component)
    super
    @component = component
  end

  def __setobj__(o); @component = o   end
  def __getobj__;    @component       end
  def send(s, *a);   __send__(s, *a)  end
end

Which is ~exactly the same as the implementation of SimpleDelegator. Seems good.

But the thing I did not want was for the code handling the Decorator to know it is manipulating a Decorator. I want full transparency.

At this moment Decorator.new(Object.new).class returned Decorator

So I tinkered a bit and came up with this :

class Decorator < Delegator
  undef_method :==
  undef_method :class
  undef_method :instance_of?

  # Stores the decorated object
  def initialize(component)
    super
    @component = component
  end

  def __setobj__(o); @component = o   end
  def __getobj__;    @component       end
  def send(s, *a);   __send__(s, *a)  end
end

This way, I can safely use class or instance_of? on my Decorated object, it will send the method to the underlying object via method_missing (which is implemented by Delegator).

The thing is : I don’t understand why I had to undef :class and :instance_of?. I can see that BasicObject defines :== so I had to undefine it but what about those two ?
I looked at the BasicObject documentation and a bit in the C code but did not find anything. I looked the same at the Delegator documentation and code, and did not find anything either.
It seems Delegator include the Kernel module, but Kernel#class or Kernel#instance_of? don’t exist.

Where those two method came from ? Why did I need to undefine them if they were not implemented at all ?
I guess I must be missing something about Ruby’s object model or something.

Thanks.

  • 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-07T08:04:49+00:00Added an answer on June 7, 2026 at 8:04 am

    You can get a hint by inspecting the method:

    Decorator.instance_method(:class)
      # =>  #<UnboundMethod: Decorator(#<Module:0x00000102137498>)#class> 
    

    The method’s owner is Decorator but actually defined in #<Module:0x00000102137498>. So there is an anonymous module that defines it. Interesting… let’s look at:

    Decorator.ancestors
      # => [Decorator, Delegator, #<Module:0x00000102137498>, BasicObject] 
    

    There’s that module again, between Delegator and BasicObject. So Delegator doesn’t directly derive from BasicObject. If you look at the source code in lib/delegate.rb you find:

    class Delegator < BasicObject
      kernel = ::Kernel.dup
      kernel.class_eval do
        [:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
          undef_method m
        end
      end
      include kernel
      # ...
    

    So a copy of the Kernel module is made, which doesn’t have to_s, inspect, etc… but still has class and instance_of?. It’s included in Delegator and that’s where they come from.

    Note that Object inherits the same methods by including the Kernel module (but it includes the full module, of course):

    42.method(:class) # => #<Method: Fixnum(Kernel)#class>
    

    This is stated in the Object doc:

    Object mixes in the Kernel module, making the built-in kernel
    functions globally accessible. Although the instance methods of Object
    are defined by the Kernel module, we have chosen to document them here
    for clarity.

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

Sidebar

Related Questions

I'm trying to make a custom context menu. I'll eventually put in all the
I am trying to put some initialization methods in controller's default constructor, but the
i am trying to put context path for an image in HTML. <img src=/MyWeb/images/pageSetup.gif>
I am trying to put up an Undo context Menu on UltraWinGrid to undo
Context: Asp.Net MVC3 w/Razor I am trying to put a login form on a
I'm trying to put some HTML content inside <content:encoded> tags using ROME and its
I'm trying to put together a fun 'contest' of sorts. Developers will write a
Context I am trying to understand the internals of a big, hairy JavaScript library.
Context I'm trying to have some "plugins" (I'm not sure this is the correct
Context: I am trying to perform a custom animation from a normal UIViewController.view 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.