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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:44:03+00:00 2026-05-24T19:44:03+00:00

Is it possible to over-ride methods that are part of Ruby itself, such as

  • 0

Is it possible to over-ride methods that are part of Ruby itself, such as rb_error_frozen, that are written in C, with Ruby code?

Background: I’m wondering if it’s possible to make Ruby merely log a warning, rather than raise an exception, when a frozen object is modified. That way, I can log a variety of state modifications, rather than stopping when the first one occurs.

I’m primarily thinking of doing this with YARV, but I could use another implementation if that made it easier.

And yes, it’s a whyday project! Don’t try this in a production environment!

  • 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-24T19:44:04+00:00Added an answer on May 24, 2026 at 7:44 pm

    I can only speak for MRI/YARV, but I’ll give it a try. You can only override functions that originate in C in Ruby if the C function has been explicitly defined as a method on a Ruby object. For example, Kernel#extend is explicitly defined in C as

    rb_define_method(rb_mKernel, "extend", rb_obj_extend, -1);
    

    So because the C function rb_obj_extend has been “linked” (in quotes because I’m figuratively speaking, I don’t mean C linkage here) with the method Kernel#extend in the Ruby world, in theory you could override rb_obj_extend‘s behaviour if you override Kernel#extend.

    I would say given the following two conditions you could claim that you actually “overrode” a rb_* C function:

    • the rb_* C function has been “linked” with some Ruby object, so we have a handle in Ruby world that serves as a hook that we can override
    • the given rb_* method is only used in this one spot for exactly this purpose, it’s reused nowhere else

    Now if you look at rb_error_frozen it fulfills neither of these two conditions. It’s a helper in the C implementation, meaning it’s called from several places. And it has not been explicitly “linked” with any Ruby object, so you have no hook where you could override it.

    Not all is lost, though. You can’t directly override rb_error_frozen, but what you could still try is to override all the Ruby methods where rb_error_frozen bubbles up to the “Ruby surface”. What I mean by that is that you could check all the places in the C sources where rb_error_frozen is used and from these places try to find each and every Ruby method that could trigger these bits of code. If this is a closed set, you could simply override all of these methods in order to “de-facto-override” rb_error_frozen‘s behaviour.

    This is only a patchwork solution, however. All your hard work is lost should somebody decide to write another C extension where they again call rb_error_frozen directly.

    So long story short: You can only override a C function if it has been explicitly defined as the implementation of some method of a Ruby object, e.g. as in

    rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);
    

    where you can assume that it will only ever be used for that purpose only. But even then you’re not a 100% safe, somebody could still decide to reuse that function in some other part of C code.


    Edit: You said you’d like Ruby only to warn instead of raise if a frozen object was modified. I just went through the sources to see if you could override all of the places where rb_error_frozen is called. The problem is rb_check_frozen – it’s called anywhere where an object is modified (as it ought to be) and again itself calls out to rb_error_frozen. This mechanism is deeply rooted in the C internals and not published at the Ruby surface everywhere, so there’s no way to override the “raising behaviour” or at least none that would not require significant effort. If you think about it for a minute that is actually a good thing. If it were possible to simply override the behaviour then this could actually be seen as a security flaw in the Ruby implementation. Freezing an object should guarantee you that it stays unmodifiable no matter what.

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

Sidebar

Related Questions

My question is simple; is it possible to over object-orient your code? How much
Over in this question, Scott writes that it is possible to get the current
I was wondering if it's possible to wrap some class methods with a decorator
In Java, it's possible to have methods inside an enum. Is there such possibility
Is it possible to override the background color of the minimize and maximized buttons
Is it possible to override a generisized function as illustrated in the code snippet
On webpart-pages it is possible modify the contained webparts over the Web by using
I've this action filter that executes before all the action methods executes. protected override
Is it possible to set up Intellij to generate javadoc for methods and classes,
From some of the action methods I want to return a result that will

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.