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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:44:33+00:00 2026-05-16T18:44:33+00:00

My friends and I are working on some basic Ruby exercises to get a

  • 0

My friends and I are working on some basic Ruby exercises to get a feel for the language, and we’ve run into an interesting behavior that we’re yet unable to understand. Basically, we’re creating a tree data type where there’s just one class, node, which contains exactly one value and an array of zero or more nodes. We’re using rspec‘s autospec test runner. At one point we started writing tests to disallow infinite recursion (a circular tree structure).

Here’s our test:

it "breaks on a circular reference, which we will fix later" do
  tree1 = Node.new 1
  tree2 = Node.new 1
  tree2.add_child tree1
  tree1.add_child tree2
  (tree1 == tree2).should be_false
end

Here’s the Node class:

class Node
  attr_accessor :value
  attr_reader :nodes

  def initialize initial_value = nil
    @value = initial_value
    @nodes = []
  end

  def add_child child
    @nodes.push child
    @nodes.sort! { |node1, node2| node1.value <=> node2.value }
  end

  def == node
    return (@value == node.value) && (@nodes == node.nodes)
  end
end

We expect the last line of the test to result in an infinite recursion until the stack overflows, because it should continually compare the child nodes with each other and never find a leaf node. (We’re under the impression that the == operator on an array will iterate over the array and call == on each child, based on the array page of RubyDoc.) But if we throw a puts into the == method to see how often it’s called, we discover that it’s called exactly three times and then the test passes.

What are we missing?

Edit: Note that if we replace be_false in the test with be_true then the test fails. So it definitely thinks the arrays are not equal, it’s just not recursing over them (aside from the three distinct calls to ==).

  • 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-16T18:44:34+00:00Added an answer on May 16, 2026 at 6:44 pm

    If you click on the method name of the RubyDoc you linked to, you will see the source (in C) of the Array#== method:

    {
        // [...]
        if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
        if (rb_inspecting_p(ary1)) return Qfalse;
        return rb_protect_inspect(recursive_equal, ary1, ary2);
    }
    

    This implementation (specifically the “recursive_equal”) suggests that Array#== already implements the infinite recursion protection you’re after.

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

Sidebar

Related Questions

I'm working on a pretty basic CMS for some friends of mine. I made
I'm working with some friends on a small private mini MMORPG game server, and
I'm working on a C project with some friends for school. They helped me
I am working on some project that needs that when ever a friend of
The logo on my friends website is working properly in all browsers yet when
I've started working on a new web project with some friends... we are using
While working with some templates and writing myself a basic container class with iterators,
hello friends i am working on a like module .and having some difficulties running
I'm working on a project with some friends over Github for a University project.
Working with MVC 2 ad with the help of some friends I thought all

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.