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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:37:35+00:00 2026-06-04T22:37:35+00:00

I want to write some tree data structure in ruby. The class file: class

  • 0

I want to write some tree data structure in ruby. The class file:

class Tree
  attr_accessor :node, :left, :right
  def initialize(node, left=nil, right=nil)
    self.node=node
    self.left=left
    self.right=right
  end
end

The rspec file:

require 'init.rb'

describe Tree do
  it "should be created" do
    t2=Tree.new(2)
    t1=Tree.new(1)
    t=Tree.new(3,t1,t2)
    t.should_not be nil
    t.left.node should eql 1
    t.right.node should eql 2
  end
end

Rspec keeps complaining:

1) Tree should be created
     Failure/Error: t.left.node should eql 1
     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./app/tree.rb:3:in `initialize'
     # ./spec/tree_spec.rb:9:in `block (2 levels) in <top (required)>'

Why?? I move the spec code into the class file and it works out. What is wrong?

  • 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-04T22:37:36+00:00Added an answer on June 4, 2026 at 10:37 pm

    Believe it or not, the problem is two missing dots in your rspec. These lines:

    t.left.node should eql 1
    t.right.node should eql 2
    

    should be this:

    t.left.node.should eql 1
    t.right.node.should eql 2
    

    Insert that period before should, and your spec should pass.

    Here’s what’s going on. The should method works on any value, but if you call it bare, like this:

    should == "hello"
    

    it will operate on the subject of your test. What’s the subject? Well, you can set the subject to whatever you want using the subject method, but if you don’t, rspec will assume the subject is an instance of whatever class is being described. It sees this at the top of your spec:

    describe Tree
    

    and tries to create a subject like this:

    Tree.new
    

    which blows up, since your initialize won’t work without any arguments; it needs at least one. The result is a pretty cryptic error if you didn’t intend to write a should with an implicit subject.

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

Sidebar

Related Questions

I want to write some large amount of data to a file. the data
I want to write some text directly to a file using Flex 3 /
I want to write some bogus text in a file (helloworld text in a
I want to write a program to hash some data and preferably i would
How would I write a map function for an n-ary tree in ruby? class
Suppose I want to store a complicated data structure (a tree, say) to disk.
I want to write some javascript classes which inherit from the DOM Node interface...
I want to write some debugging output to the log to review it with
I want to write some games, but I don't have any game development experience.
I want to write some test cases for my web app, but stuck at

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.