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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:08:55+00:00 2026-05-14T01:08:55+00:00

I’m trying to subclass Array in ruby to make it randomize its elements when

  • 0

I’m trying to subclass Array in ruby to make it randomize its elements when flatten! is called. Looking at the source code for Array#flatten (http://ruby-doc.org/core/classes/Array.src/M002218.html), it looks like it should recursively call flatten! on any array contained within an array. So, I tried doing something like this:

class RandArray < Array
    def randomize!
        self.sort!{rand(3)-1}
    end
    def flatten!
        randomize!
        super
    end
end

However, when a normal array contains my RandArray and flatten is called on the normal array, flatten! is never called in my array. I figure ruby is just calling some other method to flatten the arrays recursively, but I can’t figure out what that is. Any tips?

  • 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-14T01:08:56+00:00Added an answer on May 14, 2026 at 1:08 am

    I am not an absolute expert on this but Ruby’s Array is written as C code. here is the code for flatten! :

    static VALUE
    rb_ary_flatten_bang(ary)
        VALUE ary;
    {
        long i = 0;
        int mod = 0;
        VALUE memo = Qnil;
    
        while (i<RARRAY(ary)->len) {
            VALUE ary2 = RARRAY(ary)->ptr[i];
            VALUE tmp;
    
            tmp = rb_check_array_type(ary2);
            if (!NIL_P(tmp)) {
                if (NIL_P(memo)) {
                    memo = rb_ary_new();
                }
                i += flatten(ary, i, tmp, memo);
                mod = 1;
            }
            i++;
        }
        if (mod == 0) return Qnil;
        return ary;
    }
    

    As you can see on this line,

    i += flatten(ary, i, tmp, memo);
    

    and here is the implementation for this flatten C function :

    static long
    flatten(ary, idx, ary2, memo)
        VALUE ary;
        long idx;
        VALUE ary2, memo;
    {
        VALUE id;
        long i = idx;
        long n, lim = idx + RARRAY(ary2)->len;
    
        id = rb_obj_id(ary2);
        if (rb_ary_includes(memo, id)) {
        rb_raise(rb_eArgError, "tried to flatten recursive array");
        }
        rb_ary_push(memo, id);
        rb_ary_splice(ary, idx, 1, ary2);
        while (i < lim) {
        VALUE tmp;
    
        tmp = rb_check_array_type(rb_ary_elt(ary, i));
        if (!NIL_P(tmp)) {
            n = flatten(ary, i, tmp, memo);
            i += n; lim += n;
        }
        i++;
        }
        rb_ary_pop(memo);
    
        return lim - idx - 1;   /* returns number of increased items */
    }
    

    The flatten! code calls directly the C flatten function for any element of the array that validates rb_check_array_type it doesn’t go back to the ruby code.Instead it accesses the underlying C structure directly bypassing your overloaded implementation.

    Not sure how to override this, I think one way could be to reopen Array and rewrite the flatten and flatten! function as pure ruby.
    You would take a performance hit, but then you would be able to overload it as you see fit. And you could always use aliasing to have a “flatten_native” and a “flatten_native!” function on your modified array, to get the perfs back on some cases.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.